1. Write a program to ask the user to input an ASCII character. Remember 0 throu
ID: 3922704 • Letter: 1
Question
1. Write a program to ask the user to input an ASCII character. Remember 0 through 9 are numeric ASCII characters as well. If the user enters any character other than the numeric character, inform the user that it is an invalid entry. If the user enters a numeric character, then add 5 to the numeric value and display the result. For example, if the user enters 5, display 10. You CANNOT ask the user to enter an integer. You need only one if-else statement to solve this problem and CANNOT use more than one if-else. Don’t use switch-case.
Explanation / Answer
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char asciiChar;
int asciivalue;
printf(" Enter an ASCII character");
scanf("%c",&asciiChar);
asciiChar=asciiChar - '0'; // return actual value of char
printf(" Ascii Character=%d",asciiChar);
asciivalue=(int)asciiChar;
printf(" Ascii value=%d",asciivalue);
if(isdigit(asciiChar)!=0)
{
printf(" %c is an invalid entry",asciiChar);
}
else
{
asciivalue=asciivalue +5;
printf(" Ascii value after adding 5 : %d",asciivalue);
}
return 0;
}
Output:
Success time: 0 memory: 2172 signal:0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.