TCS Coding Practice Question | Check Palindrome Number
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
// C program to check if a number is Palindrome // using command line arguments #include <stdio.h> #include <stdlib.h> /* atoi */ // Function to reverse the number int reverseNumber(int num) { // Variable to store the // resultant reverse number int rev_num = 0; // Traverse through the number digit by digit while (num > 0) { // Append the last digit of num // as the next digit of rev_num rev_num = rev_num * 10 + num % 10; // Remove the last digit from the num num = num / 10; } // Return the reversed number return rev_num; } // Function to reverse a string int isPalindrome(int num) { int rev_num = reverseNumber(num); if (num == rev_num) return 1; else return 0; } // Driver code int main(int argc, char* argv[]) { int num, res = 0; // Check if the length of args array is 1 if (argc == 1) printf("No command line arguments found.\n"); else { // Get the command line argument and // Convert it from string type to integer type // using function "atoi( argument)" num = atoi(argv[1]); // Check if it is Palindrome res = isPalindrome(num); // Check if res is 0 or 1 if (res == 0) // Print No printf("No\n"); else // Print Yes printf("Yes\n"); } return 0; } |
chevron_right
filter_none
Java
// Java program to check if a number is Palindrome // using command line arguments class GFG { // Function to reverse the number public static int reverseNumber(int num) { // Variable to store the // resultant reverse number int rev_num = 0; // Traverse through the number digit by digit while (num > 0) { // Append the last digit of num // as the next digit of rev_num rev_num = rev_num * 10 + num % 10; // Remove the last digit from the num num = num / 10; } // Return the reversed number return rev_num; } // Function to reverse a string public static int isPalindrome(int num) { int rev_num = reverseNumber(num); if (num == rev_num) return 1; else return 0; } // Driver code public static void main(String[] args) { // Check if length of args array is // greater than 0 if (args.length > 0) { // Get the command line argument and // Convert it from string type to integer type int num = Integer.parseInt(args[0]); // Get the command line argument // and check if it is Palindrome int res = isPalindrome(num); // Check if res is 0 or 1 if (res == 0) // Print No System.out.println("No\n"); else // Print Yes System.out.println("Yes\n"); } else System.out.println("No command line " + "arguments found."); } } |
chevron_right
filter_none
Output:
Recommended Posts:
- TCS Coding Practice Question | Check Armstrong Number
- TCS Coding Practice Question | Palindrome String
- TCS Coding Practice Question | Check Odd or Even
- TCS Coding Practice Question | Sum of Digits of a number
- TCS Coding Practice Question | Reverse a Number
- TCS Coding Practice Question | Factorial of a Number
- TCS Coding Practice Question | Checking Prime Number
- TCS Coding Practice Question | HCF or GCD of 2 Numbers
- TCS Coding Practice Question | LCM of 2 Numbers
- TCS Coding Practice Question | Fibonacci Series
- TCS Coding Practice Question | Greatest of 3 Numbers
- TCS Coding Practice Question | Swap two Numbers
- TCS Coding Practice Question | Concatenate 2 Strings
- TCS Coding Practice Question | Average of 2 Numbers
- TCS Coding Practice Question | Reverse a String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





