Armstrong number in c program with source code | comments | execution line by line
In this article i have shown how to Write program for Armstrong number in c using function with while loop.
Basically we can write simple program for Armstrong number but by using function if we are
writing then its scope will be different.
first of all get all the idea about what is Armstrong number .
its definition then only one programmer can develop his logic.
Armstrong number is number which is equal to sum its individual number cube.
cube means multiplication of same number thrice or three times.
suppose i want to check number 153 is Armstrong or not, then answer is yes .
because , 153=(1*1*1) + (5*5*5) + (3*3*3) =1+125 + 27 =126 + 27=153.
in this line 1 and 5 and 3 are 153 's individual number .and sum of cube of all three
numbers that number.
so , i think you got the little idea about Armstrong number.
so here we are going to write Armstrong number program in c language.
#include<stdio.h> // including header files
#include<conio.h>
void armstrong(int n); // calling function armstrong(int n)
void main()
{
int n; //initializing variable n
clrscr();
printf("enter the number");
scanf("%d",&n); //153
arm(n); // now n=153
getch();
}
void arm (int n)
{
int r,sum=0,no;
no=n; //no=153
while(n>0)
{
r=n%10; //taking mod (153%10)=15 and r=3 and n=15
sum=(r*r*r)+sum; //3*3*3 + sum=27+0=27 so sum=27
n=n/10; // now n=15
// after first execution
r=n%10; //taking mod (15%10)=1 and mod =5 and n=10
sum=(r*r*r)+sum; //5*5*5 + sum=125+27=152
n=n/10; // now n=10
// after second execution
r=n%10; //taking mod (10%10)=0 and mod =1 and n=0
sum=(r*r*r)+sum; //1*1*1 + sum=152+1=153
n=n/10; // after last execution
}
if(no==sum) // now sum=153 and no=153 both are same hence 153 is armstrong number
printf("given number is armstrrong");
else
printf("not armstrong");
}
Armstrong number program is very simple program just we should know about c logic and loop
syntax and concept about program.
0 comments: