Question

Write a complete C++ program that asks the user to input a character. Using the ASCII...

Write a complete C++ program that asks the user to input a character. Using the ASCII character set as a guide, state whether the user's character is a digit (0 to 9), a letter (a to z or A to Z) or a symbol. Include the character's decimal, octal, and hex valur as well in your input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Code:

#include <iostream>

using namespace std;

void convertToOctal(int dec){

int octal[100];

int a=0;

while (dec != 0) {

octal[a]=dec%8;

dec=dec/8;

a++;

}

for (int b=a-1;b>=0;b--)

cout<<octal[b];

}

void convertToHex(int dec){

int a=1,b,c;

char hex[100];

while(dec!=0)

{

c=dec%16;

if(c<10)

hex[a++]=c+48;

else

hex[a++]=c+55;

dec=dec/16;

}

for (b=a;b>0;b--)

cout<<hex[b];

}

int main() {

char ch;

int dec;

cout<<"Enter a character: ";

cin>>ch;

if(ch>=48 && ch<=57){

cout<<ch<<" is a digit"<<endl;

dec=ch;

cout<<"Decimal value of "<<ch<<" is "<<dec<<endl;

cout<<"Octal value of "<<ch<<" is ";

convertToOctal(dec);

cout<<endl;

cout<<"Hex value of "<<ch<<" is ";

convertToHex(dec);

cout<<endl;

}

else if(ch>=65 && ch<=122){

cout<<ch<<" is an Alphabet"<<endl;

dec=ch;

cout<<"Decimal value of "<<ch<<" is "<<dec<<endl;

cout<<"Octal value of "<<ch<<" is ";

convertToOctal(dec);

cout<<endl;

cout<<"Hex value of "<<ch<<" is ";

convertToHex(dec);

cout<<endl;

}

else{

cout<<ch<<" is a Symbol"<<endl;

dec=ch;

cout<<"Decimal value of "<<ch<<" is "<<dec<<endl;

cout<<"Octal value of "<<ch<<" is ";

convertToOctal(dec);

cout<<endl;

cout<<"Hex value of "<<ch<<" is ";

convertToHex(dec);

cout<<endl;

}

}

if you like the answer please provide a thumbs up.

Add a comment
Know the answer?
Add Answer to:
Write a complete C++ program that asks the user to input a character. Using the ASCII...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT