Tuesday, 10 February 2015

Check whether the number is amstrong number or not.

A positive integer is called an Armstrong number if the sum of cubes of individual digit is equal to that number itself.
For example:
153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.
12 is not equal to 1*1*1+2*2*2  // 12 is not an Armstrong number.

Source Code to Check Armstrong Number

/* C program to check whether a number entered by user is Armstrong or not. */

#include <stdio.h>
#include <conio.h>
 
void main()
{
  int n, n1, rem, num=0;
  clrscr();
  printf("Enter a positive  integer: ");
  scanf("%d", &n);
  n1=n;
  while(n1!=0)
  {
      rem=n1%10;
      num+=rem*rem*rem;
      n1/=10;
  }
  if(num==n)
    printf("%d is an Armstrong number.",n);
  else
    printf("%d is not an Armstrong number.",n);
}
 
Output

Enter a positive integer: 371 
371 is an Armstrong number.
 
https://www.facebook.com/buildprogramminglogic?ref=bookmarks

No comments:

Post a Comment