Thursday, September 20, 2012

program for amstrong numbers upto a limit


/* AMSTRONG NUMBERS UP TO A LIMIT*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,count=0;
clrscr();
printf("Enter the  limit\n");
scanf("%d",&n);
while(i<=n)
{
int s=0,r=0,k=i;
 while(k!=0)
 {
 r=k%10;
 k=k/10;
 s=s+(r*r*r);
 }
 if(i==s)
 {
 count++;
 printf("\n%d",i);
 }
 i++;
 }
 printf("\nTotal count\t%d",count);
 getch();
 }

An Armstrong number is an n digit number, which is equal to the sum of the nth powers of its digits.
All the 1 digit numbers (1-9) are Armstrong number because
  • 1*1=1 which is equals to number (1) itself,
  • 2*1=2 which is equals to number(2) itself so on for all the 1 digit numbers (1-9).
There are no 2 digit Armstrong numbers.
An Armstrong number of three digit is a number such that that sum of the cubes of it's digits is equal to the number itself. so to check if a 3 digit number is an armstrong number or not we have to multiply all the three digits with itself 3 times.
For example 153 is an Armstrong number because cube of 1 is 1(1x1x1=1) + cube of 5 is 125(5*5*5=125) + cube of 3 is 27(3*3*3=27). Now add all the cubes 1+125+27=153 which is equals to number itself

 

Example 1: Check if 153 is an armstrong number or not

There are three digits in 153. First Digit is 1, Second digit is 5, and Third digit is 3.
Now,
We will multiply each of these three digits i.e 1,5,3 with itself 3 times because there are 3 digits in 153
So,
1 * 1 * 1 =   1

5 * 5 * 5 = 125

3 * 3 * 3 =  27
___________________
   Sum of=  153
Because the sum is equals to the digit itself, so we can say that 153 is an armstrong number.

No comments:

Post a Comment