0

Can someone tell me the code to print all armstrong Numbers from 1 to 10000 without using pow function in c++?

24th Jun 2022, 6:57 PM
Muhammad nawaz
Muhammad nawaz - avatar
6 Respuestas
+ 8
Muhammad nawaz , we will not give you a ready-made code. please do a try by yourself. if you get stuck somewhere, you can post a question and also the code of your attempt.
24th Jun 2022, 7:17 PM
Lothar
Lothar - avatar
+ 5
Muhammad nawaz We can not provide code but we can give you hint to solve problem Hint:- Get cube of each digits without pow function like x * x * x Then get sum of each cube value Now check if number is equal to sum of cubes and print number if it is equal.
24th Jun 2022, 7:28 PM
A͢J
A͢J - avatar
+ 2
Muhammad nawaz Why multiple while loop and if else condition even you can just do in 2 loop Create a function which will return cube of a number. Now call this function and assign return to a variable inside for loop which will run till 1000 of time. Now compare this returned value with each iteration of for loop if both are same then print number.
25th Jun 2022, 5:58 AM
A͢J
A͢J - avatar
+ 1
Muhammad nawaz Save your code in Code Playground as public, then edit your question and include a link to it in the question description. Plus, explain what output you found wrong, also in the question description.
25th Jun 2022, 3:03 AM
Emerson Prado
Emerson Prado - avatar
25th Jun 2022, 6:07 AM
A͢J
A͢J - avatar
0
Whats wrong with this code? #include<iostream> using namespace std; int main() { int sum=0,rem; for(int i=1;i<=10000;i++) { sum=0; int num=i; while(i>0) { if(i>0 && i<10) { sum=i; } } while(i>0) { if(i>=10 && i<100) { rem=i%10; i=i/10; rem=rem*rem; sum=sum+rem; } } while(i>0) { if(i>=100 && i<1000) { rem=i%10; i=i/10; rem=rem*rem*rem; sum=sum+rem; } } while(i>0) { if(i>=1000 && i<10000) { rem=i%10; i=i/10; rem=rem*rem*rem*rem; sum=sum+rem; } } if(sum==num) { cout<<i<<endl; } } }
25th Jun 2022, 1:41 AM
Muhammad nawaz
Muhammad nawaz - avatar