C Program to Check Armstrong Number

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In numerical number theory, the Armstrong number definition is the number in any given number base, which forms the total of the same number when each of its digits is raised to the power of the number of digits in the number.

Prerequisites

Check Armstrong Number of three digits

Algorithm (for a 3-digit Number)

Step 1: Start.

Step 2: Read an integer input number.

Step 3: Initialize the variables current_digit, sum = 0, and num = number.

Step 4: Repeat Steps 5 to 7 until num > 0.

Step 5: current_digit = (num % 10).

Step 6: sum = sum + (current_digit * current_digit * current_digit).

Step 7: num = num / 10.

Step 8: Check if sum == number. If true, print "It is an Armstrong Number." Otherwise, print "It is not an Armstrong Number."

Step 9: END

Program for Three-Digit Armstrong Number in C

Here's a C program to check whether a given three-digit number is an Armstrong number:

Output:

Check Armstrong Number of N digits

Algorithm (for N-digit Number)

Step 1: Start.

Step 2: Read an integer input number.

Step 3: Initialize the variables current_digit, sum = 0, digits = 0, and num = number.

Step 4: Calculate the number of digits in the input integer number and store it in the variable number_of_digits.

Step 5: Repeat Steps 5 to 7 until num > 0.

Step 6: current_digit = (num % 10).

Step 7: sum = sum + pow(current_digit, number_of_digits).

Step 8: num = num / 10.

Step 9: Check if sum == number. If true, print "It is an Armstrong Number." Otherwise, print "It is not an Armstrong Number."

Step 10: END

Program for N-Digit Armstrong Number in C

Here's a generalized C program to check whether a given N-digit number is an Armstrong number:

Output:

Armstrong Number Using Functions

Refactoring code to use functions can enhance its modularity, readability, and reusability. In this section, we'll demonstrate how to check for an Armstrong number using functions in C.

Functions in Use

1. Function to Calculate Number of Digits:

  • Purpose: Calculates the number of digits in the given number.
  • Input: Integer (number).
  • Output: Integer (number of digits).

2. Function to Check Armstrong Number:

  • Purpose: Checks if a number is an Armstrong number.
  • Input: Integer (number).
  • Output: Integer (1 if Armstrong, 0 otherwise).

Main Program

  • The main program takes an integer input.
  • Calls the isArmstrong function to check if the number is an Armstrong number.
  • Displays the result based on the output from the isArmstrong function.

Conclusion

  • We have discussed what Armstrong numbers are, and then we have seen a C program to check for a three-digit number, whether it is Armstrong or not.
  • We have learned about a more generalized version of it for N-digit numbers.