<>

Armstrong number C Program


C Program which tell input number is armstrong or not:

#include <stdio.h>
main()
{
   int n=0,s=0,t=0,r=0;

   printf("Enter an integer: ");
   scanf("%d",&n);

   t=n;

   while( t != 0 )
   {
      r = t%10;
      s = s + r*r*r;
      t = t/10;
   }

   if ( n == s )
      printf("Entered number is an armstrong number.\n");
   else
      printf("Entered number is not an armstrong number.\n");
}


Also by Making Function of it:


#include <stdio.h>
void armstrong(int);
main()
{
    int n=0;

    printf("Enter an integer: ");
    scanf("%d",&n);

    armstrong(n);
}
void armstrong(int n)
{
    int s=0,t=0,r=0;
    
    t=n;

    while( t != 0 )
    {
        r = t%10;
        s = s + r*r*r;
        t = t/10;
    }

    if ( n == s )
        printf("Entered number is an armstrong number.\n");
    else
        printf("Entered number is not an armstrong number.\n");
}


C Program which prints all the armstrong number according to given range:


#include <stdio.h>
void armstrong(int);
main()
{
    int m=0,n=0;

    printf("Enter an integer: ");
    scanf("%d",&m);
    printf("Enter an integer: ");
    scanf("%d",&n);

    printf("All Armstrong number between these range are: \n\n");

    while(m<n)
    {
        armstrong(m);
        m++;
    }
}

void armstrong(int n)
{
    int s=0,t=0,r=0;

    t=n;

    while( t != 0 )
    {
        r = t%10;
        s = s + r*r*r;
        t = t/10;
    }

    if ( n == s )
        printf("%d\n",n);
}

Rate this posting:
{[['']]}

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
TOP