C++ Program to Find the Binary Value of Decimal Number Using for loop
C++ Code:
Image view of Code:
See Another Example Here
C++ Program to Convert binary number into decimal number
C++ Code:
- #include<iostream>
- #include <stdio.h>
- using namespace std;
- int main()
- {
- int n,x,a, c, k;
- cout<<"Enter an integer in decimal number system";
- cin>>x;
- n=x;
- cout<<"Binary Value OF Given Number Is: ";
- for( a=1;n!=0;a++)
- {
- n=n/2;
- }
- a=a-2;
- for (c = a; c >= 0; c--)
- {
- k = x >> c;
- if (k & 1)
- cout<<"1";
- else
- cout<<"0";
- }
- return 0;
- }
Decimal to Binary Conversion Sample output |
Image view of Code:
Decimal to Binary Conversion C++ program code |
See Another Example Here
C++ Program to Convert binary number into decimal number
#include
ReplyDeleteusing namespace std;
void main()
{
int a=1,sum=0,x,p;
cout<<"Enter a decimel number : ";
cin>>x;
while(x!=0)
{
sum+=(x%2)*a;
x=x/2;
a=a*10;
if(x==0) break;
}
cout<<sum;
}
Program ran perfectly, thanx for sharing
ReplyDeleteplease explane it,s logic ??????
ReplyDelete@fakhrul
ReplyDeletevariable p wasn't used ,therefore should be removed
also the " if(x == 0) break; " it will already check in "x = x/2 " if x = 0 it will end the loop.
#include
ReplyDelete#include
int main(){
int a[10],n,i;
cout<<"enter decimal no.: ";
cin>>n;
for (i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
cout<<"Binary value: ";
for (i=i-1;i>=0;i--)
{
cout<<a[i];
}
getch();
return 0;
}