Decision Making in C: if, if-else, and switch Statements

Every interesting program needs to make decisions — do this if a condition holds, do something else otherwise. C provides two main tools for this: the if family of statements and the switch statement. This is Post 5, closing out the Unit I series on C fundamentals. The if Statement if (condition) { // executes … Read more

Menu-Driven Unit Converter Using Switch Statement in C

This program implements a menu-driven unit converter (kilometers to miles, Celsius to Fahrenheit) using a switch statement. C Program #include <stdio.h> int main() { int choice; float value, result; printf(“Unit Converter Menu\n”); printf(“1. Kilometers to Miles\n”); printf(“2. Celsius to Fahrenheit\n”); printf(“Enter your choice: “); scanf(“%d”, &choice); switch (choice) { case 1: printf(“Enter distance in kilometers: … Read more

Display Number of Days in a Month Using Switch Statement in C

This program displays the number of days in a given month (1-12) using a switch statement, taking leap years into account for February. C Program #include <stdio.h> int main() { int month, year, days; printf(“Enter month (1-12): “); scanf(“%d”, &month); printf(“Enter year: “); scanf(“%d”, &year); switch (month) { case 1: case 3: case 5: case … Read more

Convert Celsius to Fahrenheit and Back Using Switch Statement in C

This program converts a given temperature from Celsius to Fahrenheit or Fahrenheit to Celsius, based on the user’s choice, using a switch statement. C Program #include <stdio.h> int main() { int choice; float temp, result; printf(“1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\n”); printf(“Enter your choice: “); scanf(“%d”, &choice); printf(“Enter temperature: “); scanf(“%f”, &temp); switch (choice) … Read more

Simple Calculator Using Switch Statement in C

This program builds a simple calculator that performs addition, subtraction, multiplication, and division based on the user’s menu choice, using a switch statement. C Program #include <stdio.h> int main() { int choice; float num1, num2, result; printf(“Enter two numbers: “); scanf(“%f %f”, &num1, &num2); printf(“Choose an operation:\n”); printf(“1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n”); printf(“Enter your choice … Read more