Showing posts with label Armstrong No. Show all posts
Showing posts with label Armstrong No. Show all posts

Wednesday, July 20, 2011

Program to print Armstrong no's

/*Serial No.71     [swami44.cpp]*/ 

/*You can change the range up to which you want to check for Armstrong no's, but it will increase processing time.*/
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int number, temp, digit1, digit2, digit3;
printf("Printing all Armstrong numbers between 1 and 500:\n\n");
number=1;
while (number <= 500)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);
if (temp == number)
{
printf("\nAmstrong Number:%d", temp);
}
number++;
}
getch();
}


OUTPUT :
Printing all Armstrong numbers between 1 and 500:
Amstrong Number:1
Amstrong Number:153
Amstrong Number:370
Amstrong Number:371
Amstrong Number:407

Program to check armstrong no. of any digit {Version 2}

/*Serial No.70     [swami43.cpp]*/ 

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int i,num,m,sum,dig,p=0,n,x,z;
clrscr();
sum=0;
printf("Enter the number=");
scanf("%ld",&n);
num=n;
z=n;
for(i=1;i<=10;i++)
{
x=z/10;
z=x;
p=p+1;
if(z==0)
break;
}
for(i=1;i<=p;i++)
{dig=n%10;
n=n/10;
sum=pow(dig,p)+sum;
 }
if(num==sum)
printf("\nArmstrong no.");
else
printf("\n Not Armstrong no.");
getch();
}

Program to check armstrong no. of any digit {Version 1}

/*Serial No.69     [swami42.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,num,m,sum,dig,p,n;
clrscr();
sum=0;
printf("Enter the number=");
scanf("%d",&n);
num=n;
printf("Enter the no. of digits in the given no.=");
scanf("%d",&p);
for(i=1;i<=p;i++)
{dig=n%10;
n=n/10;
sum=pow(dig,p)+sum;
 }
if(num==sum)
printf("\nArmstrong no.");
else
printf("\n Not Armstrong no.");
getch();
}