0% found this document useful (0 votes)
8 views

Lab Report 04

The document discusses conditional statements and their usage. It explains relational expressions, if, if-else and nested if-else statements. It provides examples of programs to calculate net pay using if-else, find roots of quadratic equations using multiple if conditions and identify vowels from input character.

Uploaded by

Hamza Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab Report 04

The document discusses conditional statements and their usage. It explains relational expressions, if, if-else and nested if-else statements. It provides examples of programs to calculate net pay using if-else, find roots of quadratic equations using multiple if conditions and identify vowels from input character.

Uploaded by

Hamza Sohail
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Mechanical Engineering

Lab Report 04
Title:
Conditional Statements
1ST SEMESTER

Submitted To: Engr. Ali Hassan


Class: ME-13 B Group: N/A
Submitted By
Sr.No Name (Roll.no) Data Viva Total
Presentation
1 Muhammad
Usama Tariq
(366451)
Objectives :
1. Learn about different statements and operators.
2. Use if and else statements.
3. Use nested if else statement.

Theory :
Relational Expressions :
It consists of constants, variables and arithmetic operations that
are combined by the rational operator. It is used to find the relation between two
expressions. It returns only one value True or False.
Example:
10 < 6 is rational expression with a False value and here < the rational
operator.
Relational operators:
To specify a relation between two expressions or values.
• < Less than
• > Greater than
• <= Less than or equal to
• >= Greater than or equal to
• == Equal to
• != Not equal to

If Statement :
The if statement is used to ignore or execute the set of statements after
testing a condition.
If the condition is true, the statement written below if statement is executed.
If the condition is false, the succeeding statements are ignored and program moves to the
next statements.

If (condition)
Statement 1;
Statement 2;
If Else Statement :
It is another form of If statement. In this statement, one condition and
two blocks of statements are given. If the condition is True, one block executes and if the
condition is False, the other block of statements is executed.
Sometimes code blocks are also succeed the if statement if the required
block has more than one statement.

If (condition)
Statement 1;
Else
Statement 2;

The Nested-if Statement :


When an If statement is used inside another if statement, then
such a statement is called nested if. It is used for multiway decision making.

If (condition 1)
{
If (condition 2)
{
Statement 2;
}
Statement 3;
}

The Nested If-Else statement :


If an If-Else statement is placed inside another If-Else
structure then it is called nested if-else.

If (condition 1)
Statement 1;
Else if (condition 2)
Statement 2;
Else if (condition 3)
Statement 3;
------------------------------------------------------
------------------------------------------------------
Else if (condition m)
Statement m;
Else
Statement n;

Lab Task :
1. Program to calculate the net amount
2. //Program to calculate the net pay
3. #include<iostream>
4. using namespace std;
5. int main() {
6. float bas,hou,med,con,net;
7. cout<<"Enter the basic Pay ";
8. cin>>bas;
9. if(bas<5000)
10. {
11. med=0.05*bas;
12. con=193;
13. hou=0.45*bas;
14. }
15. else
16. {
17. med=0.02*bas;
18. con=96;
19. hou=0.45*bas;
20. }
21. net=bas+hou+con+med;
22. cout<<"The net pay is "<<net;
23. return 0;
24. }
2. Program using nested if else
//Program by using nested If-Else
#include<iostream>
using namespace std;
int main() {
int a,b;
char op;
cout<<"Enter the 1st integer :";
cin>>a;
cout<<"Enter the 2nd integer :";
cin>>b;
cout<<"Enter the operator :";
cin>>op;
if(op=='+')
cout<<"Addition "<<a+b;
else if(op=='-')
cout<<"Subtraction "<<a-b;
else if(op=='*')
cout<<"Multiplication "<<a*b;
else if(op=='/')
cout<<"Division "<<a/b;
else if(op=='%')
cout<<"Remainder "<<a%b;
else
cout<<"Invalid Input";
return 0;
}

3. Program to show real and imaginary roots


4. //Program to calculate real and imaginary roots
5. #include<iostream>
6. #include<cmath>
7. using namespace std;
8. int main() {
9. float a,b,c,r1,r2,real,imag,disc;
10. //using equation ax^2+bx+c=0
11. cout<<"Enter the value of a :";
12. cin>>a;
13. cout<<"Enter the value of b :";
14. cin>>b;
15. cout<<"Enter the value od c :";
16. cin>>c;
17.
18. disc=b*b-4*a*c;
19. if(disc==0)
20. {
21. cout<<"Roots are real and equal."<<endl;
22. r1=r2=-b/2*a;
23. cout<<"Root-1 :"<<r1<<"\nRoot-2 :"<<r2;
24. }
25. if(disc>0)
26. {
27. cout<<"Roots are real and unequal."<<endl;
28. r1=-b/2*a+sqrt(disc)/2*a;
29. r2=-b/2*a-sqrt(disc)/2*a;
30. cout<<"Root-1 :"<<r1<<"\nRoot-2 :"<<r2;
31. }
32. if(disc<0)
33. {
34. real=-b/2*a;
35. imag=sqrt(-disc)/2*a;
36. cout<<"Roots are imaginary."<<endl;
37. cout<<"Root-1 :"<<real<<"+i("<<imag<<")";
38. cout<<"Root-2 :"<<real<<"-i("<<imag<<")";
39. }
40. return 0;
41. }

Home Task :
1. Program to calculate marks of student and assign grade.
//Program to calculate the relative grade
#include<iostream>
using namespace std;
int main() {
int mar;
cout<<"Muhammad Usama Tariq CMS ID 366451";

cout<<"\nEnter the obtained marks : ";


cin>>mar;
if(mar>=90)
cout<<"You got an A+";
else if(mar>=70&&mar<90)
cout<<"You got an A";
else if(mar>=50&&mar<70)
cout<<"You got a B";
else if(mar<50)
cout<<"You got F ";
return 0;
}

2. Program to identify vowels.


3. //Program to identify vowels
4. #include<iostream>
5. using namespace std;
6. int main() {
7. char vow;
8. cout<<"Enter the alphabet :";
9. cin>>vow;
10. if(vow=='a'||vow=='e'||vow=='i'||vow=='o'||vow=='u')
11. cout<<"The alphabet is a vowel. ";
12. else
13. cout<<"It is not a vowel. ";
14. return 0;
15. }

You might also like