Printing armstrong number using For Loop in C++


This C++ code checks for Armstrong numbers in a given range. It takes a starting value i and an ending value n as inputs and iterates through the range to find Armstrong numbers. Armstrong numbers are numbers that are equal to the sum of their digits raised to the power of the number of digits.

The code uses division and modulus operators to extract the digits of each number in the range. It then raises each digit to the power of 3 (since it is checking for 3-digit Armstrong numbers), and adds them up to see if they equal the original number. The code prints out each Armstrong number it finds, and keeps a count of the total number of Armstrong numbers it finds.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n,h,arms=0,a,b,c,d,e;
    cout<<"\nEnter the starting value:";
    cin>>i;
    cout<<"\nEnter the Ending value:";
    cin>>n;
    cout<<"\nArmstrong numbers:";
    for(i;i<=n;i++)
    {
       a=i/10;//12
        b=i%10;//8
        c=a/10;//1
        d=a%10;//2
        b=b*b*b;
        c=c*c*c;
        d=d*d*d;
        e=b+c+d;
    if(i==e)
    {
        cout<<"\n"<<i;
        arms++;
    }
    }
    cout<<"\nTotal number of armstrong values:"<<arms;
    return 0;
}
To download raw file Click Here

Output

Enter the starting value:1
Enter the Ending value:10
Armstrong numbers:
1
Total number of armstrong values:1

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