cse115-lab-manual-5-if_else part2
cse115-lab-manual-5-if_else part2
#include <stdio.h>
void main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("%c is ALPHABET.\n", ch);
else if(ch >= '0' && ch <= '9')
printf("%c is DIGIT.\n", ch);
else
printf("%c is SPECIAL CHARACTER.\n", ch);
}
Try it yourself 1a: Write a C program to check whether an input alphabet is a vowel or a consonant (assume
that the input is an English letter).
#include <stdio.h>
int main() {
char ch;
// Convert the character to lowercase to handle both upper and lower case input
ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
return 0;
}
Explanation:
Enter an alphabet: b
b is a consonant.
This program will work for both uppercase and lowercase letters.
2. C program to check whether an input is an odd number or an even number using switch-case
#include <stdio.h>
void main()
{
int num;
switch(num % 2)
{
//If n%2 == 0
case 0: printf("Number is Even");
break;
//Else if n%2 != 0
case 1: printf("Number is Odd");
break;
}
}
Try it yourself 2: Write a C program to check whether an input alphabet is a vowel or a consonant using
switch case (assume that the input is an English letter).
Here is the C program that checks whether an input alphabet is a vowel or a consonant using a switch case:
#include <stdio.h>
int main() {
char ch;
return 0;
}
Explanation:
Sample Output:
Enter an alphabet: A
A is a vowel.
Enter an alphabet: b
b is a consonant.
This program will work correctly for both uppercase and lowercase alphabets.
EXERCISE:
1. Write a C program that takes input the first letter of any of the following fruits name–
Mango ---- Tk.500/kg,
Apple ---- Tk.250/kg,
Banana ----Tk. 130/kg,
Cherry -----Tk. 270/kg, and suggests the price of the fruit as output, using switch case.
2. Find the maximum between two numbers using switch case.
3. Use switch case to make a simple calculator that can add, subtract, multiply or divide two input
numbers. The operator (+ , - , * or /) should also be read from user.
Sample Input/Output:
Enter two numbers: 4.5 2.9
Enter operator: +
4.5 + 2.9 = 7.4
Assignment:
1. Write a C program to check whether a year is a leap year or not, using switch case.
2. Farhan must pay a bill to a shopkeeper. The shopkeeper doesn’t have any change, so Farhan must
have all the notes required to pay the bill. Write a C program that reads the bill and the number of
each type of note (500, 100, 50, 20, 10, 5, 2, 1) from user and then output whether it is possible for
Farhan to pay the bill or not. If it is possible, then also output the number of each notes required to
pay the bill.
Tentative Input/Output (bold ones are user inputs):
Enter amount: 1175
Enter number of notes:
500: 1
100: 7
50: 1
20: 0
10: 5
5: 1
2: 0
1: 0
Farhan can pay the bill by using the following counts of different notes:
500: 1
100: 6
50: 1
20: 0
10: 2
5: 1
2: 0
1: 0
Exercise Problems
1. Write a C program that takes input the first letter of any of the following fruits' name and suggests the
price of the fruit using switch case.
c
Copy
#include <stdio.h>
int main() {
char fruit;
return 0;
}
c
Copy
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
3. Use switch case to make a simple calculator that can add, subtract, multiply, or divide two input
numbers. The operator (+, -, *, /) should also be read from the user.
c
Copy
#include <stdio.h>
int main() {
float num1, num2;
char operator;
return 0;
}
Assignment Problems
1. Write a C program to check whether a year is a leap year or not using switch case.
c
Copy
#include <stdio.h>
int main() {
int year;
return 0;
}
2. Write a C program that reads the bill and the number of each type of note (500, 100, 50, 20, 10, 5, 2, 1)
from the user and then outputs whether it is possible for Farhan to pay the bill or not. If it is possible,
then also output the number of each note required to pay the bill.
c
Copy
#include <stdio.h>
int main() {
int amount;
int notes[8];
int denominations[] = {500, 100, 50, 20, 10, 5, 2, 1};
int total = 0;
printf("Enter number of notes for 500, 100, 50, 20, 10, 5, 2, 1:\n");
for (int i = 0; i < 8; i++) {
printf("%d: ", denominations[i]);
scanf("%d", ¬es[i]);
}
return 0;
}