Introduction to condition and switch

  

example of how to use condition in c

  

Different example of condition in c programming

  

example of how to use switch in c programming

  

example of different qauestion using switch in c programming

  

Introduction to Loop in c programming

  

Different question of using loop in c programming

  

Introduction to array in c programming

  

Different question and answer in array

  

different question and answer in switch using c programming




Write a program to print number of days in a month using switch case



#include <stdio.h>
int main() {
int month;
printf("Enter the month (1-12): ");
scanf("%d", &month);
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("31 days\n");
break;
case 4: case 6: case 9: case 11:
printf("30 days\n");
break;
case 2:
printf("28 or 29 days\n");
break;
default:
printf("Invalid month\n");
}
return 0;
}


2. Write a program to print day of week name using switch case



#include <stdio.h>
int main() {
int day;
printf("Enter a day of the week (1-7): ");
scanf("%d", &day);
switch (day) {
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 day\n");
}
return 0;
}




3. Write a program to create calculator using switch Statement



#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
} else {
printf("Error: Division by zero\n");
}
break;
default:
printf("Invalid operator\n");
}
return 0;
}



4. Write a program to check even or odd number using switch case



#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
switch (number % 2) {
case 0:
printf("Even number\n");
break;
case 1:
case -1:
printf("Odd number\n");
break;
}
return 0;
}





5. Write a program to check vowel or consonant using switch case



#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
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;
}



6. Write a program to print gender (Male/Female) program according to given M/F.



#include <stdio.h>
int main() {
char gender;
printf("Enter your gender (M/F): ");
scanf(" %c", &gender);
switch (gender) {
case 'M':
case 'm':
printf("Male\n");
break;
case 'F':
case 'f':
printf("Female\n");
break;
default:
printf("Unknown gender\n");
}
return 0;
}



7. Write a Program to find maximum number using switch case



#include <stdio.h>
#include 
int main() {
double num1, num2, num3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
printf("Maximum number: %.2lf\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("Maximum number: %.2lf\n", num2);
else
printf("Maximum number: %.2lf\n", num3);
return 0;
}


Contact Us

Email: info@learn.co.rw
Phone: +250723709880
Whatsap: +250725527181

we will try to reply to your message as quickly as we can. please be aware that because of many messages we receive it take us same time to respond