Armstrong:- An Armstrong number is the one whose value is equal to the sum of the cubes of its digits. Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is called Armstrong number and if its sum is not equal to the number then its not a Armstrong number.
C++ Program
// Open Dev C++ Application/Software on Your Laptop or PC.
// Question No.05. Write a program in C++ to print "Find Armstrong Number Between 1 to 1000 no." .
// C++ program to find Armstrong numbers // between 1 to 1000 using a brute force // approach #include <bits/stdc++.h> using namespace std; // Function to return the order of // a number. int order(int num) { int count = 0; while (num > 0) { num /= 10; count++; } return count;} // Function to check whether the // given number is Armstrong number // or not bool isArmstrong(int num) { int order_n = order(num); int num_temp = num, sum = 0; while (num_temp > 0) { int curr = num_temp % 10; sum += pow(curr, order_n); num_temp /= 10; } if (sum == num) { return true; } else { return false; }} // Driver code int main() { cout << "Armstrong numbers between 1 to 1000 : "; // Loop which will run form 1 to 1000 for (int num = 1; num <= 1000; ++num) { if (isArmstrong(num)) { cout << num << " "; } } return 0;}
Output:
Armstrong numbers between 1 to 1000 : 1 2 3 4 5 6 7 8 9 153 370 371 407
Allrounder SITA RAM SAHU
No comments:
Post a Comment