Armstrong number program in C

In this example, you will learn whether the input enter by the user is Armstrong number or not in the C program.

what is armstrong number?

Armstrong number is a number that is equal to the sum of cubes of its digits.

For example:

153, 1634 is amrstrong number. because 13+53+33=153

153=1*1*1+5*5*5+3*3*3 for 3 digits

14+64+34+44=1634 for four digit

Armstrong number of three digits in C

#include<stdio.h>
int main()
{
  int sum=0,copy,n,r;
  printf("\nenter the number:");
  scanf("%d",&n);
  copy=n;
  while(copy>0)
  {
   r=copy%10;
   sum=sum+(r*r*r);
   copy=copy/10;
  }
  if(sum==n)
   printf("\n%d is armstrong number",n);
  else
   printf("\n%d is not a armstrong number",n);
  return 0;
}

output

enter the number:153
153 is armstrong number

Armstrong number of n digits in c program

#include<stdio.h>
#include<math.h>
void main()
{
  int i=0,sum=0,copy,n,r;
  printf("\nenter the number:");
  scanf("%d",&n);
  copy=n;
  /*it will calculate number of digit */
  while(copy>0)
  {
    copy=copy/10;
    i+=1;
  }
  copy=n;
  /*store the sum of power of individual digit in sum variable */
  while(copy>0)
  {
    r=copy%10;
    sum+=pow(r,i);
    copy=copy/10;
  }
  if(sum==n)
    printf("\n%d is armstrong number",n);
  else
    printf("\n%d is not a armstrong number",n);
}

output

enter the number:1634
1634 is armstrong number

explanation of Even or Odd program

  • Take a number from the user and stored the number in a variable n.
  • Copy the value of n into the copy variable for later comparison.
  • Calculate the number of digits of variable n and store the number of digits in i variable.
  • Now again copy the value of n into the copy variable because during calculation number of digit copy value becomes zero.
  • Inside the while loop, find the sum of the power of all individual digits and stored it in a variable sum.
  •  Now check whether the user value i.e n is equal to the sum or not.
  • if the sum is equal to n then if statement will get executed and it will print “It is an Armstrong number”.
  • if it's not equal else part gets executed and prints “It is not an Armstrong number”.