C Lab Work - 2
Theory of Branching Statements
Introduction to Selection Statement
In most of the programs, the program statements were executed in the same order in which they are written. Each instruction was executed only once. This is not enough in programming. Sometimes we may have to execute program statements based on the given condition, sometimes we may have to execute program statements repeatedly, sometime we may have to choose an option and perform the task accordingly. To carry out all these tasks and other similar tasks, program statements must be executed in a controlled way and it can be done using control structure.Control structure is also known as control statement. It is defined as the group of statements or function that helps to control the flow of instructions in a program. It allows programmer to control the flow of program statements execution in a program. It also specifies the order of statements in program. If there are no control statements, the statements are executed in sequential manner, commonly known as sequential statements.
Mainly controls structures are classified into the following three categories:
- Branching Statements(Selective)
- Looping Statements(Iterative or Repetitive)
- Jumping Statements(Unconditional)
Branching Statements
In branching control structure, selection is made on the basis of condition. We have options to go when the given condition is true or false. The flow of program statement execution is totally directed by the result obtained from the checking condition. Hence, program statements using selective control structures are also called conditional Statements. This type of control structure is mainly used for decision making. It can mainly be categorized into two types.- Conditional Branching Statement
- Switch Case Statement
Conditional Statement
It is the most common decision making control structure which controls the flow of program statement execution based on the condition checked. It can be used in different forms as:- if statement
- if else statement
- if else if statement (if-else ladder)
- nested if else statement
if statement
It is the simplest form of conditional statement in which statements are executed if the test expression (condition) is true. When the condition is false there is no option to go within this structure; in such a situation control must out from the structure and statements outside this structure will be executed.Syntax for if statement
if(condition){
statements;
}
Braces are optional for only one statement as shown below:
if(condition)
statements;
Flowchart for if statement
if else statement
It is another form of selective control structure which can handle both expected as well as unexpected situation. In this control structure, statements written in the body part of if are executed if the condition is true otherwise statements written in body part of else are executed. This is appropriate when we must check only one condition.Syntax of if else statement:
if(condition)statement1;
else
statement2;
In this case if the condition is true then statement1 is executed otherwise statement2 is executed. The else portion of the if-else statement is optional.
Flowchart of if else statement:
if else if statement (if-else ladder)
When we have two or more conditions to be checked in a series, we can use if-else if statement. It is also known as multiple conditional statement / multipath conditional statement / if-else ladder.Syntax of if-else if statement
if(condition1)statement1;
else if(condition2)
statement 2;
...............
...............
else if(condition n-1)
statement n-1;
else
statement n;
In this statement, conditions are checked from the top of the ladder to downwards. As soon as the true condition is found, the statement associated with it are executed and the control is transferred to outside of the ladder skipping the remaining part of the ladder. When most of the conditions (condition1 to condition n-1) are evaluated false then statements associated with the else part will be executed.
Nested if else statement
An entire if else statement written within the body of if part or else part of another if else statement is called nested if else statement. It is used when a condition is to be checked inside another condition at a time in the same program to make a decision.Syntax of if-else if statement:
if(condition1){
if(condition2)
statement1;
else
statement2;
}
else
statement3;
Switch Case Statement
C switch case statement is a multipath decision-making statement that allows selection and execution of a particular block of statements from several blocks of statement based upon the value of expression which is included within the switch statement and branches accordingly . The expression must be of an integral value or character constant. The same task can be performed using an if-else ladder as well but as the number of alternatives increases, the selection process becomes more complex (more time consuming).The main difference between if else ladder and switch statement is - In if-else ladder selection of appropriate option is made in serial fashion whereas in switch-case statement it is done in parallel fashion. So switch statement is much faster than if-else ladder.
The switch statement body consists of a series of case labels and an optional default label. The default label can appear only once and anywhere in the body of the switch statement.
{
case constant 1:
statement 1;
break;
case constant 2:
statement 2;
break;
.......
case constant n:
statement n;
break;
default:
statement;
}
- When the switch statement is executed, the value of expression is compared with the value of case constant (constant 1 , constant 2...)
- Then the block of statements associated with the case whose value is matched with expression will be executed.
- break statement at the end of block indicates the end of the particular case and cause an exit from switch statement and control will be transferred to statements following with switch.
- If none of the case match with the value of the expression, then the block of statement under default is executed.
1. Write a C program to calculate area and circumference of a real circle.(Hint: real circle is a circle having positive radius).
Algorithm to find area and circumference of a real circle
Step 1: StartStep 2: Input radius as r
Step 3: Check r>0?
3.1 If yes, calculate area and circumference and display it.
3.2 If no, display “This is not a real circle”.
Step 4: Stop
Flowchart to find area and circumference of a real circle
C Code to find area and circumference of a real circle
#include <stdio.h>main()
{
float r,a,c;
printf("Enter radius : ");
scanf("%f",&r);
if(r>0)
{
a=3.14*r*r;
c=2*3.14*r;
printf("Area = %0.2f and Circumference = %0.2f",a,c);
}
else
printf("Sorry, given circle is not a real circle");
}
Output:
Enter radius : 7Area = 153.86 and Circumference = 43.96
Enter radius : -2
Sorry, given circle is not a real circle
2. Write a program in C to check whether the given number is odd or even.
Algorithm to check odd or even
Step 1: StartStep 2: Input number as num
Step 3: Calculate remainder by dividing num by 2
Step 4: Check is rem equal to zero?
4.1 If yes, Display “given number is even”
4.2 If no, Display “given number is odd”
Step 5: Stop
Flowchart to check odd or even
C Code to check odd or even
#include <stdio.h>main()
{
int num,rem;
printf("Enter any number : ");
scanf("%d",&num);
rem=num%2;
if(rem==0)
printf("%d is even number.",num);
else
printf("%d is odd number.",num);
}
Output:
Enter any number : 77 is odd number.
Enter any number : 92
92 is even number.
3. Write a C program to input a number and check whether it is positive, negative or zero.
Algorithm to check whether the given number is positive, negative or zero.
Step 1: StartStep 2: Input a number as num
Step 3: Check is num greater than zero?
3.1 If yes, Display “ given number is positive”
3.2 If no, goto step 4
Step 4: Check is num less than zero?
4.1 If yes, Display “ given number is negative”
4.2 If no, Display “given number is zero”
Step 5: Stop
Flowchart to check whether the given number is positive , negative or zero
C Code to check whether the given number is positive , negative or zero.
#include <stdio.h>main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
if(num>0)
printf("%d is positive number.",num);
else if(num<0)
printf("%d is negative number.",num);
else
printf("Given number is zero.");
}
Output:
Enter any number : 77 is positive number.
Enter any number : -2
-2 is negative number.
Enter any number : 0
Given number is zero.
4. Write a C program that stores cost price(cp) and selling price(sp) and determines whether there is loss or gain.
Algorithm to check loss or gain
Step 1: StartStep 2: Input sp and cp
Step 3: Check is sp greater than cp?
3.1 If yes, calculate gain=sp-cp and display gain
3.2 If no, calculate loss=cp-sp and display loss
Step 4: Stop
Flowchart to check loss or gain
C Code to check loss or gain
#include <stdio.h>main()
{
float sp,cp,gain,loss;
printf("Enter selling price and cost price : ");
scanf("%f%f",&sp,&cp);
if(sp>cp)
{
gain=sp-cp;
printf("Gain = NPR. %0.2f",gain);
}
else
{
loss=cp-sp;
printf("Loss = NPR. %0.2f",loss);
}
}
Output:
Enter selling price and cost price : 1000500
Gain = NPR. 500.00
Enter selling price and cost price : 600
700
Loss = NPR. 100.00
5. Write a C program that stores three different numbers and display the largest and smallest among them.
Algorithm to find the largest and smallest among three different numbers
Step 1: StartStep 2: Input three different numbers as a,b and c
Step 3:Check is a>b and a>c?
3.1 If yes, Display “First number is largest”
3.2 If no, goto step 4
Step 4: Check is b>a and b>c?
4.1 If yes, Display “Second number is largest”
4.2 If no, Display “ Third number is largest”
Step 5: Check is a<b and a<c?
5.1 If yes, Display “First number is smallest”
5.2 If no, goto step 6
Step 6: Check is b<a and b<c?
6.1 If yes, Display “Second number is smallest”
6.2 If no, Display “Third number is smallest”
Step 7: Stop
Flowchart to check the largest and smallest number among three different numbers
C Code to check the largest and smallest number among three different numbers
#include <stdio.h>main()
{
int a,b,c;
printf(" Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf(" %d is largest number\n ",a);
else if(b>a && b>c)
printf(" %d is largest number\n ",b);
else
printf(" %d is largest number\n ",c);
if(a<b && a<c)
printf(" %d is smallest number\n ",a);
else if(b<a && b<c)
printf(" %d is smallest number\n ",b);
else
printf(" %d is smallest number\n ",c);
}
Output:
Enter three numbers : 32
1
3 is largest number
1 is smallest number
Enter three numbers : 1
3
2
3 is largest number
1 is smallest number
Enter three numbers : 1
2
3
3 is largest number
1 is smallest number
6. Write a C program to find the middle number among three different numbers.
Algorithm to find middle number among three different numbers
Step 1: StartStep 2: Input three numbers as a,b and c
Step 3: Check is a<b AND a>c OR a>b AND a<c?
3.1 If yes, Display “First number is middle”
3.2 If no, go to step 4
Step 4: Check is b<a AND b>c OR b>a AND b<c?
4.1 If yes, Display “Second number is middle”
4.2 If no, Display “Third number is middle”
Step 5: Stop
Flowchart to find middle number among three different numbers
C Code to find middle number among three different numbers
#include <stdio.h>main()
{
int a,b,c;
printf("Enter three numbers : ");
scanf("%d%d%d",&a,&b,&c);
if(a<b&&a>c ||a>b&&a<c)
printf("%d is middle number",a);
else if(b<a&&b>c || b>a&&b<c)
printf("%d is middle number",b);
else
printf("%d is middle number",c);
}
Output:
Enter three numbers : 21
3
2 is middle number
Enter three numbers : 2
3
1
2 is middle number
Enter three numbers : 1
2
3
2 is middle number
Enter three numbers : 3
2
1
2 is middle number
Enter three numbers : 1
3
2
2 is middle number
Enter three numbers : 3
1
2
2 is middle number
7. Write a C program in C to calculate and display real roots of quadratic equation. (Hint: Check the condition (b2-4ac)>0 for real root)
Algorithm to calculate and display real roots of quadratic equation
Step 1: StartStep 2: Input the value of a , b and c
Step 3: Check is (b2-4ac)>0?
3.1 If yes, use quadratic formula and display the roots
3.2 If no, Display “Sorry, we can not calculate real root”
Step 4: Stop
Flowchart to calculate and display real roots of quadratic equation
C Code to calculate and display real roots of quadratic equation
#include <stdio.h>#include<math.h>
main()
{
float a,b,c,r1,r2;
printf("Enter value of a , b and c : ");
scanf("%f%f%f",&a,&b,&c);
if((b*b-4*a*c)>0)
{
r1=(-b+sqrt(b*b-4*a*c))/2*a;
r2=(-b-sqrt(b*b-4*a*c))/2*a;
printf(" First root= %0.2f and Second root=%0.2f",r1,r2);
}
else
printf("Sorry, can not calculate real roots");
}
Output:
Enter value of a , b and c : 26
1
First root= -0.71 and Second root=-11.29
Enter value of a , b and c : 5
6
3
Sorry, can not calculate real roots
8. Write a C program to find the commission amount on the basis of sales amount as per the following conditions:
Sales Amount | Commision |
0-1000 | 5% |
1001-2000 | 10% |
> 2000 | 12% |
Algorithm to find commission amount
Step 1: StartStep 2: Input sales amount(sa)
Step 3: Check is sa<1000?
3.1 If yes, provide 5% commission.
3.2 If no, go to step 4
Step 4: Check is sa>1001 and sa<=2000?
4.1 If yes, provide 10% commission.
4.2 If no, provide 12% commission.
Step 5 : Stop
Flowchart to find commission amount
C Code to find commission amount
#include <stdio.h>main()
{
float sa,c;
printf("Enter sales amount : ");
scanf("%f",&sa);
if(sa<1000)
c=sa*5/100;
else if(sa>1001 && sa<=2000)
c=sa*10/100;
else
c=sa*12/100;
printf("Commision amount is %0.2f ",c);
}
Output:
Enter sales amount : 900Commision amount is 45.00
Enter sales amount : 1500
Commision amount is 150.00
Enter sales amount : 2500
Commision amount is 300.00
9. Write an interactive C program that takes length and breadth and performs the following task.
- Area of rectangle
- Perimeter of rectangle
- Exit
Algorithm for menu driven program using switch statement
Step 1: StartStep 2: Input length(l) and breadth(b)
Step 3: Display menu and Enter your choice
Step 4: Check the user’s choice.
4.1 If the user enters (a) calculate the area of the rectangle and display.
4.2 If the user enters (b) calculate the perimeter and display.
4.3 If the user enters (c) exit from the program.
Step 5 : Stop
Flowchart for menu driven program using switch statement
C Code for menu driven program using switch statement
#include<stdio.h>
#include<stdlib.h>
main()
{
int l,b,area,perimeter;
char ch;
printf("Menu\n");
printf("a. Area of rectangle \n");
printf("b. perimeter of rectangle \n");
printf("c. Exit \n");
printf("Enter your choice :");
scanf("%c",&ch);
switch(ch)
{
case 'a':
printf("Enter length and breadth :");
scanf("%d%d",&l,&b);
area=l*b;
printf(" Area of rectangle is %d ",area);
break;
case 'b':
printf("Enter length and breadth :");
scanf("%d%d",&l,&b);
perimeter=2*(l+b);
printf(" Perimeter of rectangle is %d ",perimeter);
break;
case 'c':
printf("Your choice is exit!");
exit(0);
default:
printf("Wrong Choice");
}
}
Output:
Menu a. Area of rectangle b. perimeter of rectangle c. Exit Enter your choice :a Enter length and breadth :25 4 Area of rectangle is 100 ------------------------- Menu a. Area of rectangle b. perimeter of rectangle c. Exit Enter your choice :b Enter length and breadth :7 3 Perimeter of rectangle is 20 ----------------------------- Menu a. Area of rectangle b. perimeter of rectangle c. Exit Enter your choice :c Your choice is exit!10. Write a C program that takes a number less than 10 and displays its multiplication table.
Algorithm for program that takes a number less than 10 and displays its multiplication table.
Step 1: StartStep 2: Input any number(n)
Step 3: Check is n<10?
3.1 If yes,
for(i=1;i<=10;i++)
{
Print num*i
}
3.2 If no,Print “Number is greater than 10”
Step 4 : Stop
Flowchart for program that takes a number less than 10 and displays its multiplication table.
C program that takes a number less than 10 and displays its multiplication table.
#include <stdio.h>main()
{
int n,i;
printf("Enter any number : ");
scanf("%d",&n);
if(n<10)
{
for(i=1;i<=10;i++)
{
printf("%d*%d=%d\n",n,i,n*i);
}
}
else
printf("Given number is greater than 10 ");
}
Output:
Enter any number : 77*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70
---------------------------
Enter any number : 15
Given number is greater than 10