+ 2

# cpp "no output" [solved]

#include <iostream> using namespace std; int dec(int binary){ int sum=0; if(binary%10==1){ sum++; } int temp=1; while(binary>1){ temp*=2; if(binary%10==1){ sum+=temp; binary/=10; } } return sum; }; int main() { int binary; cin>>binary; cout<<binary; cout<<dec(binary); return 0; }

31st Aug 2020, 7:49 AM
Roni Berlin
8 Answers
+ 3
Okay I found the problem, it's inside the `dec` function. 1. temp *= 2; should be done after the `if` block, not before. 2. binary /= 10; should be after the `if` block, not inside it. int dec(int binary) { int sum = 0; int temp = 1; while (binary) { if (binary % 10 == 1) { sum += temp; } binary /= 10; temp *= 2; } return sum; };
31st Aug 2020, 8:37 AM
Ipang
+ 6
I think your code working infinite times check your conditions properly #include <iostream> using namespace std; int dec(int binary ) { int num = binary; int dec_value=0 ; int base = 1; int temp = num; while (temp) { int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } int main() { int binary; cin>>binary; cout << dec(binary) << endl; }
31st Aug 2020, 8:38 AM
AS Raghuvanshi
+ 1
What input to test with, and how is the expected output?
31st Aug 2020, 7:54 AM
Ipang
+ 1
the input are in the binary int in the main and then there cout (output) of the binary and there function that does output
31st Aug 2020, 7:56 AM
Roni Berlin
+ 1
I mean give me example, a number for input. And also what output is expected by that number as input ...
31st Aug 2020, 8:12 AM
Ipang
+ 1
input 100 (binary) output 4 (decimal)
31st Aug 2020, 8:16 AM
Roni Berlin
+ 1
Okay, let me check your code. I'll be back ...
31st Aug 2020, 8:19 AM
Ipang
+ 1
grate job👏 please upvote the question 🙏
31st Aug 2020, 8:44 AM
Roni Berlin