Given a number, the task is to check if this number is Odd or Even using Command Line Arguments. A number is called even if the number is divisible by 2 and is called odd if it is not divisible by 2.
Examples:
Input: 123
Output: No
Input: 588
Output: Yes
Approach:
- Since the number is entered as Command line Argument, there is no need for a dedicated input line
- Extract the input number from the command line argument
- This extracted number will be in string type.
- Convert this number into integer type and store it in a variable, say num
- Check if this number is completely divided by 2
- If completely divisible, the number is Even
- If not completely divisible, the number is Odd
Program:
C
#include <stdio.h>
#include <stdlib.h> /* atoi */
int isEvenOrOdd( int num)
{
return (num % 2);
}
int main( int argc, char * argv[])
{
int num, res = 0;
if (argc == 1)
printf ( "No command line arguments found.\n" );
else {
num = atoi (argv[1]);
res = isEvenOrOdd(num);
if (res == 0)
printf ( "Even\n" );
else
printf ( "Odd\n" );
}
return 0;
}
|
Java
class GFG {
public static int isEvenOrOdd( int num)
{
return (num % 2 );
}
public static void main(String[] args)
{
if (args.length > 0 ) {
int num = Integer.parseInt(args[ 0 ]);
int res = isEvenOrOdd(num);
if (res == 0 )
System.out.println( "Even\n" );
else
System.out.println( "Odd\n" );
}
else
System.out.println( "No command line "
+ "arguments found." );
}
}
|
Output:
- In C:

- In Java:

Time Complexity: O(1)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!