Programming Fundamental Lab 5
Programming Fundamental Lab 5
Programming Fundamental Lab 5
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;
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;
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() {
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