Given a number, the task is to check if this number is Palindrome or not using Command Line Arguments.
Examples:
Input: 123
Output: No
Input: 585
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
- Find the reverse of this number and store in a variable, say rev_num
- Check if this rev_num and num are same or not.
- If they are not same, the number is not Palindrome
- If they are same, the number is a Palindrome
Program:
C
#include <stdio.h>
#include <stdlib.h> /* atoi */
int reverseNumber(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int isPalindrome(int num)
{
int rev_num = reverseNumber(num);
if (num == rev_num)
return 1;
else
return 0;
}
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 = isPalindrome(num);
if (res == 0)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
|
Java
class GFG {
public static int reverseNumber(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
public static int isPalindrome(int num)
{
int rev_num = reverseNumber(num);
if (num == rev_num)
return 1;
else
return 0;
}
public static void main(String[] args)
{
if (args.length > 0) {
int num = Integer.parseInt(args[0]);
int res = isPalindrome(num);
if (res == 0)
System.out.println("No\n");
else
System.out.println("Yes\n");
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
|
Output:- In C:

- In Java:
