decimal to binary

Hey guys, I have to write a simple program that can convert decimal to binary but I can only use only nested if else statements.I just need to simply how to set it up and I can go from there. I cannot use loops, void, and stack. Just nested if else. This what I have so far!

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main()
{
int num1, num2, num3, num4, num4, num5, num6, num7, num8;

if (num1 < 0 && num1 > 255)
cout << "ERROR! Input value " << num1 <<"is out of range for this program. " << endl;
else
num1

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

int main()
{
    unsigned int number ;
    std::cout << "enter a non-negative number less than 256: " ;
    std::cin >> number ;

    if( number > 255 )
    {
        std::cerr << "error: out of range\n" ;
        return 1 ;
    }

    if( number > 127 )
    {
        std::cout << '1' ;
        number -= 128 ;
    }
    else std::cout << '0' ;

    if( number > 63 )
    {
        std::cout << '1' ;
        number -= 64 ;
    }
    else std::cout << '0' ;

    if( number > 31 )
    {
        std::cout << '1' ;
        number -= 32 ;
    }
    else std::cout << '0' ;

    if( number > 15 )
    {
        std::cout << '1' ;
        number -= 16 ;
    }
    else std::cout << '0' ;

    if( number > 7 )
    {
        std::cout << '1' ;
        number -= 8 ;
    }
    else std::cout << '0' ;

    if( number > 3 )
    {
        std::cout << '1' ;
        number -= 4 ;
    }
    else std::cout << '0' ;

    if( number > 1 )
    {
        std::cout << '1' ;
        number -= 2 ;
    }
    else std::cout << '0' ;

    std::cout << number << '\n' ;
}
Thank you sir! I was trying to use the quotient remainder method and it wasn't really working. Thank you for showing me such a easy way to figure it out.
It is the quotient remainder mechanism. At line 22,
1
2
3
4
5
6
if( number > 63 )
{
    std::cout << '1' ;
    number -= 64 ;
}
else std::cout << '0' ;


is equivalent to:
1
2
std::cout << number / 64 ;
number %= 64 ;


Without the multiple if-else constructs (as required by the problem statement), an equivalent program would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

int main()
{
    unsigned int number ;
    std::cout << "enter a non-negative number less than 256: " ;
    std::cin >> number ;

    if( number > 255 )
    {
        std::cerr << "error: out of range\n" ;
        return 1 ;
    }

    std::cout << number / 128 ;
    number %= 128 ;

    std::cout << number / 64 ;
    number %= 64 ;

    std::cout << number / 32 ;
    number %= 32 ;

    std::cout << number / 16 ;
    number %= 16 ;

    std::cout << number / 8 ;
    number %= 8 ;

    std::cout << number / 4 ;
    number %= 4 ;

    std::cout << number / 2 ;
    number %= 2 ;

    std::cout << number << '\n' ;
}

Topic archived. No new replies allowed.