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

Programming Fundamental Lab 5

The document contains 7 programming tasks that involve taking user input and using nested if/else statements to evaluate conditions and output results. The tasks include finding the largest of 3 numbers, determining a student's grade based on marks, identifying vowels, determining the form of water based on temperature, checking for leap years, performing basic calculations, and solving a quadratic equation. Solutions to each task are provided using C++ code with nested if/else statements.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Programming Fundamental Lab 5

The document contains 7 programming tasks that involve taking user input and using nested if/else statements to evaluate conditions and output results. The tasks include finding the largest of 3 numbers, determining a student's grade based on marks, identifying vowels, determining the form of water based on temperature, checking for leap years, performing basic calculations, and solving a quadratic equation. Solutions to each task are provided using C++ code with nested if/else statements.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Page 1 of 8

TASK #1:
Write a program in C++ using nested if-else that take input of three integer’s numbers from
user. Find the largest number among three of them.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
float num1, num2, num3;

cout << "Enter first number: ";


cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Enter third number: ";
cin >> num3;

if (num1 >= num2) {


if (num1 >= num3)
cout << "Largest number: " << num1;
else
cout << "Largest number: " << num3;
}
else {
if (num2 >= num3)
cout << "Largest number: " << num2;
else
cout << "Largest number: " << num3;
}
}
Page 2 of 8

Task #2:
Write a program in C++ using if/else operator with nested statements to find the grade of
a student. The detail is as follow.
grade >= 90 → Grade A
grade >= 80 → Grade B
grade >=70 → Grade C
grade >=60 → Grade D
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter marks of student: "; cin>>marks;
if(marks>=90) cout<<"Grade is: A";
else if(marks>=80) cout<<"Grade is: B";
else if(marks>=70) cout<<"Grade is: C";
else if(marks>=60) cout<<"Grade is: D";
else cout<<"Sorry! try again";
}
Page 3 of 8

TASK #3:
Write a program in C++ to input a single character and print a message― It is vowel" if it
is vowel otherwise print message "It is a "consonant― Use if/else structure and OR (||)
operator only.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
char c;
int LowercaseVowel, UppercaseVowel;

cout << "Enter an alphabet: "; cin >> c;

LowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');


UppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

if (LowercaseVowel || UppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
}
Page 4 of 8

TASK #4:
Make a program in C ++ using nested if-else that tells the form of Water whether it is Ice,
Water or Steam. Display the menu also as under.
HINT: Temperature Less than 0 = ICE
Temperature Greater than 0 & Less than 100 = Water
Temperature Greater than 100 = STEAM
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
int val;
cout<<"Enter value: "; cin>>val;
if(val<0)
cout<<"Form of water is: ICE";
else
if(val>0 && val<100)
cout<<"Form of water is: WATER";
else
if(val>=100)
cout<<"Form of water is: STEAM";
}
Page 5 of 8

TASK #5:
Write a program using nested if-else that take value from user and tells that the year is
leap year or not.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
int year;
cout << "Enter a year: ";
cin >> year;

if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
}
Page 6 of 8

TASK #6:
Make a calculator using nested if-else which takes integer type values from user and also
arithmetic operator from user and performs multiple operations.
SOLUTION:
#include <iostream>
using namespace std;

int main()
{
float x,y;
int a,b;
char op;
cout<<"Enter 1st numbers: "; cin>>x;
cout<<"Enter 2nd numbers: "; cin>>y;
cout<<"Enter any Arithmatic Operator: ";
cin>>op;
a=x; b=y;
if (op=='+') { cout<<"\nAnswer: "; cout<<(x+y); }
else if (op=='-') { cout<<"\nAnswer: "; cout<<(x-y); }
else if (op=='/') { cout<<"\nAnswer: "; cout<<(x/y); }
else if (op=='*') { cout<<"\nAnswer: "; cout<<(x*y); }
else if (op=='%') { cout<<"\nAnswer: "; cout<<(a%b); }
else cout<<"\nInvalid Entry";
}
Page 7 of 8

TASK #7:
Write a C++ program using nested if-else to compute the real roots of the equation:
𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎
SOLUTION:
#include <iostream>
#include <cmath>
using namespace std;

int main() {

float a, b, c, x1, x2, determinant, real, img;


cout << "*** ax2+bx+c=0 ***";
cout << "\nEnter coefficients a, b and c: ";
cin >> a >> b >> c;
determinant = b*b - 4*a*c;

if (determinant > 0) {
x1 = (-b + sqrt(determinant)) / (2*a);
x2 = (-b - sqrt(determinant)) / (2*a);
cout << "\nRoots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (determinant == 0) {
cout << "\nRoots are real and same." << endl;
x1 = (-b + sqrt(determinant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}

else {
real = -b/(2*a);
img =sqrt(-determinant)/(2*a);
cout << "\nRoots are complex and different." << endl;
cout << "x1 = " << real << "+" << img << "i" << endl;
cout << "x2 = " << real << "-" << img << "i" << endl;
}

return 0;
}
Page 8 of 8

Prepared By: Khawar Khalil

You might also like