I have to do a task for the university to write a program that converts a decimal number into binary. As I am only in a preliminary course of computer science, there were no arrays or bitwise operators (like '&') introduced yet. The program needs to be written using basic operators (+,-,*,%) and (if-else,for) only. My approach is the following, but I always get the inverted value. So instead of 1100, 0011.
#include <iostream>
int main()
{
int n,a;
std::cin >> n;
for (int i=n; n>0; --i) {
a = n%2;
std::cout << a;
n = n/2;
}
return 0;
}
is there a way to solve this problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…