2. CONTROL STRUCTURES
2.5 The Switch Statement – Selecting One Of Many Alternatives
int a,b,c;
clrscr( );
printf(“Enter the three values:”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“%d is big”,a);
else if(b>a && b>c) printf(“%d is big”,b);
else
printf(“%d is big”,c);
getch( );
}
Output 1
Enter the three values:3 5 7 7 is big
Output 2
Enter the three values:5 6 2 6 is big
Output 3
Enter the three values:3 2 1 3 is big
2.5 The Switch Statement
– Selecting One Of Many AlternativesThe Switch statement is an extension of the if-else if-else statement. The switch makes one selection when there are several
choices to be made. The direction of the branch taken by the switch statement is based on the value of any int (or int compatible) variable or expression.
The general form of Switch statement is shown below.
switch (variable) {
case constant1:statement 1;
case constant2:statement 2;
case constant3:statement 3;
“ “ “
“ “ “
“ “ “
case constant n:statement n;
default :statement;
}
The variable following the keyword switch is any C expression that that will result an integer value.
The break statement used inside each case of the switch, causes an intermediate exit from the switch statement; and continue onto the next statement outside the switch statement.
Note: If the break statement is not including, all of the statements at and below the match will be executed.
The advantage of switch over if is that it leads to a more structured program and the level of indentation is manageable. C allows nesting switch statements, i.e., a switch may be a part of a case.
Example
Program illustrates the switch statement.
/* Words corresponding Numbers */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number(0-9):”);
scanf(“%d”,&n);
switch(n) {
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(“More than 9”);
} getch();
}
Output 1 Enter a number (0-9):0 Zero
Output 2
Enter a number (0-9): 10 More than 9
PROGRAMS
Conditional Operators (?:)
Prog.21: To check whether the year is leap or not /* Inputting year is Leap or not */
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf(“Enter year:”);
scanf(“%d”,&year);
(year%4==0)?printf(“Leap year”):printf(“Not leap year”);
getch();
}
Output 1 Enter year:1990 Not leap year
Output 2 Enter year:1996 Leap year
Prog.22: To find the biggest number in two variables
/* Biggest number in two variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b;
clrscr();
printf(“Enter a,b values:”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is big”):printf(“b is big”);
getch();
}
Output 1 Enter a,b values:3 4 b is big
Output 2 Enter a,b values:4 2 a is big
Prog.23: To check the enter number is single digit or not /* Single digit or not */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
(n<=9)?printf(“Single digit”):printf(“Not single digit”);
getch();
}
Output 1
Enter a number:5 Single digit
Output 2 Enter a number:12 Not single digit
Prog.24: To check the number is whether even or odd /* Even or Odd */
#include<stdio.h>
#include<conio.h>
void main() {
int x;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&x);
(x%2==0)?printf(“It is Even Number”):printf(“It is Odd Number”);
getch();
}
Output 1 Enter a number:5 It is Odd Number
Output 2 Enter a number:4 It is Even Number
Prog.25: To check the number is positive, negative or zero.
/* To check whether +ve, -ve or zero */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
(n>0)?printf(“+ve”):(n<0)?printf(“-ve”):printf(“zero”);
getch();
}
Output 1 Enter a number: -2 -ve
Output 2 Enter a number:0 zero
Output 3 Enter a number:5 +ve
Prog.26: Find the highest number in three variables.
/* Biggest number in 3 variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b,c;
clrscr();
printf(“Enter a,b,c values:”);
scanf(“%d%d%d”,&a,&b&c);
(a>b && a>c)?printf(“a is big”):
(b>a && b>c)?printf(“b is big”):printf(“c is big”);
getch();
}
Output 1
Enter a,b,c values:3 5 7 c is big
Output 2
Enter a,b,c values:8 4 6 a is big
If statement
Prog.27: To check whether the year is leap or not /* Inputting year is Leap or not */
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf(“Enter year:”);
scanf(“%d”,&year);
if(year%4==0)
printf(“Leap year”);
if(year%4!=0)
printf(“Not leap year”);
getch();
}
Output 1 Enter year:1990 Not leap year
Output 2 Enter year:1996 Leap year
Prog.28: To find the biggest number in two variables /* Biggest number in two variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b;
clrscr();
printf(“Enter a,b values:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is big”);
if(a<b)
printf(“b is big”);
getch();
}
Output 1 Enter a,b values:3 4 b is big
Output 2 Enter a,b values:4 2 a is big
Prog.29: To check the enter number is single digit or not /* Single digit or not */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n<=9)
printf(“Single digit”);
if(n>9)
printf(“Not single digit”);
getch();
}
Output 1 Enter a number:5 Single digit
Output 2
Enter a number:12 Not single digit
Prog.30: To check the number is positive, negative or zero.
/* To check whether +ve, -ve or zero */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n>0)
printf(“+ve”);
if(n<0)
printf(“-ve”);
if(n==0)
printf(“zero”);
getch();
}
Output 1 Enter a number: -2 -ve
Output 2 Enter a number:0 zero
Output 3 Enter a number:5 +ve
Prog.31: Find the highest number in three variables.
/* Biggest number in 3 variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b,c;
clrscr();
printf(“Enter a,b,c values:”);
scanf(“%d%d%d”,&a,&b&c);
if(a>b && a>c) printf(“a is big”);
if(b>a && b>c) printf(“b is big”);
if(c>a && c>b) printf(“c is big”);
getch();
}
Output 1
Enter a,b,c values:3 5 7 c is big
Output 2
Enter a,b,c values:8 4 6 a is big
Output 3
Enter a,b,c values:4 9 1 b is big
If-else Statement
Prog.32: To check the enter number is single digit or not /* Single digit or not */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n<=9)
printf(“Single digit”);
else
printf(“Not single digit”);
getch();
}
Output 1 Enter a number:5 Single digit
Output 2 Enter a number:12 Not single digit
Prog.33: To find the biggest number in two variables /* Biggest number in two variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b;
clrscr();
printf(“Enter a,b values:”);
scanf(“%d%d”,&a,&b);
if(a>b)
printf(“a is big”);
else
printf(“b is big”);
getch();
}
Output 1 Enter a,b values:3 4 b is big
Output 2 Enter a,b values:4 2 a is big
Prog.34: To check whether the year is leap or not /* Inputting year is Leap or not */
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf(“Enter year:”);
scanf(“%d”,&year);
if(year%4==0)
printf(“Leap year”);
else
printf(“Not leap year”);
getch();
}
Output 1 Enter year:1990 Not leap year
Output 2 Enter year:1996 Leap year
Prog.35: To check whether the character is vowel or consonant.
/* Vowel or Consonant */
#include<stdio.h>
#include<conio.h>
void main() {
char x;
clrscr();
printf(“Enter a letter:”);
scanf(“%c”,&x);
if(x==‟a‟ || x==‟A‟ || x==‟e‟ || x==‟E‟ || x==‟i‟ ||
x==‟I‟ || x==‟o‟ || x==‟O‟ || x==‟u‟ || x==‟U‟)
printf(“It is Vowel”);
else
printf(“It is Consonant”);
getch();
}
Output 1 Enter a letter:b It is Consonant
Output 2 Enter a letter:u It is Vowel
If-else if-else statement
Prog.36: Find the highest number in three variables.
/* Biggest number in 3 variables */
#include<stdio.h>
#include<conio.h>
void main() {
int a,b,c;
clrscr();
printf(“Enter a,b,c values:”);
scanf(“%d%d%d”,&a,&b&c);
if(a>b && a>c) printf(“a is big”);
else if(b>a && b>c) printf(“b is big”);
else
printf(“c is big”);
getch();
}
Output 1
Enter a,b,c values:3 5 7 c is big
Output 2
Enter a,b,c values:8 4 6 a is big
Output 3
Enter a,b,c values:4 9 1 b is big
Prog.37: To check the number is positive, negative or zero.
/* To check whether +ve, -ve or zero */
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&n);
if(n>0)
printf(“+ve”);
else if(n<0) printf(“-ve”);
else
printf(“zero”);
getch();
}
Output 1 Enter a number: -2 -ve
Output 2 Enter a number:0 zero
Output 3 Enter a number:5 +ve
Prog.38:To calculate the grade of a student.
/* Grade of a student */
#include<stdio.h>
#include<conio.h>
void main() {
int stuno,sub1,sub2,sub3;
float tot,avg;
clrscr( );
printf(“Enter stuno:”);
scanf(“%d”,&stuno);
printf(“Enter sub1:”);
scanf(“%d”,&sub1);
printf(“Enter sub2:”);
scanf(“%d”,&sub2);
printf(“Enter sub3:”);
scanf(“%d”,&sub3);
tot = sub1 + sub2 + sub3;
avg = tot/3;
printf(“\nTotal=%.2f”,tot);
printf(“\nAverage=%.2f”,avg);
if(avg>=60)
printf(“\nFirst Class”);
else if(avg>=50)
printf(“\nSecond Class”);
Prog.39:To calculate the grade of a student if passed.
/* Grade of a student if passed*/
if(sub1>=35 && sub2>=35 && sub3>=35) {
if(avg>=60)
printf(“\nResult:First Class”);
else if(avg>=50)
printf(“\nResult:Second Class”);
Prog.40:To print the Result of a student.
/* Result of a student*/
if(sub1>=35 && sub2>=35 && sub3>=35) {
if(avg>=60)
printf(“\nResult:First Class”);
else if(avg>=50)
if(sub1<35 && sub2>=35 && sub3>=35) printf(“\nResult:Fail in Sub1”);
else if(sub1>=35 && sub2<35 && sub3>=35) printf(“\nResult:Fail in Sub2”);
else if(sub1>=35 && sub2>=35 && sub3<35) printf(“\nResult:Fail in Sub3”);
else if(sub1<35 && sub2<35 && sub3>=35) printf(“\nResult:Fail in Sub1 & Sub2”);
else if(sub1<35 && sub2>=35 && sub3<35) printf(“\nResult:Fail in Sub1 & Sub3”);
else if(sub1>=35 && sub2<35 && sub3<35) printf(“\nResult:Fail in Sub2 & Sub3”);
else
printf(“\nResult:Fail in all subjects”);
}
Result:Fail in Sub1 Output 3
Result:Fail in Sub2
Prog.41:To check whether letter is small, capital, digit or special symbol
/* Small, Capital, Digit or Special Symbol */
#include<stdio.h>
#include<conio.h>
void main() {
char x;
clrscr();
printf(“Enter a letter:”);
scanf(“%c”,&x);
if(x>=‟a‟ && x<=‟z‟) printf(“Small letter”);
else if(x>=‟A‟ && x<=‟Z‟) printf(“Capital letter”);
else if(x>=‟0‟ && x<=‟9‟) printf(“Digit”);
else
printf(“Special Symbol”);
getch();
}
Output 1 Enter a letter:a Small letter
Output 2 Enter a letter:C Capital letter
Output 3 Enter a letter:6 Digit
Output 4 Enter a letter:#
Special Symbol
Switch statement
Prog.42: To check whether the letter is vowel or consonant /* Vowel or Consonant */
#include<stdio.h>
#include<conio.h>
void main() {
char x;
clrscr();
printf(“Enter a char:”);
scanf(“%c”,&x);
switch(x)
{
case „a‟:
case „A‟:
case „e‟:
case „E‟:
case „i‟:
case „I‟:
case „o‟:
case „O‟:
case „u‟:
case „U‟:printf(“Vowel”);
break;
default:printf(“Consonant”);
}
getch();
}
Output 1 Enter a char:a Vowel
Output 2 Enter a char:b Consonant
Output 3 Enter a char:U Vowel
Prog.43: To print words corresponding numbers below 9 /* Numbers -> words (0-9)*/
#include<stdio.h>
#include<conio.h>
void main() {
int n;
clrscr();
printf(“Enter a number(0-9):”);
scanf(“%d”,&n);
switch(n) {
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;
default:printf(“More than 9”);
}
getch();
}
Output 1 Enter a number(0-9):3 Three
Output 2 Enter a number(0-9):12 More than 9
Prog.44:To print Day corresponding Number /* Number corresponding Day */
#include<stdio.h>
printf(“Enter a number:”);
scanf(“%d”,&n);
default:printf(“No week day”);
}
getch();
}
Output 1 Enter a number:3 Tuesday
Output 2 Enter a number:9 No week day
Prog.45: To print color name corresponding letter /* Letter -> color name */
#include<stdio.h>
#include<conio.h>
void main() {
char x;
clrscr();
printf(“Enter a char:”);
scanf(“%c”,&x);
switch(x) {
case „w‟:printf(“w->white”);
break;
case „b‟:printf(“b->black”);
break;
case „l‟:printf(“l->blue”);
break;
case „y‟:printf(“y->yellow”);
break;
case „g‟:printf(“g->green”);
break;
case „r‟:printf(“r->red”);
break;
case „o‟:printf(“o->orange”);
break;
default:printf(“No color”);
}
getch();
}
Output 1 Enter a char:l l->blue
Output 2
Enter a char:y y->yellow Output 3
Enter a char:c No color
The exit( ) Function
The exit( ) is a function in the standard library of C. This function causes immediate termination of the program and execution control return to the operating system. In general, the termination to exit( ) function is 0 to indicate that termination is normal. Other arguments may be used to indicate some sort of an error.
Prog.46: To calculate Arithmetic operations depends on user choice.
/* Arithmetic operation depends on user choice*/
#include<stdio.h>
#include<conio.h>
void main() {
int a,b,choice;
clrscr();
printf(“Welcome to Arithmetic operations”);
printf(“\n---\n”);
printf(“\n\t1.Add”);
printf(“\n\t2.Sub”);
printf(“\n\t3.Mul”);
printf(“\n\t4.Div(quo)”);
printf(“\n\t5.Rem”);
printf(“\n---\n”);
printf(“\nEnter U r choice here:”);
scanf(“%d”,&choice);
printf(“Enter 2nos:”);
scanf(“%d%d”,&a,&b);
switch(choice) {
case 1:printf(“\nThe sum is:%d”,a+b);
break;
case 2:printf(“\nThe sub is:%d”,a-b);
break;
case 3:printf(“\nThe Product is:%d”,a*b);
break;
case 4:printf(“\nThe quo is:%d”,a/b);
break;
case 5:printf(“\nThe rem is:%d”,a%b);
break;
default:exit(0);
}
getch();
}
Output 1
Welcome to Arithmetic operations Enter U r choice here:1
Enter 2nos:5 4 The sum is:9
Output 2
Welcome to Arithmetic operations --- Enter U r choice here:6
Enter 2nos:3 4
Prog.47: To calculate Arithmetic operations depends on arithmetic operator
/* Arithmetic operations depends on arithmetic operator */
#include<stdio.h>
printf(“Enter any arithmetic operator(+,-,*,/,%):”);
scanf(“%c”,&ch);
printf(“Enter 2nos:”);
scanf(“%d%d”,&a,&b);
switch(ch) {
case „+‟:printf(“\nThe sum is:%d”,a+b);
break;
case „-„:printf(“\nThe sub is:%d”,a-b);
break;
case „*‟:printf(“\nThe Product is:%d”,a*b);
break;
case „/‟:printf(“\nThe quo is:%d”,a/b);
break;
case „%‟:printf(“\nThe rem is:%d”,a%b);
break;
default:exit(0);
}
getch();
}
Output 1
Enter any arithmetic operator(+,-,*,/,%):*
Enter 2nos:4 5
The Product is:20
x
-3. LOOPS
3.1 Def: A portion of program that is executed repeatedly is called a loop.
The C programming language contains three different program statements for program looping. They are
1. For loop