Example for an Armstrong number:153=13+53+33
Algorithm
Step 1:Start
Step 2:Declare variables i,r,n,sum=0,temp.
Step 3:Input the limit up to which Armstrong number has to be generated.
Step 4:Read the limit,n.
Step 5:Initialize i as 1.
Step 6:while(i<n) repeat steps 6.1 to 6.7
6.1)Assign temp=i.
6.2)Initialize sum as 0.
6.3)while(temp!=0) repeat steps:a to c
a)Find r=temp%10
b)Find sum=sum+(r*r*r)
c)Assign temp=temp/10
6.4)End while.
6.5)if(i is equal to sum) then
a)Print the number,i
6.6)End if.
6.7)Increment i by 1.
Step 7:End while.
Step 8:Stop.
Program
#include<stdio.h>
#include<conio.h>
void
main()
{
int i,r,n,sum=0,temp;
clrscr();
printf("Enter the limit :");
scanf("%d",&n);
for(i=1;i<n;i++)
{
temp=i;
sum=0;
while(temp!=0)
{
r=temp%10;
sum=sum+(r*r*r);
temp=temp/10;
}
if(i==sum)
{
printf("\n%d",i);
}
getch();
}
Output
Enter the limit :500
1
153
370
371
407
No comments:
Post a Comment