#Can anyone help me in decimal to binary program in cpp ?

66 messages · Page 1 of 1 (latest)

kindred daggerBOT
#

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.

ruby knoll
#

!format cpp

#

wait hold up what was the cmd

#

!f

kindred daggerBOT
#
#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;
}
Gautam
ruby knoll
#

Alright there we go

#

So what's the issue

lusty abyss
#

;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);
}

elfin mantleBOT
#
Program Output
101
lusty abyss
#

appears to work idk

ruby knoll
#

;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);
}
elfin mantleBOT
#
Program Output
111110100
ruby knoll
#

Yeah seems to work

wild relic
#

I put 5 but it gave 100

#

It should be 101 I think

lusty abyss
#

;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

elfin mantleBOT
#
Program Output
101
wild relic
#

?

wild relic
lusty abyss
#

is this your current code?

wild relic
#

yup

lusty abyss
#

i want to see wht happens

ruby knoll
wild relic
lusty abyss
wild relic
#

whats wrong with vs code

lusty abyss
#

how are you compiling/running?

wild relic
lusty abyss
#

mmmmm

#

are you sure you're modifying the dec_to_binary.cpp file?

wild relic
#

yup

lusty abyss
#

absolutely no idea then

wild relic
#

ok

ruby knoll
#

As insane as it sounds

wild relic
#

yes

ruby knoll
#

Where did you get g++

wild relic
#

it was working fine yesterday .....just incurred the problem today..... so I thought some problem with code

ruby knoll
#

My best guess is that you have 2 versions of the code

#

and the one you're compiling is an old version

wild relic
#

I have only two files in this folder

ruby knoll
#

and paste them here

#

with a codeblock

wild relic
#

Meaning?

ruby knoll
#

open the dec_to_binary.cpp file

#

using notepad

#

Ctrl A

#

Ctrl C

#

then
```cpp
Ctrl V here
```

wild relic
# ruby knoll open the dec_to_binary.cpp file
#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 ;

}
ruby knoll
#

did you install any new stuff since yesterday?

wild relic
#

I don't think so

wild relic
ruby knoll
#

yeah

#

my best guess

hollow radish
#

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 ;
}
lusty abyss
#

i doubt a conditional within a loop is the best

wild relic
#

!solved

kindred daggerBOT
#

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