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 |
How to convert hexadecimal to binary using c language..
1 Answers Bajaj, GAIL, Satyam, Zenqa,
I can not get my C++ program to work right. It is supposed to tell if a word is a palindrome or not, but it only tells thet the word is not a palindrome. And I can't fix it.
how to convert decimal to hexadecimal without using arrays just loops
wap for bubble sort
WHAT WILL BE THE OUTPUT OF THE FOLLOWING QUESTION void main() { int x=4,y=3,z; z=x-- -y; printf("%d%d%d",x,y,z); }
void main() { int i=7; printf("N= %*d",i,i); }
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
#include<stdio.h> void main() { int i=1; printf("%d%d%d",i++,++i,i); }
what is the large sustained error signal that eventually cause the controller output to drive to its limit
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
how tally is useful?
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.