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 |
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
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............
Write down the difference between c. Loop and goto statement d. (!0) and (!1) e. (1= =! 1) and (1!=1) f. NULL and !NULL
what is run time error?
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]
I'm having trouble with coming up with the correct code. Do I need to put a loop? Please let me know if I'm on the right track and what areas I need to correct. I still don't have a good grasp on this programming stuff. Thanks =) The assignment was to write a program using string functions that accepts a coded value of an item and displays its equivalent tag price. The base of the keys: 0 1 2 3 4 5 6 7 8 9 X C O M P U T E R S Sample I/O Dialogue: Enter coded value: TR.XX Tag Price : 68.00
write a profram for selection sort whats the error in it?
full c programming error question based problem
#include<>stdio.h> #include<>conio.h> { printf("hello"); void main() getch(); } what the out put of this program and why ......plz clear my answer
what is the error in the following code: main() { int i=400,j; j=(i*i)/i; }
what is meant for variable not found?
difference between c/c++ programing language? what is necessesity of c++ when existing c programing language?