Assignment 4 for C programming Lab
Assignment 4 for C programming Lab
Aim:
To develop a simple calculator that performs basic arithmetic operations (+, -, *, /) using the switch case.
Code:
#include <stdio.h>
int main() {
char op;
double first, second;
switch (op) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", first, second, first + second);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", first, second, first - second);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", first, second, first * second);
break;
case '/':
if (second != 0) {
printf("%.2lf / %.2lf = %.2lf\n", first, second, first / second);
} else {
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}
return 0;
}
The program takes two numbers and an arithmetic operator as input, performs the specified operation using a switch case, and
displays the result. It also handles division by zero error.
Aim:
To determine whether the given character is a vowel or a consonant using a switch case.
Code:
#include <stdio.h>
int main() {
char ch;
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel\n");
break;
default:
printf("Consonant\n");
}
return 0;
}
Conclusion:
The program checks whether an entered character is a vowel or a consonant using a switch case. It supports both uppercase and
lowercase vowels.
Q3: Write a C program to print the day of the week using switch case.
Aim:
To take an input number (1-7) and display the corresponding day of the week using the switch case.
Code:
#include <stdio.h>
int main() {
int week;
switch(week) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input! Please enter a number between 1 and 7.\n");
}
return 0;
}
The program takes a number (1-7) as input and prints the corresponding day of the week using a switch case. It also handles invalid
inputs.