how to convert decimal to binary in c using while loop
without using array
Answers were Sorted based on User's Feedback
Answer / nimesh soni
#include<stdio.h>
#include<conio.h>
void main()
{
long int no;
long int i,k,andmask;
clrscr();
printf("No : ");
scanf("%ld",&no);
printf("\n");
for(i=20;i>=0;i--)
{
andmask=1<<i;
k=no & andmask;
k==0?printf("0 "):printf("1 ");
}
getch();
}
Is This Answer Correct ? | 4 Yes | 4 No |
Answer / kishore
void main()
{
int dec,rem,k=1;
long int bin=0;
printf("Enter the decimal number : ");
scanf("%d",&dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(k*rem);
k=k*10;
}
printf("The binary number is %l",bin);
getch();
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / rudresh
#include<iostream>
#include <conio.h>
#include<vector>
using namespace std;
// Create a function to return the n to the power of m
// or you can you pow() of <math.h> but you will have to //use the casting.
int pow(int n ,int m)
{
int ans = 1;
for(int i =1 ;i<= m;i++)
ans = n*ans;
return ans;
}
vector<int> binary(int a){
vector<int> v;
while(a != 0){
v.push_back(a%2);
a/=2;
}
return v;
}
int main(){
int decno ,binno = 0 ;
cout<<"Enter no to get binary:- ";
cin>>decno;
vector<int> v;
v = binary(decno);
while(!v.empty())
{
binno = binno + v.back()*pow(10,v.size()- 1);
v.pop_back();
}
cout<< binno;
_getch();
return 0;
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / somya garg
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
long ans=1;
clrscr();
printf("enter value in decimal=");
scanf("%d",&a);
c=a;
if(a%2==0||a==1)
a=a;
else
a=a-1;
while(a>1)
{
b=a%2;
a=a/2;
ans=ans*10+b;
}
if(c%2==0||c==1)
printf("ans in binary=%ld",ans);
else
printf("ans in binary=%ld",ans+1);
getch();
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / chiran ravani
#include <stdio.h>
void main()
{
int n, r, i=0, j, a[10], k=0;
clrscr();
printf("Enter decimal number (upto 1024):");
scanf("%d",&n);
j = n;
do
{
r = n%2;
n = n/2;
a[k] = r;
k++;
i = (i*10) + r;
}while(n>0);
printf("\nBinary equivalent of %d = ",j);
for(j=k-1;j>=0;j--)
{
printf("%d",a[j]);
}
getch();
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / anubhab pal
@#include<stdio.h>
void main()
{
int decimal,r,binary=0,i=1;
printf("Eneter a Decimal Number: ");
scanf("%d",&decimal);
while(decimal!=0)
{
r=decimal%2;
decimal=decimal/2;
binary=binary+(i*r);
i=i*10;
}
printf("\nThe binary number is: %d\n",bin);
}
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / raj bahadur patel
#include<stdio.h>
#include<conio.h>
void func(int ,int );
void main()
{
int ch,dec;
clrscr();
printf("Enter the number\n");
scanf("%d",&dec);
printf("Enter 1. for decimal to binary ");
printf("Enter 2. for decimal to octal ");
printf("Enter 3.to exit ");
printf("enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Binary equivalent is :");
func(dec,2);
break;
case 2:
printf("octal equivalent is :");
func(dec,8);
break;
default:
printf("Wrong choice ");
}
getch();
}
void func(int dec,int b)
{
int i=0,j=0;
int r,ch;
int p[10];
while(dec>0)
{
p[i]=0;
r=dec%b;
dec=dec/b;
p[i]=r;
i++;
}
printf("The binary number is...\n");
for(j=i-1;j>=0;j--)
printf("%d",p[j]);
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / preeti bahuguna
#include<stdio.h>
#include<conio.h>
void main()
{
int dec,rem,i=1;
long int bin=0;
printf("Enter the decimal number : ");
scanf("%d",&dec);
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The binary number is %ld",bin);
getch();
}
Is This Answer Correct ? | 11 Yes | 11 No |
Answer / ankita batt
#include<stdio.h>
#include<conio.h>
void main()
{
int n=0,i=0;
int a[31];
clrscr();
printf("\nenter the number \n");
scanf("%d",&n);
do
{
for(i=0;i<32;i++)
{
a[i]=n%2;
n=n/2;
}
}while((n%2)!=0);
for(i=31;i>=0;i--)
{
printf("%2d",a[i]);
}
getch();
}
Is This Answer Correct ? | 1 Yes | 1 No |
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int a,b,c,d,e,f,g,h,num;
while(1)
{
printf("\t\t\t\nENTER THE NUMBER YOU WISH TO CONVERT\n");
scanf("%d",&num);
if(num<=255) /* 1 BYTE */
{
a=num%2;
b=(num/2)%2;
c=(num/4)%2;
d=(num/8)%2;
e=(num/16)%2;
f=(num/32)%2;
g=(num/64)%2;
h=num/128;
}
printf("\t\t\tTHE BINARY EQUIVALENT FOR %d IS
%d%d%d%d%d%d%d%d",num,h,g,f,e,d,c,b,a);
}
getche();
}
Is This Answer Correct ? | 0 Yes | 0 No |
How to reverse a linked list without using array & -1? Thank you.
printy(a=3,a=2)
WHAT WILL BE THE OUTPUT OF THE FOLLOWING QUESTION void main() { int x=4,y=3,z; z=x-- -y; printf("%d%d%d",x,y,z); }
How to create a program that lists countries capitals when country is entered? (Terribly sorry, I'm a complete novist to coding with C, am looking for inspiration and general tips on how to code and create this program.)
how to convert decimal to hexadecimal without using arrays just loops
UINT i,j; i = j = 0; i = ( i++ > ++j ) ? i++ : i--; explain pls....
which typw of errors ? & how to solve it ?
I am using Qt 5.6 during compilation it stops and gives error about Qmake The process "C:QtQt5.6.35.6.3msvc2015_64inqmake.exe" crashed. Error while building/deploying project untitled1 (kit: Desktop Qt 5.6.3 MSVC2015 64bit) When executing step "qmake"
A sample program using data structure? what is file handling?
How to upgrade LOOP environment, I just mean, how can i make loop statement editable ? I just try some program using loop statement and checking it in multiple compilers. Every compiler showing different output, what's the wrong ? is it a compiler based problem, or loop based problem, tell me why ? and what will be the debugging process, for this kind of problem ?
How to convert hexadecimal to binary using c language..
1 Answers Bajaj, GAIL, Satyam, Zenqa,
write the value of x and y after execution of the statements: int x=19,y; y=x++ + ++x; x++; y++;