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..

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 ? 387 Yes 148 No

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

@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 ? 136 Yes 88 No

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

#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 ? 49 Yes 21 No

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

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

 Is This Answer Correct ? 15 Yes 7 No

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

//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 6 No

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

#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 ? 20 Yes 16 No

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

#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 8 No

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

#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 5 No

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

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

#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..

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 ? 7 Yes 7 No

More C C++ Errors Interview Questions

What is probability to guarantee that the task a programmer is going to create will be created and be able to run on a particular system (RTOS/GPOS).

Given that two int variables, total and amount , have been declared, write a sequence of statements that: initializes total to 0 reads three values into amount , one at a time. After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ). Instructor's notes: If you use a loop, it must be a for loop. And if you use a loop control variable for counting, you must declare it.

full c programming error question based problem

To generate the series 1+3+5+7+... using C program

what is macro in c? Difference between single linked list & double linked list what is fifo & lifo? what is stack & queue?

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

what is exceptions?

A sample program using data structure? what is file handling?

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

2. A student studying Information Technology at Polytechnic of Namibia is examined by coursework and written examination. Both components of assessment carry a maximum of 50 marks. The following rules are used by examiners in order to pass or fail students. a. A student must score a total of 40% or more in order to pass (total = coursework marks + examination marks) b. A total mark of 39% is moderated to 40% c. Each component must be passed with a minimum mark of 20/50. If a student scores a total of 40% or more but does not achieve the minimum mark in either component he/she is given a technical fail of 39% (this mark is not moderated to 40%) d. Grades are awarded on marks that fall into the following categories. Mark 100-70 69-60 59-50 49-40 39-0 Grade A B C D E Write a program to input the marks for both components (coursework marks out of 50 and examination marks out of 50), out put the final mark and grade after any moderation. [30]

Display this kind of output on screen. 1 0 1 1 0 1 3. Display this kind of output on screen. 1 1 0 1 0 1 4. Display this kind of output on screen. 1 1 0 1 0 1 5.Display this kind of output on screen. 1 2 3 4 5 6 7 8 9 10