Basic Switch Statement Example in C

This C program prompts the user to enter a number and uses a switch statement to check if it matches 10, 20, or 30. Depending on the input, it prints the corresponding number or a default message if the number is not one of those options.
 avatar
AdhamElshabasy
c_cpp
6 months ago
327 B
2
Indexable
C Basics
#include <stdio.h>

int main() {
	
	int x;
	
	printf("Please enter X: ");
	scanf("%d", &x);
	
	switch(x) {
		case 10:
			printf("10");
			break;
			
		case 20:
			printf("20");
			break;
			
		case 30:
			printf("30");
			break;
			
		default:
			printf("it was not 10, 20, or 30");
	}
	
	return 0;
}
Editor is loading...
Leave a Comment