0% found this document useful (0 votes)
9 views

Assignment 4 for C programming Lab

The document contains three C programs that demonstrate the use of switch cases for different functionalities. The first program is a simple calculator performing basic arithmetic operations, the second checks if a character is a vowel or consonant, and the third prints the day of the week based on a number input. Each program includes code, example outputs, and a conclusion summarizing its purpose.

Uploaded by

f48785237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment 4 for C programming Lab

The document contains three C programs that demonstrate the use of switch cases for different functionalities. The first program is a simple calculator performing basic arithmetic operations, the second checks if a character is a vowel or consonant, and the third prints the day of the week based on a number input. Each program includes code, example outputs, and a conclusion summarizing its purpose.

Uploaded by

f48785237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1: Write a program in C to make a calculator.

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;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &op);

printf("Enter first operand: ");


scanf("%lf", &first);

printf("Enter second operand: ");


scanf("%lf", &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;
}

Output (Example Runs):


Enter an operator (+, -, *, /): +
Enter first operand: 10
Enter second operand: 5
10.00 + 5.00 = 15.00
Conclusion:

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.

Q2: Write a C program to check whether an alphabet is a vowel or


consonant using switch case.

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;

printf("Enter any alphabet:\n");


scanf("%c", &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;
}

Output (Example Runs):


Enter any alphabet:
a
Vowel

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;

printf("Enter Week number (1-7):\n");


scanf("%d", &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;
}

Output (Example Runs):

Enter Week number (1-7):


3
Wednesday
Conclusion:

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.

You might also like