Q:

Rust program to print prime numbers from the array

belongs to collection: Rust Arrays Programs

0

In this program, we will create an array of integer elements then we will find the prime numbers from the array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to print prime numbers from the array is given below. The given program is compiled and executed successfully.

// Rust program to print prime numbers 
// from the array

fn main() 
{
    let mut arr:[usize;5] = [1,5,11,26,23];
    let mut flag:i32 = 0;
    let mut i:usize = 0;
    let mut j:usize = 0;
	
    println!("Prime Numbers: ");
    while i<arr.len()
    {
    	flag = 0;
    	j=2;
    	while j<(arr[i]/2)
    	{
    		if arr[i]%j == 0 {
    			flag = 1;
    			break;
    		}
    		j=j+1;
    	}
    	if (flag == 0) && (arr[i]>1) {
    		print!("{} ", arr[i]);
    	}
    	i=i+1;
    }
}

Output:

Prime Numbers: 
5 11 23

Explanation:

Here, we created an array of integers with 5 elements, and then we found the prime numbers from the array and printed them.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to search an item into the array usin... >>
<< Rust program to merge two arrays into third array...