You are currently viewing C Program to Check Armstrong Numbers using While Loop

C Program to Check Armstrong Numbers using While Loop

To check given input is an Armstrong number or not, our program takes an input number from the user. And with the help of a while loop we will check whether the number is an Armstrong number or not.

An Armstrong number is called an Armstrong number if it is equal to the sum of its digits raised to the power of the number of digits.

In this tutorial, you will learn how to write a program to check whether a given number is Armstrong or not using a while loop. For better understanding, we are using examples and algorithms with program explanations.

Required Knowledge

Problem Statement

You have to write a C program that will take a positive integer as input from the users. After taking the inputs it will check whether it is an Armstrong number or not using a while loop.

Example

Input:
Enter a number: 153

Output:
153 is an Armstrong number.

C Program to check whether a given number is Armstrong or not

#include <stdio.h>
#include <math.h>
int main() {
    int num, temp, remainder, sum = 0, count = 0;
    printf("Give an Integer input : ");
    scanf("%d", &num);
    temp = num;
    while (num != 0) {
        num /= 10;
        ++count;
    }
    num = temp;
    while (num != 0) {
        remainder = num % 10;
        sum += pow(remainder, count);
        num /= 10;
    }
    if (sum == temp) {
        printf("Given number %d is an Armstrong number.\n", temp);
    } else {
        printf("Given number %d is not an Armstrong number.\n", temp);
    }
    return 0;
}

Output 1

Enter a number: 55
55 is not an Armstrong number

Output 2

Enter a number: 153
153 is an Armstrong number

Program Explanation

  • In the above program, we have taken 5 variables num, temp, remainder, sum = 0, and count = 0.
  • We have initialized variables sum and count with value 0. User input will be taken into the num variable and temp will also take the num value.
  • The program is using a while loop with the condition num != 0 to count the digits in the input number.
  • Also, there is one more while loop that has our logic to calculate the Armstrong number.
  • The while loop continues until the number becomes 0, indicating that all digits have been processed.
  • After the completion of the while loop, we will compare the calculated value stored in sum with the original value stored in the temp variable.
  • If the temp variable value matched with the calculated value means the condition is true. It will print the given number as an Armstrong number.
  • Otherwise, it prints that the given number is not an Armstrong number.

Conclusion

This C program successfully checks whether a given number is an Armstrong number or not using a while loop. By extracting the digits of the number, calculating their sum raised to the power of the number of digits, and comparing it with the original number, it determines whether it is an Armstrong number or not and displays the result on the screen.