C Program to Check for Armstrong Number

This C program checks whether a given number is an Armstrong number or not, where the number is entered by the user. The program then determines whether the number is an Armstrong number using a loop and prints a message to the screen.

Problem Statement

Write a C program that checks whether a given number is an Armstrong number or not, where the number is entered by the user. The program should prompt the user to enter the number, determine whether it is an Armstrong number using a loop, and print a message to the screen indicating whether the number is an Armstrong number or not.

Solution

#include <stdio.h>
#include <math.h>

int main() {
   int n, digit, sum = 0, temp, num_digits = 0;

   // Get the value of n from user
   printf("Enter a positive integer: ");
   scanf("%d", &n);

   temp = n;

   // Calculate the number of digits in n
   while (temp != 0) {
      temp /= 10;
      num_digits++;
   }

   temp = n;

   // Check whether the number is an Armstrong number or not
   while (temp != 0) {
      digit = temp % 10;
      sum += pow(digit, num_digits);
      temp /= 10;
   }

   // Print the appropriate message to the screen
   if (sum == n)
      printf("%d is an Armstrong number.", n);
   else
      printf("%d is not an Armstrong number.", n);

   return 0;
}

Output

Explanation

The program starts by declaring variables to hold the value of the number entered by the user (n), the individual digits of the number (digit), the sum of the digits raised to the power of the number of digits (sum), a temporary variable to store the value of n (temp), and the number of digits in n (num_digits).

The program then prompts the user to enter a positive integer, and stores the value in the n variable.

Next, the program calculates the number of digits in n using a while loop. The loop continues until the value of temp becomes 0. In each iteration of the loop, the program removes the last digit of the number using integer division (/) and increments the num_digits variable.

After calculating the number of digits, the program checks whether the number is an Armstrong number or not using another while loop. The loop continues until the value of temp becomes 0. In each iteration of the loop, the program extracts the last digit of the number using the modulo operator (%), raises the digit to the power of the number of digits using the pow() function, adds the result to the sum variable, and removes the last digit of the number using integer division (/).

After the loop, the program checks whether the sum of the digits raised to the power of the number of digits is equal to the value of n. If the sum is equal to n, the program prints a message saying that the number is an Armstrong number. Otherwise, the program prints a message saying that the number is not an Armstrong number.

Finally, the program exits.

Conclusion

This C program demonstrates how to check whether a given number is an Armstrong number or not. It prompts the user to enter the number, determines whether it is an Armstrong number using a loop, and prints a message to the screen indicating whether the number is an Armstrong number or not.

Share:

Leave A Reply

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

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...