在C ++中将十进制转换为二进制
生活随笔
收集整理的這篇文章主要介紹了
在C ++中将十进制转换为二进制
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Here you will get program to convert a number from decimal to binary in C++. 在這里,您將獲得在C ++中將數(shù)字從十進(jìn)制轉(zhuǎn)換為二進(jìn)制的程序。 How to convert decimal number to binary? 如何將十進(jìn)制數(shù)轉(zhuǎn)換為二進(jìn)制?
Divide the number by 2 and save the remainder somewhere.
將數(shù)字除以2,然后將其余部分保存在某處。
Divide the quotient with 2 again and save the remainder somewhere.
再次將商除以2,并將余數(shù)保存到某處。
Repeat the process until 1 is left as quotient.
重復(fù)該過程,直到將1保留為商。
Now write the remainders in reverse order.
現(xiàn)在,以相反的順序?qū)懹鄶?shù)。
C ++中的十進(jìn)制到二進(jìn)制程序 (Program for Decimal to Binary in C++)
#include<iostream>using namespace std;int main() {int d,n,i,j,a[50];cout<<"Enter a number:";cin>>n;cout<<"\nThe binary conversion of "<<n<<" is 1";for(i=1;n!=1;++i){d=n%2;a[i]=d;n=n/2;}for(j=i-1;j>0;--j)cout<<a[j];return 0; } Output輸出量 Enter a number:5輸入數(shù)字:5 The binary conversion of 5 is 1015的二進(jìn)制轉(zhuǎn)換為101翻譯自: https://www.thecrazyprogrammer.com/2011/03/c-program-to-convert-decimal-number-to-2.html
總結(jié)
以上是生活随笔為你收集整理的在C ++中将十进制转换为二进制的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 欧盟商标注册
- 下一篇: cacti监控linux网卡流量,Lin