Wednesday, March 22, 2023

Write a C++ Program to Check Armstrong Number

C++ Program to Check Armstrong Number

Write a C++ Program to Check Armstrong Number without using library.

The goal of this program is to check whether a given integer is an Armstrong number or not. An Armstrong number is a number such that the sum of its digits raised to the power of the number of digits is equal to the original number. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

#include <iostream>
using namespace std;

int main() {
   int num, originalNum, remainder, n = 0;
   float result = 0.0;

   std::cout << "Enter an integer: ";
   std::cin >> num;

   originalNum = num;

   // count number of digits
   while (originalNum != 0) {
      originalNum /= 10;
      ++n;
   }

   originalNum = num;

   // calculate result
   while (originalNum != 0) {
      remainder = originalNum % 10;

      // calculate remainder raised to the power of n
      int r_n = 1;
      for (int i = 1; i <= n; ++i) {
         r_n *= remainder;
      }
      result += r_n;

      originalNum /= 10;
   }

   // check if number is Armstrong
   if (int(result) == num)
      std::cout << num << " is an Armstrong number.";
   else
      std::cout << num << " is not an Armstrong number.";

   return 0;
}


//===========
OUTPUT:
Enter an integer: 153
153 is an Armstrong number.

Enter an integer: 123
123 is not an Armstrong number.

Here are some notes that can help you to understand the C++ program to check whether a number is an Armstrong number or not:

  1. The #include <iostream> statement at the beginning of the program is used to include the standard input/output library in C++. This library provides functionality for reading and writing data to and from the console.
  2. The using namespace std; statement is used to avoid having to write std:: before every standard library function call. It tells the program to use the std namespace by default.
  3. The int main() function is the entry point of the program, and it must be included in every C++ program. This function returns an integer value indicating whether the program terminated successfully or with an error.
  4. The int num, originalNum, remainder, n = 0; and float result = 0.0; statements are used to declare variables of type int and float respectively. These variables are used to store the input number, the original input number, the remainder after dividing the original number by 10, the number of digits in the input number, and the result of the Armstrong number check.
  5. The cout << "Enter an integer: "; statement is used to prompt the user to enter an integer, which is then stored in the num variable using the cin >> num; statement.
  6. The originalNum = num; statement is used to store the original input number, so that we can use it later to check if the calculated result is equal to the input number.
  7. The while (originalNum != 0) { originalNum /= 10; ++n; } loop is used to count the number of digits in the input number by dividing originalNum by 10 repeatedly until it becomes zero. The ++n statement is used to increment the n variable for each digit counted.
  8. The originalNum = num; statement is used to reset originalNum to the original input number so that we can use it again in the next loop.
  9. The while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } loop is used to calculate the result of the Armstrong number check. This loop calculates the remainder of originalNum after dividing it by 10, raises it to the power of n using the pow function, adds the result to result, and then divides originalNum by 10. This loop repeats until originalNum becomes zero.
  10. The if (int(result) == num) cout << num << " is an Armstrong number."; else cout << num << " is not an Armstrong number."; statement is used to check if the integer value of result is equal to the input number. If it is, then the program outputs that the input number is an Armstrong number. If it is not, then the program outputs that the input number is not an Armstrong number.

I hope these notes help you understand the program better!

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...