Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Faculty of Computing

[Programming Fundamentals]
Lab No 5 solution

Course Instructor: Ma’am Mehvish


Lab Instructor: Ma’am Hafsah Mahmood
Task 1: Write a program to check whether triangle is valid or not if sides are taken as input
from user. Use Nested If statements.

SOURCE CODE
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter side a"<<endl;
cin>>a;
cout<<"Enter side b"<<endl;
cin>>b;
cout<<"Enter side c"<<endl;
cin>>c;
if(a+b>c){cout<<"a+b>c"<<endl;
if(a+c>b){
cout<<"a+c>b"<<endl;
if(b+c>a){
cout<<"b+c>a"<<endl;
cout<<"All the three conditions satisfied"<<endl;
cout<<"Triangle is valid"<<endl;
}
}
else{cout<<"All the three conditions do not satisfied"<<endl;
cout<<"Triangle is not valid";
}
}
return 0;
}
OUTPUT

Task 2: Write a Program to find the tallest student among 3 students. Note the heights must be
taken as input from user. Use Nested if statements.
Task 3: Write a C++ program print total number of days in a month using switch case. The
number of the month should be taken as input from user.

Source code
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"Enter month number";
cin>>a;
switch(a)
{case 1:
cout <<"JANUARY has 31 days";
break;
case 2:
cout <<"FEBRUARY has 28 days";
break;
case 3:
cout <<"MARCH has 31 days";
break;
case 4:
cout <<"APRIL has 30 days";
break;
case 5:
cout <<"MAY has 31 days";
break;
case 6:
cout <<"JUNE has 30 days";
break;
case 7:
cout <<"JULY has 31 days";
break;
case 8:
cout <<"AUGUST has 31 days";
break;
case 9:
cout <<"SEPTEMBER has 30 days";
break;
case 10:
cout <<"OCTOBER has 31 days";
break;
case 11:
cout <<"NOVEMBER has 30 days";
break;
case 12:
cout <<"DECEMBER has 31 days";
break;
}
return 0;
}
Output

Task 4: Write program to find all roots of a quadratic equation using switch case. The
coefficients of the equation should be taken from user.
Source code
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c;
int discriminant;
cout<<"number 1 ";
cin>>a;
cout<<"number 2 ";
cin>>b;
cout<<"number 3 ";
cin>>c;
discriminant=b*b-4*a*c;
switch(discriminant)
{case positive dicriminant:
cout <<"discrminant is positive";
break;
case negative discriminant:
cout <<"discriminat is negative";
break;
}
return 0;
}

You might also like