my code is running fine but after submitting it says wrong ans
Armstrong number?
okay, but where is your code.
Would mind sharing it.
It would be of great help to solve it.
#include #include using namespace std; int digits(int x){ int count=0; while(x!=0){ x=x/10; count++; } return count; } bool armstrong(int n){ int result=0; int a=n; int count=digits(n); while(n!=0){ int rem=n%10; result+=pow(rem,count); n=n/10; } if(a==result){ return true; } else{ return false; } } int main() { int number; cin>>number; cout<<armstrong(number)<<endl; return 0; }
how can i share my code…? by submitting??
No.
From the next time, pass the link of the coding blocks IDE where you are writing your code.
Now, coming back to your code,
You are required to print either “true” or “false” depending upon the result of processing.
But, the cout statement is printing their binary representation i.e. 1 or 0.
The standard output stream(i.e. cout) have a boolalpha flag that determines what gets displayed:
- When this flag is false, they’ll display as 0 and 1.
- When it’s true, they’ll display as false and true.
There’s also an boolalpha manipulator to set the flag:
Solution:
cout<< boolalpha<<armstrong(number)<<endl;
cout<<boolalpha; //this statement will set the flag as true.
Hope, this would help.
Give a like if you are satisfied.