Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

LAB No.

9
IMPLEMENTATION OF CONDITIONAL STATEMENTS IN C++

PRE LAB TASK

Objectives:

 To implement conditional statements in C.

Introduction:

C++ is an object oriented programming (OOP) language, developed by Bjarne


Stroustrup, and is an extension of C language. It is therefore possible to code C++ in a "C
style" or "object-oriented style."

Theory:

IF statement:

if statement is a conditional statement which is used to make decision. It is used when a


single condition is to be checked. A condition is enclosed in if statement which decides the
sequence of execution of instruction. If the condition is true, the statements inside if
statement are executed, otherwise they are skipped.

Syntax of if statement:

if (condition)

statements;

... ... ...}


Flowchart for if statement: (Fig 1)

Fig 1

if ... else statement:

if ... else statement is a two way branching statement. It is similar to if statement but the only
difference is if the condition is false then a different block of statements is executed which is
inside else statement.

Syntax of if...else statement:

if (condition)

statements;

... ... ...}

else

statements;

... ... ...}


Flowchart for if ... else statement: (Fig 2)

Fig 2

Nested else if statement:

if ... else if ... else statement is used for multiple branching. When there are two or more
conditions that needs to be checked to decide which block of statement is to be executed, it is
used. The number of else if statements depends upon the number of conditions to be checked.

Syntax of nested else if statement:

if (condition 1)

statements;

... ... ...

else if (condition 2)

statements;

... ... ...

}
... ... ...

else if (condition n)

statements;

... ... ...

else

statements;

... ... ...

Flowchart for nested else if statement: (Fig 3)

Fig 3
LAB TASKS

1. Write program to check percentage of a student and display pass if it is greater than
40.

#include <iostream>

#include <conio.h>

int main()

int marks;

cout<<"enter your marks: ";

cin>>marks;

if (marks>=40)

cout<<"congratulation: you have passed";

else

cout<<" you have failed";

getch();

2. Write a Program to find whether a year is a leap year or not

#include<iostream.h>

#include<conio.h>

int main()
{

clrscr();

int year;

cout<<"\n enter the year = ";

cin>>year;

if(year%4==0 || year%400==0 || year%100==0)

cout<<"this is a leap year"<<endl;

else

cout<<"this is not a leap year"<<endl;

getch();

3. Write a C++ Program to check whether a given number is Positive,Negative or Zero

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int number;

cout<<"enter a number : ";

cin>>number;

if(number>0)

cout<<"number is positive";

else if(number<0)

 
 
cout<<"number is negative";

else

cout<<"number is zero";

getch();

4. Write a program to find whether a number is even or odd.

#include <iostream>

#include <conio.h>

int main()

int number;

cout << "Enter any integer: ";

cin >> number;

if(number % 2 ==0)

cout << number << " is even number.";

else

cout << number <<" is odd number.";

return 0;

5. Write a program to find when a number is equal to other.

#include <iostream>

#include <conio.h>
void main()

int num1,num2;

cout << "Enter num1 value: ";

cin >> num1;

cout << "Enter num2 value: ";

cin >> num2;

if(num1 = num2)

cout << "thankyou";

else

cout <<" sorry";

getch();

6. Write a program to calculate electricity bill.

#include <iostream>

#include <conio.h>

void main()

int bill,unit;

cout << "enter consumed units: ";

cin >> units;

if(units<300)

 
 
bill=units*2;

cout << "your bill is"<<bill;

else if(units>300&& units<500)

bill=units*5;

cout << "your bill is"<<bill;

getch();

7. Write a Program to check your percent.

#include <iostream>

#include <conio.h>

int main()

float percent;

cout<<"Enter your percentage: ";

cin>>percent;

cout<<"You scored "<<percent<<"%"<<endl;

if (percent>=80)

cout<<"You have passed with distinction";

}
else if (percent>=60)

cout<<"You have passed with first division";

else if (percent>=50)

cout<<"You have passed with second division";

else if (percent>=40)

cout<<"You have passed with third division";

else

cout<<"Sorry: You have failed";

getch();

8. Write a Program to check whether a number is greater than or smaller than other.

#include <iostream>

#include <conio.h>

void main()

int a,b,c;

 
 
cout << "Enter three numbers: ";

cin >>a>>b>>c;

if(a<b)

if(a<c)

cout <<a<< "is smallest number"<<endl;

else

cout <<c<< "is smallest number"<<endl;

if(b<c)

cout <<b<< "is smallest number"<<endl;

else

cout <<c<< "is smallest number "<<endl;

getch();

9. Write program to check whether input alphabet is a vowel or not.

#include <iostream>

#include <conio.h>

void main()

char ch;

cout<<"Enter a character\n";

cin>>ch;

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch ==


'u' || ch == 'U')

cout<<ch<<" is a vowel.\n";

 
 
else

cout<<ch"is not a vowel.\n";

getch();

String:

Strings are objects that represent sequences of characters. The standard string class provides
support for such objects with an interface similar to that of a standard container of bytes, but
adding features specifically designed to operate with strings of single-byte characters.

10. Write a program to display your name.

#include <iostream>

#include <conio.h>

void main()

char name[50];

cout<<”enter your name:”;

cin>>name;

cout<<”your name is:”<<name<<endl;

getch();

} //it will only display the name before space.

IT WILL ONLY DISPLAY YOUR NAME AFTER SPACE ALSO.

#include <iostream>

#include <conio.h>

void main()

{char name[50];

 
 
cout<<”enter your name:”;

cin>>name;

cin.getline(name,50);

cout<<”your name is:”<<name<<endl; getch();}

Another way to define conditional statements is by using Switch Statement.

A Switch Statement allows a variable to be tested for equality against a list of values.
Each value is called a case, and the variable being switched on is checked for each switch
case.

Syntax:

The syntax for a switch statement in C programming language is as follows −

switch(expression) {

case constant-expression:

statement(s);

break; /* optional */

case constant-expression:

statement(s);

break; /* optional */

/* you can have any number of case statements */

default: /* Optional */

statement(s);

The following rules apply to a switch statement:

 
 
1. The expression used in a switch statement must have an integral or enumerated type,
or be of a class type in which the class has a single conversion function to an integral
or enumerated type.

2. You can have any number of case statements within a switch. Each case is followed
by the value to be compared to and a colon.
3. The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements following that
case will execute until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow of control will
fall through to subsequent cases until a break is reached.
7. A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases
is true. No break is needed in the default case.

Flow Diagram: (Fig 1)

Fig 1

 
 
Lab Task

11. Write a program to display days of week using switch case.

#include<iostream>

#include<conio.h>

void main()

int n;

clrscr();

cout<<“enter any number b/w 1 and 7: “;

switch(n)

case 1:

cout<<“sunday is the first day of the week”;

break;

case 2:

cout<<“monday is the second day of the week”;

break;

case 3:

cout<<“tuesday is the third day of the week”;

break;

case 4:

cout<<“wednesday is the fourth day of the week”;

break;

case 5:
cout<<“thursday is the fifth day of the week”;

break;

case 6:

cout<<“friday is the sixth day of the week”;

break;

case 7:

cout<<“saturday is the seventh day of the week”;

break;

default:

cout<<“not a day”;

getch();

12. Write a program to display month name according to the month number using
switch statement.

#include<iostream>

#include<conio.h>

void main()

int n;

clrscr();

cout<<"Month No:-> ";

cin>>n;

switch(n)
{

case 1:

cout<<"January\n";

break;

case 2:

cout<<"February\n";

break;

case 3:

cout<<"March\n";

break;

case 4:

cout<<"April\n";

break;

case 5:

cout<<"MAy\n";

break;

case 6:

cout<<"June\n";

break;

case 7:

cout<<"July\n";

break;

case 8:

cout<<"August\n";
break;

case 9:

cout<<"September\n";

break;

case 10:

cout<<"October\n";

break;

case 11:

cout<<"November\n";

break;

case 12:

cout<<"December\n";

break;

default:

cout<<"invalid Month number\nPlease try again ....\n");

break;

getch(); }

13. Write a program to build simple calculator using switch Statement.

#include <iostream>

#include<conio.h>

int main()

 
 
char oper;

float num1,num2;

cout<<"enter an operator";

cin>>oper;

cout<<"enter two operands: ";

cin>>num1>>num2;

switch(oper)

case '+':

cout<<num1<<" + "<<num2<<" = "<<num1+num2;

break;

case '-':

cout<<num1<<" - "<<num2<<" = "<<num1-num2;

break;

case '*':

cout<<num1<<" * "<<num2<<" = "<<num1*num2;

break;

case '/':

cout<<num1<<" / "<<num2<<" = "<<num1/num2;

break;

default:

cout<<"error! invalid operator ");

break;

 
 
getch();

14. Write a program to check for vowel or consonant using switch case.

#include<conio.h>

#include<iostream.h>

Void main()

char ch;

cout<<"Enter any character from A to Z := ";

cin>>ch;

switch(ch)

case 'A':

cout<<"Vowel";

break;

case 'E':

cout<<"Vowel";

break;

case 'I':

cout<<"Vowel";

break;

case 'O':

cout<<"Vowel";

 
 
break;

case 'U':

cout<<"Vowel";

break;

case 'a':

cout<<"Vowel";

break;

case 'e':

cout<<"Vowel";

break;

case 'i':

cout<<"Vowel";

break;

case 'o':

cout<<"Vowel";

break;

case 'u':

cout<<"Vowel";

break;

default:

cout<<"consonant";

getch();

 
 
LAB SESSION

1. In a company an employee is paid as under:


 If his basic salary is less than Rs. 1500, then HRA = 10% of
basic salary and DA = 90% of basic salary.
 If his salary is either equal to or above Rs. 1500, then HRA =
Rs. 500 and DA = 98% of basic salary.
If the employee's salary is input by the user write a program to find his
gross salary.

2. Write a C++ program to note the nature of roots of Quadratic Equation.

You might also like