+ 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; }
8 odpowiedzi
+ 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;
};
+ 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; 
}
+ 1
What input to test with, and how is the expected output?
+ 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
+ 1
I mean give me example, a number for input. And also what output is expected by that number as input ...
+ 1
input 100 (binary) 
output 4 (decimal)
+ 1
Okay, let me check your code.
I'll be back ...
+ 1
grate job👏
please upvote the question 🙏





