I am not getting the expected output while entering 10000 in this code. This is the code
*decimal to binary*\
#include<iostream>
using namespace std;
int main() {
int N,sum=0,pv=1,r;
cin>>N;
while(N>0){
r=N%2;
sum= sum + r*pv;
N=N/2;
pv=pv*10;
}
cout<<sum<<endl;
}
You need to use unsigned long long int
type modifier to get your expected output. The value of sum
exceeds the limit of a common integer does somehow.
Just modify your declaration:
unsigned long long int N, sum = 0, pv, r;
Your code with the above modification:
Output:
10000
10011100010000
And you're good to go!