Uploaded by Arslan afzal

Fundamentals of Programming Lab Report: If-Else Statements

advertisement
1|Page
NATIONAL UNIVERISTY OF SCIENCE AND TECHNOLOGY (NUST)
SECTOR H-12 ISLAMABAD
SCHOOL OF MECHENICAL AND MANUFACTURING
ENGEENRING(SMME)
NAME: M Arslan Afzal
Cms id: 503076
Class: AE-02-B
Fundamentals of programming
Lab Report# 03
1|Page
2|Page
Table of Contents
Objectives: .................................................................................................................................................... 3
Introduction .................................................................................................................................................. 3
Lab task#01 ................................................................................................................................................... 3
Explain: ...................................................................................................................................................... 3
Code: ......................................................................................................................................................... 3
Output: ...................................................................................................................................................... 4
Lab Task#02................................................................................................................................................... 4
Explanation: .............................................................................................................................................. 4
Code: ......................................................................................................................................................... 5
Result ........................................................................................................................................................ 5
Lab Task# 03 .................................................................................................................................................. 6
Explanation: .............................................................................................................................................. 6
Code: ......................................................................................................................................................... 6
Output: ...................................................................................................................................................... 7
Lab Task# 04 .................................................................................................................................................. 8
Explanation: .............................................................................................................................................. 8
Code: ......................................................................................................................................................... 8
Output: ...................................................................................................................................................... 9
2|Page
3|Page
Objectives:
The lab task is conducted to understand the use of if-else statement in C++
and also used to understand the logics used in the statements for real world problems.
This lab work is done with having objective that we understand the relational opreations
to build conditions.
Introduction:
We understand how blocks are formed in using conditions and relational
operations. We use different operations and selection structures for making decisions or
choosing between the available options. We studied the use of if-else statement in
making decisions.
Lab task#01
Explain:
The program asks the user to input two integers, x and y.
It then checks if x is a multiple of y by using the modulo operator (%). If x % y==0 it
means x is divisible by y.
If the above condition is false, it checks if y is a multiple of x by verifying if y % x == 0.
If neither condition is true, the program prints a message indicating that neither number
is a multiple of the other.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x,y;
cout<<"enter two integer values "<<endl;
cin>>x>>y;
if(x%y==0)
3|Page
4|Page
cout<<x<<"is the multiple of y "<<endl;
else if (y%x==0)
cout<<y<<"is the multiple of x "<<endl;
else
cout<<x<<" "<<y<<"is not multiple of both "<<endl;
}
Output:
If one number is divisible by the other (i.e., remainder is 0), the program prints that it is
a multiple of the other.
If neither number is divisible by the other, the program prints that neither is a multiple
of the other.
Lab Task#02
Explanation:
 The program asks the user to enter three integers and stores them in variables a,
b, and c.
 It determines the largest value using the ternary operator:
 Compares a with b and c, then b with c to find the maximum.
 It determines the smallest value using a similar ternary operation.
4|Page
5|Page
 Finally, it prints the largest and smallest values to the console.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the value of a,b,c "<<endl;
cin>>a>>b>>c;
if(a>b && a>c)
cout<<a<<" "<<"is the largest number"<<endl;
else if(b>a && b>c)
cout<<b<<" "<<"is the largest number"<<endl;
else
cout<<c<<" "<<"is the largest number"<<endl;
}
Result
 The user enters three integers, e.g., 7, 2, and 9.
 The program compares the numbers and finds 9 as the largest.
 It also finds 2 as the smallest.
 The program then prints: "The largest value is 9" and "The smallest value is: 2
5|Page
6|Page
Lab Task# 03
Explanation:
 The user enters three double values representing triangle sides.
 The program stores them in an array and sorts them in ascending order to ensure
the largest side is last (c).
 It applies the Pythagorean theorem:
𝑎2 = 𝑏2 + 𝑐 2
 If true, it confirms the sides form a right-angled triangle.
 Otherwise, they do not form one.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double base,hypotenues,prependicular;
cout<<"enter the value of base"<<endl;
cin>>base;
cout<<"enter the value of prependicular"<<endl;
6|Page
7|Page
cin>>prependicular;
cout<<"enter the value of hypotenues"<<endl;
cin>>hypotenues;
if(pow(hypotenues,2)==pow(base,2)+pow(prependicular,2))
cout<<" the values you have entered are the dimensions of right angle triangle"<<endl;
else
cout<<"the triangle is not right angle triangle "<<endl;
}
Output:
 The user inputs three side lengths of a triangle.
 The program sorts them so that the largest value is treated as the hypotenuse.
 It checks if the Pythagorean theorem holds:
If 𝑎2 = 𝑏2 + 𝑐 2 , it prints "The given sides form a right-angled triangle."
Otherwise, it prints "The given sides do not form a right-angled triangle."
7|Page
8|Page
Lab Task# 04
Explanation:
 The program asks the user to enter their percentage marks.
 It checks the percentage and assigns a letter grade using if-else conditions.
 If the percentage is 90+A, 80-89 B, 70-79 C, 60-69 D, below 60 F.
 Finally, it displays the letter grade on the screen.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double percentage;
cout<<"enter your percentage"<<endl;
cin>>percentage;
if(percentage>=90)
cout<<"your grade is A "<<endl;
else if(percentage>=80 && percentage<90)
cout<<"your grade is B "<<endl;
else if(percentage>=70 && percentage<80)
cout<<"your grade is C "<<endl;
else if(percentage>=60 && percentage<70)
cout<<"your grade is D "<<endl;
else
cout<<"your grade is F "<<endl;
}
8|Page
9|Page
Output:
 The program asks for your percentage marks.
 It checks which grade your marks fall into.
 Based on the marks, it assigns A, B, C, D, or F.
 Finally, it displays your grade on the screen.
9|Page
Download