easy
0 views
Character – Male or Female
Determine if a given character represents male ('m') or female ('f') and print the corresponding gender.
Understand the Problem
Problem Statement
Write a program that accepts a single character as input and determines the gender it represents.
- If the character is 'm', print 'male'
- If the character is 'f', print 'female'
The program should handle both lowercase characters as specified in the problem statement.
Constraints
- Input will be a single character (either 'm' or 'f')
- Input will be provided via standard input (stdin)
- Output must be exactly 'male' or 'female' (lowercase)
- Program must terminate after processing one character
Examples
Example 1
Input
mOutput
maleExplanation
When the input character is 'm', the program correctly identifies it as representing male and prints 'male'.
Example 2
Input
fOutput
femaleExplanation
When the input character is 'f', the program correctly identifies it as representing female and prints 'female'.
Solution
#include <stdio.h>
int main() {
char ch;
scanf("%c", &ch);
if (ch == 'm') {
printf("male");
} else {
printf("female");
}
return 0;
}Time:O(1) - Constant time operation regardless of input
Space:O(1) - Uses only one character variable for storage
Approach:
The C solution:
- Declares a character variable
chto store input - Uses
scanf("%c", &ch)to read a single character from stdin - Uses an if-else statement to check if
ch == 'm' - If true, prints 'male' with
printf - Otherwise, prints 'female'
- Returns 0 to indicate successful execution
Visual Explanation
Loading diagram...