Skip to content

Commit d7f67e8

Browse files
modified: Chapter_3/07_Switch_Case_Instructions.c
new file: Chapter_3_Practice_Set/Questions.c
1 parent 97124ed commit d7f67e8

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

Chapter_3/07_Switch_Case_Instructions.c

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,73 @@
1212
//code
1313
}
1414
*/
15-
// The value of integer expression is matches against c1, c2, c3. If it matches any of these cases, that case along with all subsequenced "case" and "default" sattements are executed.
15+
// The value of integer expression is matches against c1, c2, c3. If it matches any of these cases, that case along with all subsequenced "case" and "default" sattements are executed.
16+
17+
//example code:
18+
#include <stdio.h>
19+
int main(){
20+
int a;
21+
printf("Enter a: ");
22+
scanf("%d", &a);
23+
switch (a){
24+
case 1:
25+
printf("You entered 1");
26+
case 2:
27+
printf("You entered 2");
28+
case 3:
29+
printf("You entered 3");
30+
case 4:
31+
printf("You entered 4");
32+
default:
33+
printf("You didn't entered a value between 1-4. You entered %d", a);
34+
}
35+
return 0;
36+
}
37+
38+
/*
39+
input:
40+
3
41+
*/
42+
/*
43+
output:
44+
You entered 3
45+
you entered 4
46+
You didn't entered a value between 1-4. You entered 3
47+
*/
48+
49+
// To avoid this output, you use break statement like this:
50+
#include <stdio.h>
51+
int main(){
52+
int a;
53+
printf("Enter a: ");
54+
scanf("%d", &a);
55+
switch (a){
56+
case 1:
57+
printf("You entered 1");
58+
break;
59+
case 2:
60+
printf("You entered 2");
61+
break;
62+
case 3:
63+
printf("You entered 3");
64+
break;
65+
case 4:
66+
printf("You entered 4");
67+
break;
68+
default:
69+
printf("You didn't entered a value between 1-4. You entered %d", a);
70+
break;
71+
}
72+
return 0;
73+
}
74+
75+
// input: 3
76+
// output: You entered 3
77+
// Nothing else will be printed
78+
79+
/*
80+
Note:
81+
1. We can use switch case statements even by writing cases in any order of our choice (Not necessarily ascending).
82+
2. Char values are allowed as they can be easily evaluated to an integer
83+
3. A switch can occur within another but in practice this is rarely done.
84+
*/

Chapter_3_Practice_Set/Questions.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
1. What will be the output of the program:
3+
int a = 10;
4+
if(a = 11)
5+
printf("I am 11");
6+
else
7+
printf("I am not 18");
8+
9+
2. Write program to determine whether a student has passed or failed. To pass, a student requires a total of 40% and atleast 33% in each subject. Assume there are 3 subjects and take the marks as input from the user.
10+
11+
3. Calculate the income tax paid by an employee to the government as per the slabs mentioned below:
12+
Income Slab Tab
13+
--------------------------
14+
2.5L-5.0L 5%
15+
5.0L-10.0L 20%
16+
Above 10L 30%
17+
Note that there is no tax below 2.5L. Take income amount as input from the user.
18+
19+
4. Write a program to find whether a year entered by the user is a leap year or not. Take year as input from the user.
20+
21+
5. Write a program to determine whether a charactered entered by the user is lowercase or not.
22+
23+
6. Write a program to find greatest of 4 numbers entered by the user.
24+
*/

0 commit comments

Comments
 (0)