Written by Sai Sandeep Thota on January 28, 2014 in C > Programming

C Program to check Armstrong Number


Get our Latest updates delivered to your mailbox!

An Armstrong number is a number in which the sum of cubes of individual digits is equal to the Given number. Today we are going to learn how to write a C Program to check Armstrong Number.

In this program we are using 4 variables n, temp, rem, sum to check an Armstrong Number. n is used to take the user input temp value is copied from n variable and it is used for iteration, rem is used for calculating the remainder using the modulo operator, sum variable is used to store the cubic value of the rem variable plus previous sum variable’s value [sum = sum + (rem*rem*rem)].

You might also consider reading:

#1. Here we are using a while loop to iterate until temp value is not equal to 0. temp%10 extracts the last digit from the number (example: 512 % 10 = 2),
#2. We are calculating the cube of the obtained digit and adding it to the previous sum.
#3. Then we are stripping out the last digit by dividing the original number by 10 (example: 512 / 10 = 51.2 => 51, decimal point is ignored as it is declared as int variable).
#4. When the value of temp variable reaches 0, while loop ends then we are comparing whether sum variable equals n variable. If they are equal then Armstrong else not Armstrong.

C Program to check Armstrong Number


void main()
	int n, temp, rem=0, sum=0;
	printf("\nEnter a Number:");
	scanf("%d", &n);
	temp = n;
		rem = temp%10;
		sum = sum + (rem*rem*rem);
		temp = temp/10;
		printf("\nGiven Number : %d is an Armstrong Number", n);
		printf("\nGiven Number : %d is not an Armstrong Number", n);

Download Source Code


{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: