Armstrong Number using C++ Programming


An Armstrong number is the one whose value is equal to the sum of the cubes of its digits. Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is called Armstrong number and if its sum is not equal to the number then its not a Armstrong number. Armstrong Number Program is very popular in c++.

Examples:
    153 => ( 1*1*1 ) + ( 5*5*5 ) + ( 3*3*3 ) = 153 is Armstrong Number
    453 => ( 4*4*4 ) + ( 5*5*5 ) + ( 3*3*3 ) = 216 is Not Armstrong Number

The program is written in C++ programming language and aims to find all the three-digit Armstrong numbers. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits.

The program starts by declaring four integer variables named sum, n, t, and r, and initializing the sum variable to zero. The for loop is used to iterate through all three-digit numbers, from 100 to 999.

Inside the for loop, the current number is assigned to the variable n. Then, a while loop is used to extract each digit of the number n by repeatedly dividing n by 10 and taking the remainder. The remainder is stored in the variable r. The sum of the cubes of each digit is calculated by adding the cube of the current digit (rrr) to the variable sum.

After the while loop ends, the sum is compared with the original number i. If they are equal, then the number is an Armstrong number and is printed on the console. Finally, the sum variable is reset to zero, and the next number is processed in the for loop.

Source Code

//Program for armstrong number between 100-999
#include<iostream>
using namespace std;
int main()
{
    int sum=0,n,t,r;
  for(int i=100;i<=999;i++)
  {
 
    n=i;
    while(n>0)//153  15
    {
        r=n%10;//3  5 1
        sum=sum+(r*r*r);//27 + 125=>152+1
        n=n/10;//15  1 0
    }
    if(sum==i)
    {
        cout<<i<<endl;
    }
    sum=0;
  }
    return 0;
}
 

Output

153
370
371
407
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples