Monday, January 11, 2016

To check given number is Armstrong or not.

Write a program and algorithm to check given number is Armstrong or not.
Step (1):- Start
Step (2):- Declare the variable n, r, arm = 0, temp
Step (3):- Enter the value of n
Step (4):- temp ← n

Step (5):- while (n > 0)
(5.1) r=n%10
(5.2) arm = arm + r * r* r
(5.3) n = (n/10)
(5.4) end while loop

Step (6):- if (temp = = arm)
(6.1)Given no is armstrong

Step (7):- else
(7.1)Given no is not armstrong

Step (8):- Stop.

Program:-

# include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, r, arm=0, temp;
printf("Enter the value of n");
scanf("%d",&n);
temp=n;
while (n>0)
{
r=n%10;
arm=arm+r*r*r;
n=n/10;
}
if(temp==arm)
{
printf("Given no is armstrong");
}
else
{
printf("not armstrong");
}
getch();
}
-------------------------------------------------------------------------

Output:-

Enter the value of n
153
Given no is armstrong

0 Comments:

Post a Comment