Beginner program for C++. Having trouble writing a program that converts decimal
ID: 3758332 • Letter: B
Question
Beginner program for C++. Having trouble writing a program that converts decimal numbers to binary numbers and hexadecimal. Output should be: Decimal Binary Hexdecimal 0 00000000 00 1 00000001 01I have to output 1-255. To a output txt file.
Help would be appreciated. Beginner program for C++. Having trouble writing a program that converts decimal numbers to binary numbers and hexadecimal. Output should be: Decimal Binary Hexdecimal 0 00000000 00 1 00000001 01
I have to output 1-255. To a output txt file.
Help would be appreciated. Output should be: Decimal Binary Hexdecimal 0 00000000 00 1 00000001 01
I have to output 1-255. To a output txt file.
Help would be appreciated.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
using namespace std;
void main()
{
clrscr();
long int dec, rem, quot;
char hexdec[100];
int i=1, j, temp,sum=0;
cout<<"Enter any decimal number to be converted: ";
cin>>dec;
quot=dec;
while(quot!=0)
{
temp=quot%16;
// to convert integer into character
if(temp<10)
{
temp=temp+48;
}
else
{
temp=temp+55;
}
hexdecnum[i++]=temp;
quot=quot/16;
}
cout<<"Equivalent hexadecimal value of "<<decnum<<" is : ";
for(j=i-1; j>0; j--)
{
cout<<hexdecnum[j];
}
do
{
rem=dec%2;
sum=sum + (i*rem);
dec=dec/2;
i=i*10;
}while(dec>0);
cout<<"The binary of the given number is:"<<sum<<endl;
cin.get();
cin.get();
return 0;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.