# TCS Coding Practice Question | Check Armstrong Number

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 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 number digits (or find order) of the number num and store it in a variable, say n.
• For every digit r in input number num, compute rn.
• If sum of all such values is equal to num
• If they are not same, the number is not Armstrong
• If they are same, the number is a Armstrong

Program:

## C

 `// C program to check if a number is Armstrong ` `// using command line arguments ` ` `  `#include ` `#include /* atoi */ ` ` `  `// Function to calculate x raised to the power y ` `int` `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 number ` `int` `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 not ` `int` `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 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); ` ` `  `        ``// 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

 `// 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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Content Manager at LambdaTest

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.

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.