Saturday, 2 August 2014

Armstrong Number

Armstrong Number

Armstrong number is a number that is the sum of its own digits each raised tothe power of the number of digits is equal to the number itself.
For example:
Three Digits armstrong number is 153, 1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
Four Digits armstrong number is 1634, 1 ^ 4 + 6 ^ 4 + 3 ^ 4 + 4 ^ 4 + = 1634

3 Digits Armstrong number

#include< stdio.h>
#include< conio.h>
void main()
{
int arm=0,a,b,c,d,no; 
clrscr();
printf("Enter any num = ");
scanf("%d",&no);
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
printf("\n%d armstrom",no);
}
else
{
printf("not armstrom");
}
getch();
}
Output
Enter any number = 23
not armstrom

Any number of Digits Armstrong number

#include< stdio.h>
#include< conio.h>
void main()
{
     int num;
     int f,rem,sum=0,temp,a=0;
     clrscr();
     printf("\n Enter any number ");
     scanf("%d",&num);
     temp=num;
     while(temp != 0)
     {
      temp=temp/10;
      a=a+1;
     }
     f=num;
     while(f!=0)
     {
  rem=f%10;
  sum = sum + power(rem,a);
  f=f/10;
     }

     if( sum == num )
     printf("\n %d is a armstronumg number ",num);
     else
     printf("\n %d is not a armstronumg number",num);
     getch();
}

int power(int c, int d)
{
     int pow=1;
     int i=1;
     while(i<=d)
     {
          pow=pow*c;
          i++;
     }
     return pow;
}
Output
Enter any number = 153
153 is a armstronumg number
Explanation of Program
temp=num;
 while(temp != 0)
  {
  temp=temp/10;
  a=a+1;
  }

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...