c++ language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).
Source Code:-
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,c,k;
cout<<"Convert Decimal To Binary No. by-Tarun Rawat\n";
cout<<"Enter an Integer in decimal number system : ";
cin>>n;
cout<<"Binary number system is : ";
for (c = 31; c >= 0; c--)
{k = n >> c;
if (k & 1)
cout<<"1";
else
cout<<"0";
}
cout<<"\n";
getch();
}
Welcome to " way2cplusplus.blogspot.in " Objective of this blog is to implement various Computer Science Engineering Lab problems into C++ programming language. These are basically most common Lab Exercise problems based on the curriculum of engineering colleges throughout the Nation. These lab exercises are also relevant to Data structure. Simply C++ programming zone...
No comments:
Post a Comment