Priniting armstrong number using While Loop in C++


This is a C++ program that finds Armstrong numbers between a given starting and ending value. Armstrong numbers are numbers whose sum of cubes of digits is equal to the number itself. Let's go through the program step-by-step:

  • The program starts by including the necessary header file iostream.
  • The using namespace std statement is used to avoid writing std:: before every standard library function.
  • The main() function is the entry point of the program.
  • Inside main(), the program declares some integer variables i, n, h, and arms.
  • The user is prompted to enter the starting and ending values for the range of numbers to be checked.
  • The program then enters a while loop that checks each number in the range between the starting and ending values.
  • Inside the while loop, the program extracts the individual digits of the number and calculates the sum of the cubes of the digits.
  • If the sum of the cubes of the digits is equal to the original number, the program prints the number and increments the arms variable.
  • The program then continues checking the next number in the range.
  • Finally, the program prints the total number of Armstrong numbers found.

Overall, the program correctly identifies Armstrong numbers in the given range and prints them to the console. However, the variable h is not used anywhere in the program, and it's unclear why it was declared. Additionally, the variable e is used to store the sum of the cubes of the digits, but it's not a very descriptive name. It would be better to use a variable name that reflects the purpose of the variable.

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:";
    while(i<=n)
    {
       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++;
    }
    i++;
    }
    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:20
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