Write a program to check a given number is Armstrong using command line argument
//definition of main in command-line arguments
int main(int argc, char *argv[])
{
//some coding
}
- argc – It is known as argument count, it stores the count of the number of arguments.
- argv[] – Pointer, contains location of all the values(arguments).
- *argv[] – Array of values of all the arguments
Approach
C
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(int argc, char *argv[]){if (argc != 2){printf("Enter a number: ");return -1;}else{int num = atoi(argv[1]);int sum = 0;int n = num;while (n > 0){int temp = n % 10;sum += pow(temp, 3);n = n / 10;}if (sum == num){printf("Number is armstrong ");}else{printf("Number is not armstrong ");}}return 0;}
To Run a Program
- Go to the location/directory where your program is present.
- g++ proramName.c -o out
- ./out 153
Example:
Input: num = 153 Output: Number is armstrong
No comments:
Post a Comment