C Program to Check a Number Is Armstrong Number or Not



An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example,153  is an Armstrong number since 153=1*1*1+5*5*5+3*3*3






1. Using while and if-else:
#include<stdio.h>
main()  {
int num,sum=0,rem,arm;
printf("Enetr a number to check:");
scanf("%d",&num);
arm=num;
while(num!=0)
{     rem=num%10;
      sum=sum+(rem*rem*rem);
      num=num/10;
} if (arm==sum)
{     printf("Number is armstrong");
} else
{     printf("Number is not armstrong");
} }
Output:

2. Using Recursive Function:
#include<stdio.h>
main() {
  int armstrong(int num);
  int num,arm;
  printf("\n Enter a number: ");
  scanf("%d", &num);
 arm=armstrong(num);
 printf("\n %d is armstrong number",arm);
}  int armstrong(int num)
{
    int sum;
    if(num)  {
     sum=((num%10)*(num%10)*(num%10)) + armstrong(num/10)
 }  return sum;  }
Output:

C Program to Check a Number Is Armstrong Number or Not C Program to Check a Number Is Armstrong Number or Not Reviewed by vijay pratap singh on May 04, 2017 Rating: 5

5 comments:

  1. Oh for fuck's sake, that code is a pain to read without using a code tag !

    ReplyDelete
  2. Why is the §armstrong§ procedure referenced as the first thing done in the method 2's §main§ procedure ? I'm not good at C but I think that's a syntax error, or if it ain't it's unnecessary.

    ReplyDelete
    Replies
    1. it's for function declaration

      Delete
    2. main() {
      int armstrong(int num);
      int num,arm;

      You declare a function inside the main procedure ? I thought that's totally not done there.

      Delete

Powered by Blogger.