c语言/c++十进制转化为二进制的代码教程
程序员文章站
2022-04-15 19:28:41
:
#include
int main()
{
intnum,r;
scanf("%d",&...
:
#include<stdio.h> int main() { intnum,r; scanf("%d",&num); printf("%d的二进制数是:",num); while(num!=0) { r=num%2; num=num/2; printf("%d",r); }; printf("\n"); return0; }
(2)c++
#include<iostream> using namespace std; class c { intnum; public: c() { cin>>num;} ~c(){} voidchange(); }; void c::change() { int r; cout<<"将"<<num<<"转换为二进制是:"; while(num!=0) { r=num%2; num=num/2; cout<<r; }; cout<<endl; } int main() { c c1; c1.change(); return 0; }