When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
66 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
#include <cmath>
#include <iostream>
using namespace std;
int dectobinary(int n) {
int binary = 0;
int i = 0;
while (n > 0) {
int bit = n % 2;
binary = bit * pow(10, i++) + binary;
n = n / 2;
}
return binary;
}
int main() {
int num;
cin >> num;
int binary = dectobinary(num);
cout << binary << endl;
}
;compile ```cpp
#include <cmath>
#include <iostream>
int dectobinary(int n) {
int binary = 0;
int i = 0;
while (n > 0) {
int bit = n % 2;
binary = bit * std::pow(10, i++) + binary;
n = n / 2;
}
return binary;
}
int main() {
std::cout << dectobinary(5);
}
101
appears to work idk
;compile
#include <cmath>
#include <iostream>
int dectobinary(int n) {
int binary = 0;
int i = 0;
while (n > 0) {
int bit = n % 2;
binary = bit * std::pow(10, i++) + binary;
n = n / 2;
}
return binary;
}
int main() {
std::cout << dectobinary(500);
}
111110100
Yeah seems to work
.
;compile ```cpp
#include <iostream>
int dectobin(int num) {
int result;
int power = 1;
while (num) {
result += num % 2 * power;
power *= 10;
num /= 2;
}
return result;
}
int main() {
std::cout << dectobin(5);
}
``` small improvement to not use std::pow
101
when i am putting 4 its giving 99
is this your current code?
yup
mm try this
i want to see wht happens
you telling me std pow is slow?
kinda
whats wrong with vs code
yup
absolutely no idea then
ok
yes
Where did you get g++
it was working fine yesterday .....just incurred the problem today..... so I thought some problem with code
My best guess is that you have 2 versions of the code
and the one you're compiling is an old version
open the contents of the cpp file with notepad
and paste them here
with a codeblock
Meaning?
open the dec_to_binary.cpp file
using notepad
Ctrl A
Ctrl C
then
```cpp
Ctrl V here
```
#include<iostream>
#include<cmath>
using namespace std ;
int dectobinary ( int n ) {
int binary = 0 ;
int i = 0 ;
while ( n > 0 ) {
int bit = n % 2 ;
binary = bit*pow(10,i++) + binary ;
n = n / 2 ;
}
return binary ;
}
int main () {
int num ;
cin >> num ;
int binary = dectobinary(num) ;
cout << binary << endl ;
}
did you install any new stuff since yesterday?
I don't think so
Should I again install the compiler
This version doesn't require exponents.
int dectobinary(int n) {
int binary=0;
int multiplier = 1;
while (n > 0){
if (n & 1){
binary += multiplier;
}
n = n >> 1;
multiplier *= 10;
}
return binary ;
}
i doubt a conditional within a loop is the best
.
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity