Brijmohan lal Sahu - Facebook
Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, December 10, 2014

Program to find Armstrong number with power function in C.


Armstrong number in C.


Code Box
---------------------------------------------

/*Program to find Armstrong number with power function*/

/*Armstrong number means sum of nth power of each digits of a given number.
Example: 123 is a given number than 
Total number of digits= 3
so for checking 123 is Armstrong or not we need to do
sum=Cube(1)+Cube(2)+Cube(3)
and if sum==123 than it is an Armstrong number.

*/

/*We need to find:
Step1:Finding Length of number
Step2:Each digits 
Step3:power digits with length
Step4:calculate sum
Step5:Checking sum==number.

*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int p,num,num1,len,i,sum;
clrscr();

len=0;
sum=0;

printf("\n\tArmstrong Number Checker");
printf("\n-------------------------");
printf("\nEnter a number:");
scanf("%d",&num);

num1=num;

/*Step1:Finding length of number*/


while(num1>=1)
{
i=num1%10;  //Extraction of digits
num1=num1/10; //Reduction of number
len++;
}

printf("\nNumber of digits in number : %d",len);

/*Calculation of Armstrong number */

num1=num;

printf("\nAt Initial sum=%d",sum);
printf("\n------------------------");
while(num1>=1)
{
i=num1%10; //Step2
printf("\nFor digit=%d",i);
num1=num1/10;
printf("\nNumber =%d",num1);
p=pow(i,len); //Step3
sum=sum+p;  //Step4
printf("\nSum=%d",sum);

printf("\n----------------------\n");
}


printf("\nTotal sum: %d",sum);
/*Step 5 */
if(sum==num)
{
printf("\nNumber %d is an Armstrong number ",num);
}
else
{
printf("\nNumber %d is not an Armstrong number ",num);
}
getch();
}




No comments: