Rust program to print prime numbers from the array

Rust | Array Example: Write a program to reverse an array.
Submitted by Nidhi, on October 21, 2021

Problem Solution:

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

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.

Rust Arrays Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.