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 / lakshya mehra

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 %l",bin);
getch();
}

Explanation:
The output variable bin is taken as long int bcoz it might
exceed the range of normal int.

e.g.
dec=25

Then

bin=(1*1)+(10*0)+(100*0)+(1000*1)+(10000*1)
=11001

Is This Answer Correct ?    411 Yes 160 No

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

Answer / @pravin.08

@sudha
ur code is abs right with a minor mistake.
main()
{
int num,rem,b=0,i=1;
printf("enter num");
scanf("%d",&num);
while(num)
{
rem=num%2;
num=num/2;
b=b+rem*i;
i=i*10;
}
printf("\n%d",b);

}

Is This Answer Correct ?    142 Yes 96 No

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

Answer / sachin kumar sharma

#include<stdio.h>
#include<conio.h>
void main()
{
int d,rem,i=1;
long int bin=0;
printf("Enter the decimal number : ");
scanf("%d",&d);
while(d>0)
{
rem=d%2;
d=d/2;
bin=bin+(i*rem);
i=i*10;
}
printf("The binary number is %ld",bin);
getch();
}
/* eg.
enter decimal no 10
binary no is 1010

Is This Answer Correct ?    53 Yes 24 No

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

Answer / nimesh soni

#include<stdio.h>
void main()
{
long int no,i,k,andmask;
printf("Entre No ");
scanf("%ld",&no);
for(i=15;i>=0;i--)
{
andmask=1<<i;
k=no & andmask;
k==0 ? printf("0 ") : printf("1 ");
}
}

Is This Answer Correct ?    17 Yes 8 No

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

Answer / tushar srivastava

//A very Powerful method just a bit of tweaking done here
#include<iostream.h>
#include<conio.h>
void main()
{
unsigned long dec,rem,i=1;
unsigned long bin=0;//remember to set it to zero
cout<<"Enter a decimal number : ";
cin>>dec;
while(dec>0)
{
rem=dec%2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
cout<<"The binary form is "<<bin;
getch();
clrscr();
}

//this method is tested by me it worked till 1023 but not
over it
//but it's fine right......

Is This Answer Correct ?    13 Yes 7 No

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

Answer / pritesh2444

#include<stdio.h>
void main()
{
int n,j=0,i=1;
printf("\nEnter the number : ");
scanf("%d",&n);
while(n!=0)
{
j=j+((n%2)*i);
n=n/2;
i=i*10;
}
printf("%d",j);
}

Is This Answer Correct ?    23 Yes 19 No

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

Answer / tushar srivastava

Hi Friends, This is Tushar Srivastava once again. This time
I have a program which is compatible with real world bit
level communication, since I have used the VC++'s 'bool'
keyword which can store only since bit of data. Take aa look
at this simple algorithm purely developed by me.
Please note that this program has used VC++'s 'bool' keyword
which will not be available with Turbo C++ 16 bit IDE (Old
DOS Mode).
Thank you.

// Bit Converter.cpp : This Program is a simple decimal to
binary converter
//This is a demonstration program which demonstrate the
method to convert any integer data into it's binary equivalent
//The algorithm is made be Tushar Srivastava independent of
any other person working on same method.
//This program is available to users under General Public
License.

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>

int i;
bool bits(int bit_data);
int main()
{
int input_data=0;
bool bit=0;
printf("Please Enter the data to be converted :");
scanf("%d",&input_data);
for(i=15;i>=0;i--)
{
bit = bits(input_data);
printf("%d",bit);
}
getch();
return 0;
}

bool bits(int bit_data)
{
int temp_var;
temp_var = bit_data >> i;
temp_var &= 0x01;
return temp_var;
}

Is This Answer Correct ?    7 Yes 6 No

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

Answer / pritesh

#include<stdio.h>
void main()
{
int j=0,n,i=1;
printf("\nenter no.=");
scanf("%d",&n);
while(n!=0)
{
j=j+((n%2)*i);
n=n/2;
i=i*10;
}
printf("%d",j);
}

Is This Answer Correct ?    6 Yes 6 No

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

Answer / mritunjay kumar

void main()
{
int no,i=1,r,bin=0;
printf("enter a decimal no:-");
scanf("%d",&no);
clrscr();
while(no!=0)
{
r=no%2;
no=no/2;
bin=bin+(r*i);
i=i*10;
}
printf("converted no%d",bin);
getch();
}

Is This Answer Correct ?    8 Yes 8 No

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

Answer / rupam

#include<stdio.h>
#include<conio.h>
main(){
int dec,i,c,rem,bin[16];
c=0;
printf("\n \t Enter the Value : ");
scanf("%d",&dec);
while(dec!=0){
rem=dec%2;
bin[c]=rem;
dec=dec/2;
c++;
}
printf("\n \t Binary Equivalent : ");
for(i=(c-1);i>=0;i--){
printf("%d",bin[i]);
}
getch();
}

Is This Answer Correct ?    2 Yes 2 No

Post New Answer

More C C++ Errors Interview Questions

what are the techniques for reducing the fragility of a memory bug?

1 Answers  


What is the out put of this programme? int a,b,c,d; printf("Enter Number!\n"); scanf("%d",&a); while(a=!0) { printf("Enter numbers/n"); scanf("%d%d%d",&b,&c,&d); a=a*b*c*d; } printf("thanks!"); getche(); Entering numbers are a=1,b=2,c=3,d=4 b=3,c=4,d=-5 b=3,c=4,d=0

5 Answers   TCS,


loop1: { x=i<n?(i++):0; printf("%d",i); exit(x); continue; } Error- misplaced continue. Doubt-1.will the exit(x) be executed for all values of x 2.will this statement go out of the program.

5 Answers   CMC,


How to create a program that lists the capital country when told what the original country is? (Terribly sorry, I'm a novice programmer and would appreciate any help ;). Cheers, Alexxis

0 Answers  


write the value of x and y after execution of the statements: int x=19,y; y=x++ + ++x; x++; y++;

0 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,


errors are known as?

3 Answers   EX, State Bank Of India SBI,


what is the error in the following code: main() { int i=400,j; j=(i*i)/i; }

4 Answers  


full c programming error question based problem

3 Answers   HCL, TCS,


void main() { int i=1; printf("%d%d%d",i,++i,i++); } Cau u say the output....?

24 Answers   HCL,


how tally is useful?

2 Answers  


what is run time error?

7 Answers  


Categories