C++ Online Compiler
Example: C++ program to display armstrong number between two intervals using user defined function
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ program to display armstrong number between two intervals using user defined function #include <bits/stdc++.h> #include <cmath> using namespace std; // @custom function to find the armstrong numbers int findArmstrongNumbers(int low_number, int high_number) { int original_number, remind, count = 0, temp; double result = 0.0; // this will swap the value of intervals between each other // if low_number is greater then high_number if (low_number > high_number) { temp; temp = low_number; low_number = high_number; high_number = temp; } cout << "\nArmstrong numbers between " << low_number << " and " << high_number << " is: "; for (int i = low_number + 1; i < high_number; i++) { original_number = i; // first it will count digits of numbers while (original_number != 0) { original_number = original_number/10; count++; } original_number = i; // second it will contains sum of nth power of individual digits while (original_number != 0) { remind = original_number % 10; result += pow(remind, count); original_number = original_number/10; } // third it will check if number is equal to the sum of nth power of individual digits if ((int)result == i) { cout << i << " "; } // last it will reset the values of count & result count = 0; result = 0; } cout << "\n"; return 0; } // @driver function to call & find the armstrong numbers int main() { int low_number, high_number; cout << "Enter the two intervals:\n"; cin >> low_number >> high_number; // @call the custom function to find the armstrong numbers findArmstrongNumbers(low_number, high_number); return 0; }
100 2000
Output
Clear
ADVERTISEMENTS