Home / Programs / Command-Line program to find Armstrong number using Command line arguments
🚀 Programming Example

Command-Line program to find Armstrong number using Command line arguments

👁 1,369 Views
💻 Practical Program
📘 Step Learning
In this section you will learn about Command-Line program to find Armstrong number using Command line arguments.

📌 Information & Algorithm

A number is called Armstrong number if the Sum of the cubes of its digits is equal to the number itself.

C program to find Armstrong number using Command line arguments

The following is a C program to check whether the given number is Armstrong number or not using command line arguments.


💻 Program Code

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
 int Given_number= atoi(argv[1]);
 int num;
 for(num=1; num<=Given_number; num++)
 {
 int a=num;
 int s=0;
 int r=0;

while(a>0)
 {
 s=a%10;
 r=r+(s*s*s);
 a=a/10;
 }
 if(r==num)
 printf(" %d is armstrong no \n", num);
 }
}
                        

🖥 Program Output

1637

 1 is armstrong no 
 153 is armstrong no 
 370 is armstrong no 
 371 is armstrong no 
 407 is armstrong no 



                            

📘 Explanation

#include

void main(int argc, char * argv[])

{

int num,num1,arms=0,rem;

if ( argc != 2 )

{

printf("Enter the number:\n");

scanf("%d",&num);

}

else

{

num = atoi(argv[1]);

}

num1=num;

while(num>0)

{

rem=num%10;

arms=arms+rem*rem*rem;

num=num/10;

}

if(num1==arms)

{

printf(" \n%d is an Armstrong number",num1);

}

else

{

printf("\n%d is NOT an Armstrong number",num1);

}

}
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.