C- Control Statement: Switch Codes
1. Write a program to find the arithmetic operation using the switch statement
#include <stdio.h>
int main() {
float a, b, R;
char x;
printf("Enter the numbers: ");
scanf("%f%f", &a, &b);
printf("Enter the operation (+, -, *, /): ");
scanf(" %c", &x);
switch (x) {
case '+':
R = a + b;
printf("The sum is %.2f", R);
break;
case '-':
R = a - b;
printf("The difference is %.2f", R);
break;
case '*':
R = a * b;
printf("The product is %.2f", R);
break;
case '/':
if (b != 0) {
R = a / b;
printf("The division is %.2f", R);
} else {
printf("Error: Division by zero");
}
break;
default:
printf("Invalid input");
}
return 0;
}
2. Write a program to print the names of the days using switch statement. Our output must show Sunday if the user enters 1 and so on.
#include <stdio.h>
int main() {
int num;
printf("Enter the number: ");
scanf("%d", &num);
switch (num) {
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 input");
}
return 0;
}
3. Write a program to display whether the entered charactered by the user is either vowel, consonant or digits using switch statement.
#include <stdio.h>
int main() {
char a;
printf("Enter the alphabet or digit: ");
scanf(" %c", &a);
switch (a) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("It is a vowel");
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
printf("It is a digit");
break;
default:
printf("It is a consonant");
}
return 0;
}
4. Write a program to find whether the given number is odd or even using switch statement
#include <stdio.h>
int main() {
int a, R;
printf("Enter the number: ");
scanf("%d", &a);
R = a % 2;
switch (R) {
case 0:
printf("The number is even");
break;
default:
printf("The number is odd");
}
return 0;
}
5. Write a program to check whether a given number is prime or composite using switch statement
#include <stdio.h>
int main() {
int n, i = 2;
printf("Enter the number: ");
scanf("%d", &n);
switch (n) {
case 0:
case 1:
printf("Not a Prime Number");
break;
case 2:
printf("Prime Number");
break;
default:
while (i < n) {
if (n % i == 0) {
printf("Composite Number");
break;
}
i++;
}
if (i == n) {
printf("Prime Number");
}
break;
}
return 0;
}
6. Write a program to input digit decimal number and display its digits in words using switch statement.
#include <stdio.h>
int main() {
int n, R, reverse = 0;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("Digits in words: ");
if (n == 0) {
printf("Zero");
return 0;
}
// Reverse the number
do {
R = n % 10;
reverse = reverse * 10 + R;
n = n / 10;
} while (n != 0);
// Print digits in words
while (reverse != 0) {
R = reverse % 10;
reverse = reverse / 10;
switch (R) {
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
default:
printf("Invalid Number ");
}
}
printf("\n");
return 0;
}
Comments
Post a Comment