ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.