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.

30 comments:

  1. this will get you only 3 digit armstrong nos..

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

      Delete
  2. 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

    ReplyDelete
    Replies
    1. nice explanation yaar...thanx alot

      Delete
  3. 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);

    }

    ReplyDelete
  4. 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;
    }

    ReplyDelete
  5. /*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;
    }

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

    ReplyDelete
  7. how it works for 407......

    ReplyDelete
    Replies
    1. 4*4*4=64
      7*7*7=343
      343+64=407

      Delete
  8. merging 2 1-d arrays into 3rd array

    ReplyDelete
  9. saumya pandey12/19/11, 6:21 PM

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

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. 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;
      }

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

      Delete
  10. ethilum bedam njana

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

    ReplyDelete
    Replies
    1. 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

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

    ReplyDelete
  13. 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.

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

    ReplyDelete
  15. 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

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

    ReplyDelete
  17. 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

    ReplyDelete
  18. 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.

    ReplyDelete
  19. above all code implemented by admin is wrong

    ReplyDelete
  20. #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;
    }

    ReplyDelete
  21. /* */

    #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;
    }

    ReplyDelete
  22. nice explnation

    ReplyDelete