C Switch Case
C Switch Case
#include <stdio.h>
#include <conio.h>
void main() {
int choice;
float temp, result;
clrscr();
switch (choice) {
case 1:
printf("Enter temperature in Celsius: ");
scanf("%f", &temp);
result = (temp * 9 / 5) + 32;
printf("Temperature in Fahrenheit: %.2f", result);
break;
case 2:
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &temp);
result = (temp - 32) * 5 / 9;
printf("Temperature in Celsius: %.2f", result);
break;
default:
printf("Invalid Choice");
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main() {
int day;
clrscr();
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Sunday"); break;
case 2: printf("Monday"); break;
case 3: printf("Tuesday"); break;
case 4: printf("Wednesday"); break;
case 5: printf("Thursday"); break;
case 6: printf("Friday"); break;
case 7: printf("Saturday"); break;
default: printf("Invalid day number");
}
getch();
}
3. Calculator (Simple)
#include <stdio.h>
#include <conio.h>
void main() {
float a, b, result;
char op;
clrscr();
switch (op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b != 0)
result = a / b;
else {
printf("Division by zero!");
getch();
return;
}
break;
default:
printf("Invalid operator");
getch();
return;
}
#include <stdio.h>
#include <conio.h>
void main() {
int marks;
clrscr();
getch();
}
#include <stdio.h>
#include <conio.h>
#define PI 3.14
void main() {
int choice;
float radius, height, result;
clrscr();
switch (choice) {
case 1:
printf("Enter radius: ");
scanf("%f", &radius);
result = PI * radius * radius;
printf("Area = %.2f", result);
break;
case 2:
printf("Enter radius: ");
scanf("%f", &radius);
result = 2 * PI * radius;
printf("Circumference = %.2f", result);
break;
case 3:
printf("Enter radius and height: ");
scanf("%f %f", &radius, &height);
result = PI * radius * radius * height;
printf("Volume = %.2f", result);
break;
default:
printf("Invalid choice");
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main() {
char str[100];
int i;
clrscr();
getch();
}
#include <stdio.h>
#include <conio.h>
void main() {
int month;
clrscr();
switch (month) {
case 4: case 6: case 9: case 11:
printf("30 days");
break;
case 2:
printf("28 or 29 days");
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("31 days");
break;
default:
printf("Invalid month");
}
getch();
}
8. Check if Alphabet is Vowel or Consonant
#include <stdio.h>
#include <conio.h>
void main() {
char ch;
clrscr();
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main() {
int a, b;
clrscr();
switch (a > b) {
case 1: printf("%d is maximum", a); break;
case 0: printf("%d is maximum", b); break;
}
getch();
}
10. Check Even or Odd
#include <stdio.h>
#include <conio.h>
void main() {
int num;
clrscr();
switch (num % 2) {
case 0: printf("Even"); break;
case 1: printf("Odd"); break;
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main() {
int num;
clrscr();
getch();
}
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main() {
float a, b, c, d, root1, root2;
clrscr();
d = b * b - 4 * a * c;
switch (d > 0) {
case 1:
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
printf("Real and Distinct Roots: %.2f, %.2f", root1, root2);
break;
case 0:
switch (d == 0) {
case 1:
root1 = root2 = -b / (2 * a);
printf("Real and Equal Roots: %.2f, %.2f", root1, root2);
break;
case 0:
printf("Complex roots");
}
}
getch();
}