Rust program to print the EVEN numbers using Command Line Arguments

Rust Example: Write a program to print the EVEN number using Command Line Arguments.
Submitted by Nidhi, on November 26, 2021

Problem Solution:

In this program, we will find the EVEN numbers in Command Line Arguments and printed them.

Program/Source Code:

The source code to print the EVEN number using Command Line Arguments is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to print the EVEN numbers 
// using Command Line Arguments

fn main(){
   	let args = std::env::args(); 
   	let mut num=0;
   	let mut cnt=0;

   	for arg in args {
   		if cnt>0{
			num = arg.parse::<i32>().unwrap();
			
			if num%2==0{
				print!("{} ",num);
			}
		}
		cnt=1;
   	}
   	
   	println!();
}

Output:

$ rustc CheckEven.rs

Execute:
$ ./CheckEven 10 15 20
10 20

Explanation:

In the main() function, we found the even numbers in Command Line Arguments and printed them.

Rust Miscellaneous Programs »




Comments and Discussions!

Load comments ↻





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