codescracker
c++

C++ Program to Generate Armstrong Numbers



« Previous Program Next Program »

In this article, you will learn and get code to generate and print armstrong numbers in C++ programming. Here are the list of programs available here:

What is an Armstrong Number ?

A number that equals to the sum of its own digits, where each digit raised to the power of number of digits. For example, 8208 is an armstrong number, because:

8208 = 84 + 24 + 04 + 84
     = 4096 + 16 + 0 + 4096
     = 4096 + 16 + 4096
     = 8192 + 16
     = 8208

The result is equal to the number itself. So it is an Armstrong number.

Note - Because the total number of digit in 8208 is 4, so each of its digit raised to the power of 4.

Print Armstrong Numbers between 1 to 500

This program generate and prints all armstrong numbers between 1 to 500.

The question is, write a program in C++ to generate and print Armstrong numbers between 1 to 500. Here is its answer:

// C++ Print Armstrong Numbers between 1 to 500
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int first, last, temp, noOfDigit, num, res=0, rem, pow, i;
    first = 1;
    last = 500;
    cout<<"Armstrong Numbers Between 1 and 500\n";
    while(first<=last)
    {
        temp = first;
        noOfDigit = 0;
        while(temp>0)
        {
            temp = temp/10;
            noOfDigit++;
        }
        num = first;
        while(num>0)
        {
            rem = num%10;
            pow = 1;
            i = 0;
            while(i<noOfDigit)
            {
                pow = pow*rem;
                i++;
            }
            res = res + pow;
            num = num/10;
        }
        if(res==first)
            cout<<res<<endl;
        first++;
        res = 0;
    }
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program generate Armstrong number

The program uses while loop that starts with 1 and ends with 500. And everytime the value of first gets initialized to temp. Now we've calculated the number of its digits. Then checks whether the value in temp is an armstrong number or not. If it an Armstrong, then prints its value as output, otherwise the same process continues to check with next number until the value of first becomes greater than last.

Note - You can refer to check armstrong number or not for its in-depth detail.

Print Armstrong Number in Given Range

To generate and print Armstrong numbers in given range by user, using C++ programming, you have to ask from user to enter the interval. Then apply the same process as given in previous program.

The only difference is, the value of first and last gets received by user here.

// C++ Print Armstrong Numbers in Given Range
// ----codescracker.com----

#include<iostream>
using namespace std;
int main()
{
    int first, last, temp, noOfDigit, num, res=0, rem, pow, i;
    cout<<"Enter the Interval: ";
    cin>>first>>last;
    cout<<"\nArmstrong Number(s) Between "<<first<<" and "<<last<<endl;
    while(first<=last)
    {
        temp = first;
        noOfDigit = 0;
        while(temp>0)
        {
            temp = temp/10;
            noOfDigit++;
        }
        num = first;
        while(num>0)
        {
            rem = num%10;
            pow = 1;
            i = 0;
            while(i<noOfDigit)
            {
                pow = pow*rem;
                i++;
            }
            res = res + pow;
            num = num/10;
        }
        if(res==first)
            cout<<res<<endl;
        first++;
        res = 0;
    }
    cout<<endl;
    return 0;
}

Here is its sample run. This is the initial output produced by above program:

C++ program display Armstrong number between range

Now enter the interval say 1 and 50000 to print all armstrong numbers between 1 and 50000 as shown in the snapshot given below:

print armstrong nubers in given range c++

Same Program in Other Languages

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker