Wednesday 27 January 2016

Program to Check Armstrong Number (C++)

Program to Check Armstrong Number (C++)

Description:    A C++ program that checks whether the entered number is an Armstrong Number or not. Those numbers which sum of its digits to power of number its digits is equal to that number are known as Armstrong Number.

EXAMPLE 1
        407
        Total digits in 407=3
         4^3 + 0^3 + 7^3 = 64 + 0 + 343 = 407

EXAMPLE 2
        1634
        Total digits in 1634=4
          1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 64 = 1634

Examples of Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725  

C++ Code


#include<iostream>
using namespace std;

int main() {

     int input, temp, sum = 0, m, count = 0;
     cout << "Enter any number: ";
     cin >> input;

     temp = input;
     while (temp>0)
     {
           count++;
           temp % 10;
           temp = temp / 10;
     }

     temp = input;
     while (temp > 0)
     {
           m = temp % 10;
           sum = sum + pow(m, count);
           temp = temp / 10;
     }

     if (input == sum)
           cout << "\nYES!! The number you entered is an Armstrong number\n\n";
     else
           cout << "\nNO!! The number you entered is an Armstrong number\n\n";


}


No comments

Post a Comment

Recent Posts