-->

Header Ads

WAP to check whether given number is Armstrong number or not?

What is Armstrong number?
An Armstrong number is if the sum of cubes of individual digits is equal to the number itself called Armstrong number. E.g.
 371 is an Armstrong number as
 33 + 73 + 13 = 371.
Some other examples are:   0, 1, 153, 370, and 407.
C program:
#include<stdio.h>
#include<conio.h>
void main()
{
  clrscr();
  int number,temp,sum=0, remainder;
  char ch;
  printf("Enter number to check whether number is Armstrong or not??\n");
  scanf("%d",&number);
  temp=number;
  while(temp!=0)
   {
    remainder=temp%10;
    sum=sum+remainder*remainder*remainder;
    temp=temp/10;
   }
  if(number==sum)
    printf("number is Armstrong");
    else
    printf("number is not Armstrong");
   getch();
  }
Output of c program:



See another way to check Armstrong number:
Here I am using GOTO and POW () function to make more useful program.

C Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  clrscr();
  L1:// goto label
   int number,temp,sum=0, remainder;
   char ch;
  printf("Enter number to check whether number is Armstrong or not??\n");
  scanf("%d",&number);
  temp=number;
  while(temp!=0)
   {
     remainder=temp%10;
    // sum=sum+remainder*remainder*remainder;
     sum=sum+pow(remainder,3); // we can also use with ” math.h” header file
     temp=temp/10;
   }
  if(number==sum)
    printf("number is Armstrong\n");
    else
   printf("number is not Armstrong\n");

printf("do u want to check another number: press Y/N:");
fflush(stdin);
scanf("%c",&ch);
if(ch=='y'|| ch==’Y’)
    goto L1;
getch();
}
Output of c program:



No comments

Powered by Blogger.