A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write C++ program to find Armstrong numbers between 1 to n
Q:

Write C++ program to find Armstrong numbers between 1 to n

0

Write C++ program to find Armstrong numbers between 1 to n

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#include <math.h>
 
using namespace std;
 
int main()
{
     int lower, higher, i, temp1, temp2, remainder, n = 0, result = 0;
 
     //Reading two numbers from user
    cout<<"Enter two numbers: ";
    cin>>lower;
    cin>>higher;
 
    cout<<"Armstrong numbers between "<< lower << " and "<<higher << " are: ";
     for(i = lower + 1; i < higher; ++i)
    {
        temp2 = i;
        temp1 = i;
 
        // number of digits calculation
        while (temp1 != 0)
        {
            temp1 /= 10;
            ++n;
        }
 
        // result contains sum of nth power of its digits
        while (temp2 != 0)
        {
            remainder = temp2 % 10;
            result += pow(remainder, n);
            temp2 /= 10;
        }
 
        // checking if number i is equal to the sum of nth power of its digits
        if (result == i) {
            cout<<i<<endl;
        }
 
        // resetting the values to check Armstrong number for next iteration
        n = 0;
        result = 0;
 
    }
 
    return 0;
}

Result:

Enter two numbers: 200

500

Armstrong numbers between 200 and 500 are: 370

371

407

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now