Check the given number is armstrong number or not using c program



Code 1:
1. Warp to check a number is Armstrong
2. C program to check whether a number is Armstrong or not
3. Simple c program for Armstrong number
4. Armstrong number in c with output

#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

Sample output:
Enter a number: 153
153 is an Armstrong number

The time complexity of a program that determines Armstrong number is: O (Number of digits)

Code 2:
1. Write a c program for Armstrong number
2. C program for Armstrong number generation
3. How to find Armstrong number in c
4. Code for Armstrong number in c

#include<stdio.h>
int main(){
    int num,r,sum,temp;
    int min,max;

    printf("Enter the minimum range: ");
    scanf("%d",&min);

    printf("Enter the maximum range: ");
    scanf("%d",&max);

    printf("Armstrong numbers in given range are: ");
    for(num=min;num<=max;num++){
         temp=num;
         sum = 0;

         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

Sample output:
Enter the minimum range: 1
Enter the maximum range: 200
Armstrong numbers in given range are: 1 153

Code 3:
1. Armstrong number in c using for loop

#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    for(temp=num;num!=0;num=num/10){
         r=num%10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d is an Armstrong number",temp);
    else
         printf("%d is not an Armstrong number",temp);

    return 0;
}

Sample output:
Enter a number: 370
370 is an Armstrong number
Logic of Armstrong number in c

Code 4:
1. C program to print Armstrong numbers from 1 to 500
2. C program for finding Armstrong numbers

#include<stdio.h>
int main(){
    int num,r,sum,temp;

    for(num=1;num<=500;num++){
         temp=num;
         sum = 0;

         while(temp!=0){
             r=temp%10;
             temp=temp/10;
             sum=sum+(r*r*r);
         }
         if(sum==num)
             printf("%d ",num);
    }

    return 0;
}

Output:
1 153 370 371 407


Definition of Armstrong number or what is an Armstrong number:
Definition according to c programming point of view:

Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153

Other Armstrong numbers: 370,371,407 etc.

In general definition:

Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.

Example 1: 153

Total digits in 153 is 3

And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725


18. Write a c program which passes structure to function.

50 comments:

Anonymous said...

this will get you only 3 digit armstrong nos..

Raj said...

lets work out this with example
take num = 153
while 153!=0 satisfies
r = 153%10 = 3
num = 153/10 = 15
sum=0+(3*3*3) = 27
now num = 15
15!=0
r=15%10=5
num=15/10=1
sum = 27+(5*5*5)=152
now num=1
1!=0
r=1%10=1
num=1/10=0
sum = 152+1=153
so now the condition sum==temp or num satisfies

Arpan Patel said...

I have written this code for any no. of digits. It works. I have used above code. Need Help. Help me reduce no of lines as well as no of variables. you can reply on arpann72@gmail.com

#include
void main()
{
int num,exp=1,rem,sum=0;
int i,temp,temp2,count=0,j;

scanf("%d",&num);

temp=num;
while(num/exp>0)
{
count=count+1;
exp*=10;
}

j=count;

while(j)
{
temp2=1;
rem=num%10;
num=num/10;
for(i=0;i<count;i++)
{
temp2*=rem;
}
sum+=temp2;
exp*=10;
j--;
}

if(sum==temp)
printf("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);

}

Arijit said...

Armstrong checking for any no
#include
int main(){
int num,r,s,sum=0,temp,c=0;
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
c++;
}
printf("\nno of digits:\n%d",c);
num=temp;
printf("\nno after assignment :\n%d",num);
while(num!=0){
s=num%10;
num=num/10;
sum=sum+power(s,c);
}
printf("\nsum is\n%d",sum);
if(sum==temp)
printf("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);
getch();
return 0;
}
int power(int a,int b){
int i=1,mul=1;
while(i<=b){
mul*=a;
i++;
}
return mul;
}

Anonymous said...

/*the below program holds good for any no. of digits*/
#include
#include
int main()
{
int m,t,i=1,s=0,n,r,j=1;
printf("enter the no: ");
scanf("%d", &n);
m=t=n;
while(j!=0)
{
r=n%10;
if(n<10)
{
break;
}
n=n/10;
j++;
}
printf("no of digits=%d\n",j);
while(i<=j)
{
r=m%10;
m=m/10;
s=s+pow(r,j);
i++;
}
printf("sum=%d\n",s);
if(s==t)
{
printf("valid");
}
else
{
printf("invalid");
}
getch();
return 0;
}

Anonymous said...

in the above program directives are stdio.h and math.h

Anonymous said...

how it works for 407......

Unknown said...

merging 2 1-d arrays into 3rd array

saumya pandey said...

how to find armstrong numbers between 1 and 1000 using only while?

siya rose said...

ethilum bedam njana

Anonymous said...

nice explanation yaar...thanx alot

Anonymous said...

Guys can u help me out knowing why "temp" is used in this program???

ramswaroop said...

you can visit www.codecast.org for more codes,can register and upload there

I&mybackpack said...

There is a rail road to different towns from one town. It is considered that the rail road is a one way because of budgetary issues. If there is city A and it has cities B and C cities connected by rail roads, it is not mandatory that A has direct rail road to both B and C. The route to C from A can be A to B and then B to C. The above scenario is depicted as a graph. The nodes are the towns and edges are the distances between them. The input given will be a graph and also the route to towns. The output must be the total rail road distance between the towns and if no route exists, it must say 'No Route exists'.

PLEASE SEND ME A C PROGRAMME FOR THIS QUESTION. PLEASE ITS URGENT.

Vakum said...

There is a mistake in the explanation of the Armstrong section: its 3^3=27 not 9 (the first part of the explanation)

Vakum said...

A correction on this part. Its seems that if the Armstrong num. is anything other than three, then this wont work (except for 1). I think the number of r's has to be adjusted to how many digits the natural number inputted has. the way to do this is to create a loop to see how many times is the number entered divisible by 10 at the beginning of the program, and then multiply by that many r's everytime. Please notify me if I am wrong. Thanks

BODEPUDI SRINIVASARAO said...

good , but small mist.
go to Definition. 1^3 + 5^3 + 3^3 = 1+ 125 + 27 =153
thats it.

Anonymous said...

4*4*4=64
7*7*7=343
343+64=407

Unknown said...
This comment has been removed by the author.
Unknown said...

int i=1,temp,s;
while(i<=1000)
{
temp=i;
s=0
i++;
while(temp!=0)
{
r=temp%10;
temp=temp/10;
s=s+temp;
}
if(s==i)
printf("%d",i);
}
return 0;
}

Unknown said...

because by using temp or any another variable we can get the single value.
for example:
153
step 1 step 2 step 3
153%10=3 15%10=5 1%10=1
153/10=15 15/10=1 1/10=0
0+(3*3*3)=27 27+(5*5*5)=152 152+(1*1*1)=153

Anonymous said...

yahh!! 1-9 all r armstrong but it won't show it

Mani Sai Gundumogula said...

i have searched in net that there are only 4 three digited amstrong nos
153,370,371,407
but if 153 is amstrong then 351 and 531 are also amstrong numbers
if u find answer for this plz mail@mani sai
@mani4sai@gmail.com

Unknown said...

how can 531 be an amstrong no,
3^3+5^3+1^3=27+125+1=153 not 351 so 351 cant be an amstrong no.An amstrong no is sum of cubes of the number is the no itself.
same goes with 531.

pawan said...

thanks

Mohammed Shafeeque said...

is it for amstrong numbers??? or for perfect numbers...??

Unknown said...

above all code implemented by admin is wrong

Anonymous said...

#include
#include
int main(void)
{
int sum,term,policytype;
float bonus;
printf("enter the sum \n");
scanf("%d",&sum);
printf("enter 1 for Endownment \n 2 for Money Bank \n 3 for Term Assurance \n");
scanf("%d",&policytype);
switch(policytype)
{
case 1: printf("enter the term: \n");
scanf("%d",&term);
if(term>=25)
{
bonus=((float)sum/1000)*60;
printf("the bonus rate per thousand is %f \n",bonus);
}
else if((term>=20)&&(term<25))
{
bonus=((float)sum/1000)*55;
printf("the bonus rate per thousand is %f \n",bonus);
}
else if((term>=10)&&(term<20))
{
bonus=((float)sum/1000)*50;
printf("the bonus rate per thousand is %f \n",bonus);
}
else if(term<10)
{
bonus=((float)sum/1000)*45;
printf("the bonus rate per thousand is %f \n",bonus);
}
break;
case 2:printf("enter the term: \n");
scanf("%d",&term);
if(term>=25)
{
bonus=((float)sum/1000)*55;
printf("the bonus rate per thousand is %f \n",bonus);
}
else if((term>=20)&&(term<25))
{
bonus=((float)sum/1000)*50;
printf("the bonus rate per thousand is %f \n",bonus);
}
else if((term>=10)&&(term<20))
{
bonus=((float)sum/1000)*45;
printf("the bonus rate per thousand is %f \n",bonus);
}
break;
case 3:printf("the bonus is not applicable for this policy type \n");
break;
default: printf("please enter valid policy type \n");
break;
}
getch();
return 0;
}

Anonymous said...

/* */

#include //directive for input output
#include //directive for displaying output
#include

int compound_interest(float p,float t, float r,float n);
void main()
{
float comp_intrest,principal_amt,rate,time,number_interest;
float result;

printf("enter the amount, time, rate and number\n");
scanf("%f%f%f%f",&principal_amt,&time,&rate,&number_interest);
result = compound_interest(principal_amt,time,rate,number_interest);
printf("the compound intreset is %f\n",result);
getch();

}
int compound_interest(float p,float t, float r,float n)
{
float amount, ci,temp=1,nt;
int i;
nt=n*t;
for(i=0;i<nt;i++)
{
temp=temp*(1+r/n);
}
amount=p*temp;
ci=amount-p;
return ci;
}

Anonymous said...

nice explnation

Anonymous said...

dont know why m nt gtng ans..plz any1 can notify me wher m going wrng
result iz blank..?????

#include
#include
int main()
{
int n,sum=0,a,i;
printf("armstrng no are \n\n");
for(i=1;i<=1000;i++)
{
n=i;
sum=0;
while(n>0)
{
a=n%10;
n=n/10;
sum=sum+(a*a*a);
}
if(n==sum)
{
printf("%d",n);
}
}
getch();
return 0;
}

Mohammed Shafeeque said...

change condition of if, if(i==sum) not n==sum, bcoz at this point n 'll be zero...

Unknown said...

Maybe not the best but works for 3 digit armstrong number :D
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
main()
{
int a,b,c,whole,choice,n=0,count=0,onedigit=0,twodigit=0,threedigit=0;
printf("Enter the number you want to test :");
scanf("%d",&whole);
n=whole;
while(n!=0)
{
n/=10;
++count;
}

printf("The number of digits entered are %d\n",count);

if(count==1)
{
onedigit=(whole*whole*whole);

if(onedigit==whole)
printf("This is an armstrong number");
else
printf("Not an armstrong number");
}

else if(count==2)
{
a=whole%10;
b=whole/10;
twodigit=(a*a*a)+(b*b*b);
if(twodigit==whole)
printf("This is an armstrong number");
else
printf("Not an armstrong number");
}

else if (count==3)
{
a=whole%10;
b=(whole/10)%10;
c=whole/100;
threedigit=(a*a*a)+(b*b*b)+(c*c*c);
if(threedigit==whole)
printf("This is an armstrong number");
else
printf("Not an armstrong number");
}
else
printf("Not a valid input");

getche();
}

Unknown said...

mis print of 3^3 = 9

vijay krishna said...

the above mentioned codes wont work for 4 digit Amstrong numbers so those are wrong codes, please review my code and it will work for all Amstrong numbers:

#include
#include
#include
#include
void main()
{
char num[8];int len,temp,rem,sum=0,temp1;
printf("enter number:");
gets(num);
len=strlen(num);
temp=atoi(num);
temp1=temp;
while(temp>0)
{
rem=temp%10;
sum=sum+pow(rem,len);
temp=temp/10;
}
if(sum==temp1)
{printf("%d is an Amstrong number",temp1);}
else
{printf("%d is not an Amstrong number",temp1);}
}

send email to me at chvijay.1990@gmail.com regarding the review of this code....!

Unknown said...

thank u sooooo much

SarmaKarthik said...

armstrong number program
#include
#include
void main()
{
int sum=0, num,temp,n=0;
printf("Enter Number");
scanf("%d",&num);
temp=num;
while(temp>0)
{
n++;
temp/=10;
}
temp=num;
while(temp>0)
{
sum+=pow(temp%10,n);
temp/=10;
}
if(sum==num)
{
printf("%d is an armstrong Number",num);
}
else
{
printf("%d is Not an Armstrong Number",num);
}
}

for Armstrong number program in java visit:
http://programmerminds.blogspot.in/2013/11/armstrong-number-program-in-java.html

Unknown said...
This comment has been removed by the author.
Guru said...

Thank you very much...

Unknown said...

//This will work for any number.
#include
int main ()
{
int i=0,y,t,sum=0,m,x,num;
scanf("%d",&num);
m=num;
while(m!=0)
{
m=m/10;
i++;
}
while(num!=0)
{
x=num%10;
y=1;
t=i;
while(t!=0)
{
y=y*x;
t--;
}
sum =sum + y;
num = num/10;
}
printf("%d",sum);

}

Unknown said...

Why is temp used?

rdx said...

You r worng because Armstrong number are those numbers which power is 3 and get same value like 1^3 =1, 153= 1×1×1+5×5×5+3×3×3

prasanna said...

Given a string of word with the combination of words number and symbol.write a program to read the number in the string and add it print like below.Exampleinput : ci2t2r1is@y$%soutput citrisys5@$%

prasanna said...

write a program for given logic belowstep 1: find the unitdigit of the number Example 34246 is 6(unit digit)Step 2: (3*2 + 4*2 + 2*2 + 4*2)/4 = 6step 3: check unit dight and above condition equals if TRUE print SUCCESS elseFAIL

prasanna said...

check and remove duplicate in Array(very Easy ANS put the value in Hashset collection)Given array ={1,2,3,4,1,2,3,5,1,2}ouput={1,2,3,4,5}

prasanna said...

given a condition 2^n+1 taake some value of n like n=100check number beween the condion is prime or notfor example2^0+1=2 is prime2^1+1=3 is prime2^2+1=5 is prime2^3+1=9 is not prime

prasanna said...

if anybody knows answer my questions please send me....

Unknown said...

how to find given number is armstrong num or not using strings]

Unknown said...

An armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits. Write a program that accepts an integer and determines if it is an armstrong number or not.

Example:

153 is a 3- digit number so each digit should be raised to 3 and add the results.

1 3 ++ 5 3 + 33 = 1 + 125 + 27 = 153

153 = 153

Therefore, 153 is an armstrong number.

25 is a 2-digit number so each digit should be raised to 2 and add the results.

22 + 52 = 4 + 25 = 29

25 is not equal to 29

Therefore, 25 is not an armstrong number.

Requirement: do not use any math function (math.h)E.g. pow( ), sqrt() etc.

Create a loop that:

1.)counts the number of digits in an n-digit number (for determining exponent)

2.) loop that extracts individual digit

3.) loop that computes the power of x raise to n.


please reply to me in an e-mail: hannahdelacruz009@gmail.com. Thank you

Unknown said...

sir your programming using for loop of Armstrong no is not running in blue j