Lab 5
Lab 5
Lab 5
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
return 0;
}
int main()
{
char ch;
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
case 'A':
printf("Vowel");
break;
case 'E':
printf("Vowel");
break;
case 'I':
printf("Vowel");
break;
case 'O':
printf("Vowel");
break;
case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}
5. 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.
#include<stdio.h>
int main()
{
char character;
printf("Enter a letter: ");
scanf("%c",&character);
switch(character)
{
case 'M':
printf("Mango---TK.500/kg");
break;
case 'A':
printf("Apple---TK.250/kg");
break;
case 'B':
printf("Banana---TK.130/kg");
break;
case 'C':
printf("Cherry---TK.270/kg");
break;
default:
printf("Error");
}
int main()
{
int num1, num2;
return 0;
}
7. 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.
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
8. Write a C program to check whether a year is a leap year or not, using switch
case.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
10. 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.
#include <stdio.h>
int main()
{
int amount;
int note500, note100, note50, note20, note10, note5, note2, note1;
return 0;
}