Armstrong number in C

armstrong number

C program to check number is armstrong or not

A number is called Armstrong number when the sum of cubes of its digits of a number is equal to the number itself. Here in the below C program, we are checking whether the input number which is taken by the user is Armstrong or not.
We take an example, and try to check whether an integer entered by the user is an Armstrong number or not.

Problem Statement:

An Armstrong number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself. Hence 370 is armstrong number because
370 = 3*3*3 + 7*7*7 + 0*0*0
= 27 + 343 + 0
= 370

So, move forward to our source code:

Example #1: Check Armstrong Number

#include<stdio.h>
#include<conio.h>
  
void main()  
{    
int num, rem, sum=0,temp;
clrscr();
printf("\n\t\t\t...Welcome To EduNews.Tech... ");
printf("\n\nEnter the number : ");
scanf("%d",&num);
temp=num;
while(num>0)  
   {
    rem=num%10;
    sum=sum+(rem*rem*rem);
    num=num/10;
   }
 if(temp==sum)
 printf("The given no is armstrong no.");
  else
 printf("The given no is not a armstrong no ");
 printf("\n\n\n\t\t\tThankyou for Joining Us !");
 printf("\n\t\t\t!Regards EduNews !");
 getch();
}

Program Output:

armstrong output

Example #2: Check Armstrong Number of n digits

#include<stdio.h>
#include<conio.h>

void main()
{
  int num, rem, sum=0,temp;
  printf(“…Welcome To EduNews.Tech…”);
  printf("\n\nenter the number=");
  scanf("%d",&num);
  temp=num;
while(num>0)
  {
   rem=num%10;
   sum=sum+(rem*rem*rem);
   num=num/10;
  }
 if(temp==sum)
  printf("The given no is armstrong no.");
 else
  printf("The given no is not a armstrong no ");
  printf("\n\n\n\t\t\tThankyou for Joining Us !");
  printf("\n\t\t\t!Regards EduNews !");

getch();
}

Program Output:

armstrong output-2

Program Explanation

Using the while loop we check the value of the ‘number’ variable is not equal to 0. If the condition is true, execute the iteration of the loop. The ‘rem’ variable is used to compute the modulus of the value of the ‘number’ variable by 10 and the ‘cube’ variable is used to compute the cube of the value of ‘rem’ variable using pow().
Then we used the ‘sum’ variable to compute the summation of the value of the ‘sum’ variable with the value of the ‘cube’ variable. The If-else condition statement is used to check both the value of the ‘sum’ variable and the value of the ‘temp’ variable are equal. If the condition is true, then it will print The given no is armstrong no. Otherwise, it will execute the else condition statement and print The given no is not a armstrong no.

 

I hope this post helps you to understand Armstrong number and its implementation in C programming language.

Keep coding 🙂

4 thoughts on “Armstrong number in C”

Leave a Reply

Your email address will not be published. Required fields are marked *