Solved Examples · Example 23
Q.Write a C program to accept a character from the keyboard and print "Yes" if it is equal to y.
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include<stdio.h>
void main()
{
char ch;
printf("Enter the character\n");
ch=getchar();
if (ch=='y'||ch=='y')
printf("Yes\n");
else
printf("No\n");
}
/*End of main()*/
Sample output printed in the textbook:
Run 1 Run 2
Enter a character Enter a character
y s
Yes No
#include<stdio.h>
void main()
{
char ch;
printf("Enter the character\n");
ch=getchar();
if (ch=='y'||ch=='y')
printf("Yes\n");
else
printf("No\n");
}
/*End of main()*/
Run 1 Run 2
Enter a character Enter a character
y s
Yes No
Karnataka PUCTextbookLongImportance★★★★★est
70% · 108/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
Read one character with getchar(); print "Yes" for 'y' or 'Y', otherwise "No".
This shows character input with getchar() and a compound condition built with the logical OR operator ||. A character constant is written in single quotes ('y'), and || evaluates to true when at least one of its two comparisons is true, so ch == 'y' || ch == 'Y' matches both cases of the letter. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.