Before going to the program first let us understand what is a Armstrong Number?
Armstrong Number:
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
In other word “A number is Armstrong if it is equal the sum of cube of its digits.”
For example,
371 is an Armstrong number because 33 + 73 + 13 = 27 + 343 + 1 = 371
431 is not an Armstrong number because 43 + 33 + 13= 64 + 27 + 1 = 92
Program code for Armstrong Number or Not in C:
#include<stdio.h> #include<conio.h> void main() { int n,num,r,ans=0; clrscr(); printf("Enter a positive integer: "); scanf("%d", &#); n=num; /* Loop to calculate the sum of the cubes of its digits */ do { r=n%10; ans=ans+r*r*r; n=n/10; }while(n>0); /* if else condition to print Armstrong or Not */ if(ans==num) { printf("%d is an Armstrong number.",num); } else { printf("%d is not an Armstrong number.",num); } getch(); }
Related: Armstrong Number or Not in C using While loop
Working:
- First the computer reads the positive integer value from the user.
- Then using do-while loop it calculates the sum of the cubes of its digits.
- Finally if else condition is used to print the number is Armstrong or Not .
Step by Step working of the above Program Code:
For Armstrong Number:
- Let us assume that a user enters the positive integer value as 153.
- It assigns the value of ans=0, num=153.
- It assigns the value of n=num(ie. n=153) and the loop continues till the condition of the do-while loop is true.
3.1. do
r=n%10 (r=153%10) So r=3
ans=ans+r*r*r (ans=0+3*3*3) So ans=27
n=n/10 (n=153/10) So n=15
n>0 (15>0) do-while loop condition is true
3.2. do
r=n%10 (r=15%10) So r=5
ans=ans+r*r*r (ans=9+5*5*5) So ans=152
n=n/10 (n=15/10) So n=1
n>0 (1>0) do-while loop condition is true
3.3. do
r=n%10 (r=1%10) So r=1
ans=ans+r*r*r (ans=152+1*1*1) So ans=153
n=n/10 (n=1/10) So n=0
n>0 (0>0) do-while loop condition is false
3.4. It comes out of the do-while loop and checks whether the number is Armstrong or not.
- ans==num (153==153) if condition is true
It prints 153 is an Armstrong number.
- Thus program execution is completed.
For Not an Armstrong Number:
- Let us assume that a user enters the positive integer value as 431.
- It assigns the value of ans=0, num=431.
- It assigns the value of n=num(ie. n=431) and the loop continues till the condition of the do-while loop is true.
3.1. do
r=n%10 (r=431%10) So r=1
ans=ans+r*r*r (ans=0+1*1*1) So ans=1
n=n/10 (n=431/10) So n=43
n>0 (43>0) do-while loop condition is true
3.2. do
r=n%10 (r=43%10) So r=3
ans=ans+r*r*r (ans=1+3*3*3) So ans=28
n=n/10 (n=43/10) So n=4
n>0 (4>0) do-while loop condition is true
3.3. do
r=n%10 (r=4%10) So r=4
ans=ans+r*r*r (ans=28+4*4*4) So ans=92
n=n/10 (n=4/10) So n=0
n>0 (0>0) do-while loop condition is false
3.4. It comes out of the do-while loop and checks whether the number is Armstrong or not.
- ans==num (92==431) if condition is true
It prints 431 is not an Armstrong number.
- Thus program execution is completed.