Search C Program | nimishsoft@gmail.com

Armstrong Numbers upto N

/* Print Armstrong Numbers upto N */

#include<stdio.h>

int main()
{
    int i,j,sum,n;

    printf("Please enter the value of N: ");
    scanf("%d",&n);
   
    for(i=2;i<=500;i++)
    {
        for(j=i,sum=n;j>=1;j=j/10)
            sum=sum+(j%10)*(j%10)*(j%10);
        if(sum==i)
            printf("%d is Armstrong.\n",i);
    }
    return 0;
}

Related Links:
Check Armstrong Number 
http://cbasicprogram.blogspot.in/2012/05/check-armstrong-number.html

7 comments:

  1. why for(i=2;i<=500;i++) is used?

    ReplyDelete
  2. its not working and not getting the correct output

    ReplyDelete
    Replies
    1. if you want to right answer then changed this code sum=0 instead of sum=n, and n instead of 500.

      Delete
  3. #include
    #include
    int casg(int);
    int main()
    {
    int j,i;

    for(i=10;i<10000;i++)
    {
    j=casg(i);
    if (j==1)
    printf ("%d\n",i);
    }
    return 0;
    }
    int casg(int a)
    {
    int q,w,e=0,r,t,y,u,s=0,o;
    r=a;
    t=a;

    while(a>0)
    {
    q=a%10;
    a=a/10;
    e++;
    }
    while(r>0)
    {
    y=r%10;
    r=r/10;
    s=s+pow(y,e);
    }
    if(s==t)
    return 1;
    else
    return 0;

    }

    ReplyDelete