TCS Coding Practice Question | Check Armstrong Number

06 Jul, 2022
Given a number, the task is to check if this number is Armstrong or not using Command Line Arguments. A positive integer of n digits is called an Armstrong number of order n (order is the number of digits) if.
abcd... = pow(a, n) + pow(b, n) + pow(c, n) + pow(d, n) + .... 
Example:
Input: 153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9

Input: 1253
Output: No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723

Input: 1634
Output: Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
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
Count the number of digits (or find the order) of the number num and store it in a variable, say n.
For every digit r in input number num, compute rn.
If the sum of all such values is equal to num
If they are not same, the number is not Armstrong
If they are the same, the number is an Armstrong Number.
Program: 
C
Java
// C program to check if a number is Armstrong// using command line arguments #include <stdio.h>#include <stdlib.h> /* atoi */ // Function to calculate x raised to the power yint power(int x, unsigned int y){    if (y == 0)        return 1;    if (y % 2 == 0)        return power(x, y / 2) * power(x, y / 2);    return x * power(x, y / 2) * power(x, y / 2);} // Function to calculate order of the numberint order(int x){    int n = 0;    while (x) {        n++;        x = x / 10;    }    return n;} // Function to check whether the given number is// Armstrong number or notint isArmstrong(int x){    // Calling order function    int n = order(x);    int temp = x, sum = 0;    while (temp) {         int r = temp % 10;        sum += power(r, n);        temp = temp / 10;    }     // If satisfies Armstrong condition    if (sum == x)        return 1;    else        return 0;} // Driver codeint 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 Armstrong        res = isArmstrong(num);         // Check if res is 0 or 1        if (res == 0)            // Print No            printf("No\n");        else            // Print Yes            printf("Yes\n");    }    return 0;}
// Java program to check if a number is Armstrong// using command line arguments class GFG {     // Function to calculate x    // raised to the power y    public static int power(int x, long y)    {        if (y == 0)            return 1;        if (y % 2 == 0)            return power(x, y / 2) * power(x, y / 2);        return x * power(x, y / 2) * power(x, y / 2);    }     // Function to calculate order of the number    public static int order(int x)    {        int n = 0;        while (x != 0) {            n++;            x = x / 10;        }        return n;    }     // Function to check whether the given number is    // Armstrong number or not    public static int isArmstrong(int x)    {        // Calling order function        int n = order(x);        int temp = x, sum = 0;        while (temp != 0) {            int r = temp % 10;            sum = sum + power(r, n);            temp = temp / 10;        }         // If satisfies Armstrong condition        if (sum == x)            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 Armstrong            int res = isArmstrong(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.");    }}
Output:
In C: 
In Java: 
Time Complexity: O (log N)Auxiliary Space: O (1)
jayanth_mkv
TCS
TCS-coding-questions
C++ Programs
Java Programs
Placements
TCS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Shallow Copy and Deep Copy in C++
C++ Program to check if a given String is Palindrome or not
How to find the minimum and maximum element of a Vector using STL in C++?
C++ Program for QuickSort
C Program to Swap two Numbers
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class