0
Can someone tell me the code to print all armstrong Numbers from 1 to 10000 without using pow function in c++?
6 ответов
+ 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.
+ 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.
+ 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.
+ 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.
+ 1
Muhammad nawaz
In just few lines of code
https://code.sololearn.com/cWUiDZr3GlOe/?ref=app
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;
}
}
}