Problem Statement
Write a C program to check whether a given number of 3 digits is an Armstrong number or not. 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. For example,
153 = 13+53+33
#include<stdio.h>
int armstrong(int n);
int main()
{
int y,n;
printf("enter the number \n");
scanf("%d",&n);
y=armstrong(n);
if(y==1)
{
printf("this is a armstrong number \n");
}
else
{
printf("not a armstrong number \n");
}
return 0;
}
int armstrong(int n)
{
int tempcub,temp,tempsum,i,tempnum;
tempsum=0;
tempnum=n;
while(n%10>=1)
{
temp=n%10;
tempcub=1;
for(i=0;i<3;i++)
{
tempcub=tempcub*temp;
}
tempsum=tempsum+tempcub;
n=n/10;
}
if(tempsum==tempnum)
{
return 1;
}
return 0;
}
Write a C program to check whether a given number of 3 digits is an Armstrong number or not. 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. For example,
153 = 13+53+33
#include<stdio.h>
int armstrong(int n);
int main()
{
int y,n;
printf("enter the number \n");
scanf("%d",&n);
y=armstrong(n);
if(y==1)
{
printf("this is a armstrong number \n");
}
else
{
printf("not a armstrong number \n");
}
return 0;
}
int armstrong(int n)
{
int tempcub,temp,tempsum,i,tempnum;
tempsum=0;
tempnum=n;
while(n%10>=1)
{
temp=n%10;
tempcub=1;
for(i=0;i<3;i++)
{
tempcub=tempcub*temp;
}
tempsum=tempsum+tempcub;
n=n/10;
}
if(tempsum==tempnum)
{
return 1;
}
return 0;
}
No comments:
Post a Comment