how to convert decimal to binary in c using while loop
without using array
Answers were Sorted based on User's Feedback
Answer / casona the hunter
/* To Convert Using While Loop/*
#include <stdio.h>
#include<conio.h>
int main(void)
{
int Bin, Dec,Octal,Hexa,ans;
int rem[10],x=0;
char ch;
clrscr();
printf("B- - -Decimal-B\n\n");
printf("O- - -Decimal-O\n\n");
printf("H- - -Decimal-H\n\n");
printf("E- - -Exit\n\n");
printf("Choice:");
scanf("%c",& ch);
switch (ch)
{
case 'b': clrscr();
printf("Enter Decimal.:");
scanf("%d",& Dec);
while(Dec!=0)
{
rem[x] = Dec%2;
Dec = Dec/2;
x++;
}
x = x-1;
while (x>=0)
{
printf("%i",rem[x]);
x--;
}
}
getch();
return 0;
}
| Is This Answer Correct ? | 21 Yes | 33 No |
Answer / vimal vijayakumar
A simple code for u....
#include<stdio.h>
void main()
{
int n,b;
printf("Enter a no");
scanf("%d",&n);
while(n>0)
{
b=n%2;
if(b==1)
printf("0");
else
printf("1");
n=n/2;
}
getch();
}
send ur feed back to --- vimal9446706153@gmail.com
| Is This Answer Correct ? | 18 Yes | 30 No |
Answer / kk
//in c++
#include<iostream.h>
#include<conio.h>
void conv(int n)
{
int a[100],i;
for(i=0;n!=0;i++)
{
a[i]=n%2;
n=n/2;
}
cout<<"the binary is";
for(i-=1;i>=0;i--)
{
cout<<a[i];
}
}
void main()
{
clrscr();
int n;
cout<<"enter the no:";
cin>>n;
conv(n);
getch();
}
| Is This Answer Correct ? | 2 Yes | 17 No |
Answer / nick
/* convert a decimal number to binary */
int dectobin(int dec)
{
int bin=0, i=1;
while(dec!=0)
{
bin+=(dec%2)*i;
dec=dec/2;
i*=10;
}
return bin;
}
| Is This Answer Correct ? | 24 Yes | 47 No |
Answer / sam
all these answers crap
when you
b = b+rem*i
b is filled with int number
and adds the next one to it in the loop
so it wont be as b= 10011001
it will be b= the number i have intered
you idiots ....
| Is This Answer Correct ? | 29 Yes | 56 No |
Answer / darhakz
#include<stdio.h>
#include<math.h>
#include<string.h>
int bin2dec(int decimal);
int main(void)
{
int num;
printf("The decimal equilavent is: %d", bin2dec(decimal));
getchar();
getchar();
return 0;
}
int bin2dec(int decimal)
{
int num[10];
int i;
int bin;
int arr1[10];
int arr2[10] = {512,256,128,64,32,16,8,4,2,1};
printf("Enter a binary(0's and 1's) number: ");
scanf("%d", &arr1);
This is qheere I get confused.
for (i = 0; i < arr1; i++)
{
if (arr1[i] == 1)
num[i] = arr1[i];
arr2[i]= num[i]
decimal = arr2[i];
}
return decimal;
}
| Is This Answer Correct ? | 15 Yes | 46 No |
Answer / ritcanz
Void main()
{
int dec,i=1,rem,res=0;
Printf("Enter the Value %d",&dec);
while(dec!=0)
{
rem=dec%2;
dec=dec/2;
res=res+(i * 1);
i=i*10;
}
printf("The Binary value is %d",res);
}
| Is This Answer Correct ? | 15 Yes | 48 No |
Answer / sachin
#include<stdio.h>
int main()
{
int n, rem, num, i=1;
printf("enter no\n");
scanf("%d", &n);
while(n>0 )
{
rem = n % 2;
n = n/2;
num = (rem * i)+num;
i = i * 10;
}
printf("binary no: %d", num);
}
| Is This Answer Correct ? | 57 Yes | 93 No |
Answer / sujeeshkrishnan
main()
{
int dec,rem,ans=0;
printf("Enter the number\n");
scanf("%d",&dec);
while(dec>=2)
{
rem=dec%2;
dec=dec/2;
if(rem==0)
ans=ans*10;
else
ans=(ans*10)+1;
}
printf("The binary number is");
while(ans>0)
{
rem=ans%10;
ans=ans/10;
printf("%d",rem);
}
getch();
return 0;
}
| Is This Answer Correct ? | 74 Yes | 163 No |
Answer / sudha
Void main()
{
int dec,i=1,rem,res=0;
Printf("Enter the Value %d",&dec);
while(dec!=0)
{
rem=dec%2;
dec=dec/2;
res=res+(i * 1);
i=i*10;
}
printf("The Binary value is %d",res);
}
| Is This Answer Correct ? | 154 Yes | 396 No |
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............
what is exceptions?
main() { char c; for(c='A';c<='Z';c++) getch(); }
What are the different types of errors in C and when they occur?
what is syntax error?
What is the code for following o/p * * * * * * * * * * * * * * * *
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
#include<iostream.h> #include<stdlib.h> static int n=0; class account { int age,accno; float amt; char name[20]; public: friend void accinfo(account [] ,int); void create(); void balenq(); void deposite(); void withdrawal(); void transaction(account []); }; void account :: create() { static int acc=1231; accno=acc+n; cout<<"\n\tENTER THE CUSTOMER NAME : "; cin>>name; cout<<"\n\t ENTER THE AGE : "; cin>>age; cout<<"\n\t ENTER THE AMOUNT : "; cin>>amt; // if(amt<=500) // cout<<"\n\tAMOUNT IS NOT SUFFICIENT TO CREATE AN ACCOUNT..."; cout<<"\n\t YOUR ACCOUNT NUMBER : "<<accno<<endl; n++; } void accinfo(account cus[],int ch) { int no,flag=0; cout<<"\n\t\tENTER YOUR ACCOUNT NUMBER : "; cin>>no; for(int i=0;i<=n&&flag==0;i++) if(no==cus[i].accno) { flag=1; switch(ch) { case 2: cus[i].balenq(); break; case 3: cus[i].deposite(); break; case 4: cus[i].withdrawal(); break; case 5: cus[i].transaction(cus); break; default: cout<<"\n\t\tEND OF THE OPERATION"; exit(1); } } if(flag==0) cout<<"\n\t\tYOUR ACCOUNT DOES NOT EXIST..."<<endl; } void account :: balenq() { cout<<"\n\t\tCUSTOMER NAME : "<< name << endl; cout<<"\n\t\tBALANCE : "<< amt << endl; } void account :: deposite() { int damt; cout<<"\n\t\tCUSTOMER NAME : "<< name <<endl; cout<<"\n\t\tBALANCE : "<< amt <<endl; cout<<"\n\tENTER THE AMOUNT TO BE DEPOSITED : "; cin>>damt; amt+=damt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt<<endl; } void account :: withdrawal() { int wamt; cout<<"\n\t\tCUSTOMER NAME : "<< name; cout<<"\n\t\tBALANCE : "<< amt; cout<<"\n\tENTER THE AMOUNT TO BE WITHDRAWN : "; cin>>wamt; if(amt-wamt>=500) { amt-=wamt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt; } else cout<<"\n\tYOUR BALANCE IS TOO LOW FOR WITHDRAWAL..."<<endl; } void account :: transaction (account cus[]) { int no,tamt,flag=0; cout<<"\n\tENTER THE RECEIVER'S ACCOUNT NUMBER : "; cin>>no; cout<<"\n\t\t ENTER THE AMOUNT : "; cin>>tamt; for(int i=0;i<=n&&flag==0;i++) if(cus[i].accno==no) { flag=1; cus[i].amt+=tamt; amt-=tamt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt<<endl; cout<<"\n\t\t RECEIVER'S BALANCE : "<<cus[i].amt<<endl; } if(flag==0) cout<<"\n\tRECEIVER'S ACCOUNT NUMBER IS NOT AVALIABLE..."<<endl; } void main() { account cus[10]; int ch; do { cout<<"\n\t\t BANK ACCOUNT"; cout<<"\n\t\t ************\n"; cout<<"\n\t\t1.CREATE AN ACCOUNT"; cout<<"\n\t\t2.BALANCE ENQUIRY"; cout<<"\n\t\t3.DEPOSITE"; cout<<"\n\t\t4.WITHDRAWAL"; cout<<"\n\t\t5.TRANSACTION"; cout<<"\n\t\t6.EXIT\n\n"; cout<<"\n\t\tENTER YOUR CHOICE : "; cin>>ch; if(ch==1) cus[n].create(); else accinfo(cus,ch); }while(1); }
How to reverse a linked list without using array & -1? Thank you.
what is meant for variable not found?
To generate the series 1+3+5+7+... using C program
how tally is useful?