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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

771


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]

2511


quoroum of computer languages?

2936


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

2742


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

1498






I am using Qt 5.6 during compilation it stops and gives error about Qmake The process "C:QtQt5.6.35.6.3msvc2015_64inqmake.exe" crashed. Error while building/deploying project untitled1 (kit: Desktop Qt 5.6.3 MSVC2015 64bit) When executing step "qmake"

1291


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

1732


Write down the difference between c. Loop and goto statement d. (!0) and (!1) e. (1= =! 1) and (1!=1) f. NULL and !NULL

3422


How to create a program that lists countries capitals when country is entered? (Terribly sorry, I'm a complete novist to coding with C, am looking for inspiration and general tips on how to code and create this program.)

1002


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

1979