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 |
Write a program to accept two strings of Odd lengths. Then take all odd characters from one string and even characters from the other and concatenate and produce a string.
What are the different types of errors in C and when they occur?
void main() { for(int i=0;i<5;i++); printf("%d",i); } What is the output?..
32 Answers College School Exams Tests, CTS, HCL, iGate, SmartData,
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 ?
what is macro in c? Difference between single linked list & double linked list what is fifo & lifo? what is stack & queue?
class test { int a; public: test(int b):a(b){} void show(){ cout<<a; } }; void main() { test t1; test t2(5); t1.show(); t2.show(); } }
Why are memory errors hard to debug?
main() { char c; for(c='A';c<='Z';c++) getch(); }
void main() { int i=5,y=3,z=2,ans; clrscr(); printf("%d",++i + --z + i++ + --i * ++y); i=5,y=3,z=2; ans=++i + --z + i++ + --i * ++y; printf("\n%d",ans); getch(); } Its output is 37 and 31.... Please explain me why its different How it works.....
#include<stdio.h> void main() { int i=1; printf("%d%d%d",i++,++i,i); }
Find the error (2.5*2=5) (a) X=y=z=0.5,2.0-5.75 (b) s=15;
how tally is useful?