how to convert decimal to binary in c using while loop
without using array

Answers were Sorted based on User's Feedback



how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

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

how to convert decimal to binary in c using while loop without using array..

Answer / amir

#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

Post New Answer

More C C++ Errors Interview Questions

I'm having trouble with coming up with the correct code. Thank You!! The assignment was to write a program using string functions that accepts a price of an item and displays its coded value. The base of the keys: X C O M P U T E R S 0 1 2 3 4 5 6 7 8 9 Sample I/O Dialogue: Enter Price: 489.50 Coded Value: PRS.UX

0 Answers  


Answering Yes or No in C++...using only stdio.h and conio.h..........help me please...? here's must be the output of the program: Screen A Exam No. items Score 1 20 20 2 35 35 Another Entry? [Y] or [N] : Screen B: Record No. Student's Name: 1 Fernando Torres 2 Chuck Norris Note: if you press Y, the program must repeat the procedure in screen A, then if N, the program must proceed to the screen B....Please Help me out............

1 Answers  


when i use cout or cin call & then either << or >> .....it shows declaration syntax error...what should i do? cout<<"anything"; int a; cin>>a; return 0;

2 Answers  


char* f() return "hello:"; void main() {char *str=f(); }

1 Answers  


what is meant by linking error? how can i solve it? if there is a linking error " unable to open file 'cos.obj'? then what should i do?

1 Answers  






which typw of errors ? & how to solve it ?

0 Answers  


What are the different types of errors in C and when they occur?

4 Answers  


difference between c/c++ programing language? what is necessesity of c++ when existing c programing language?

2 Answers   TCS,


how to convert decimal to hexadecimal without using arrays just loops

2 Answers  


Assume that the int variables i and j have been declared, and that n has been declared and initialized. Write code that causes a "triangle" of asterisks of size n to be output to the screen. Specifically, n lines should be printed out, the first consisting of a single asterisk, the second consisting of two asterisks, the third consistings of three, etc. The last line should consist of n asterisks. Thus, for example, if n has value 3, the output of your code should be * ** *** You should not output any space characters. Hint: Use a for loop nested inside another for loop.

2 Answers   HCL,


what is run time error?

7 Answers  


void main() { int i=5; printf("%d",i+++++i); }

14 Answers   HCL,


Categories