[wpseo_breadcrumb]
Program 5: Check For Armstrong Number
Intro Text
Our problem statement is to check for Armstrong number. To solve this problem we need to understand what Armstrong number really means?.
Armstrong Number
An Armstrong numbers are also known as Narcissistic Number G.H.Hardy wrote, “There are just four numbers, after unity, which are the sums of the cubes of their digits: 153=1^3+5^3+3^3 and 407=4^3+0^3+7^3 . These are odd facts, very suitable for puzzle columns and likely to amuse amateurs, but there is nothing in them which appeals to the mathematician.” . I hope you understand what Armstrong is. Now Lets move to our next phase of our problem.
Algorithm
lets design our algorithm by testing one known armstrong number. let us assume 153.
let x=153 first round: 153/10 remainder = 3 | quotient = 15 a = 3*3*3 (i.e) remainder^3 15/10 remainder = 5 | quotient = 1 b = 5*5*5 1/10 remainder = 1 c = 1*1*1 final answer = a+ b + c = 153 so this is a Armstrong number
So we optimize the above algorithm to effectively check for Armstrong number
the algorithm will be like
int temp,r,result=0; temp=Input; while(Input>=1){ r=Input%10; Input=Input/10; result+=r*r*r; }
Program:
So This is the program which uses the above algorithm.
/* Experiment:Check for Armstrong number */ #include<stdio.h> #include<conio.h> void isArmstrong(int); void main(){ int num; clrscr(); printf("\nCHECK FOR ARMSTRONG NUMBER"); printf("\n**************************"); printf("\nEnter any number..."); scanf("%d",&num); isArmstrong(num); getch(); } void isArmstrong(int num){ int temp,r,result=0; temp=num; while(num>=1){ r=num%10; num=num/10; result+=r*r*r; } if(result==temp){ printf("\n%d is an armstrong number.",temp); } else{ printf("\n%d is not an armstrong number.",temp); } }