Solution Lab - 04
Solution Lab - 04
LAB ACTIVITIES
4.2.1. Program No 1
Write a program to input two numbers, and check if the inputted second number is the square of
first or not. (i.e., a2 = b)
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{ int a, b;
if(a*a==b)
{ cout<<"The second number is the square of first"<<endl;
}
getch();
}
4.2.2. Program No 2
Write a program that input three numbers, evaluate their average. If the average is greater than
80, print “You are above average”.
#include<iostream>
using namespace std;
int main()
{ int a, b, c;
cout<<"Enter first number";
cin>>a;
int avg;
avg=(a+b+c)/3;
4.2.3. Program No 3
Write a program that finds largest of three inputted numbers.
#include<iostream>
using namespace std;
int main()
{ int a, b, c, max;
cout<<"Enter first number";
cin>>a;
max=a;
if(b>max)
{ max=b;
}
if(c>max)
max=c;
cout<<"The maximum number is: " <<max;
}
4.2.4. Program No 4
Write a program that inputs a number, and finds whether it’s even or odd.
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{ int a;
if(a/2==0)
{ cout<<"The number is even"<<endl;
}
else
{ cout<<"The number is odd";
}
getch();
}
4.2.5. Program No 5
Write a program that inputs salary and grade. It adds 50% bonus if grade is greater than 15. It
adds 25% bonus if grade is 15 or less, and then displays the total salary.
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{ int a,b,c;
if(b>15)
{ c=a+(a*0.5);
cout<<"Now your salary is"<< c<<endl;
}
else if(b<=15)
{ c=a+(a*0.25);
cout<<"Now after increment your salary is "<<c;
}
getch();
}
4.2.6. Program No 6
Write a program that inputs test marks of a student and displays his grade on the following
criteria:
Marks Grade
>=90 A
80 – 89 B
70 – 79 C
60 – 69 D
Below 60 F
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{ int a;
if(a>=90)
{
cout<<"Your grade is A"<<endl;
}
else if(a>=80)
{
cout<<"Your grade is B"<<endl;
}
else if(a>=70)
{
cout<<"Your grade is C"<<endl;
}
else if(a>=60)
{
cout<<"Your grade is D"<<endl;
}
else
{
cout<<"Your grade is F"<<endl;
}
getch();
}
4.2.7. Program No 7
Write a program that inputs two numbers, and a character operator (+,-,*, /), and perform the
specified operation on numbers.
#include<iostream>
#include <conio.h>
using namespace std;
int main()
{ int a,b;
char c;
if(c=='+')
{
cout<<"The addition of numbers is: "<<a+b<<endl;
}
else if(c=='-')
{
cout<<"The subtraction of numbers is: "<<a-b<<endl;
}
else if(c=='*')
{
cout<<"The multiplication of numbers is"<<a*b<<endl;
}
else if(c=='/')
{
cout<<"The division of numbers is: "<<a/b<<endl;
}
else
{
cout<<"You have entered wrong character"<<endl;
}
getch();
}
4.2.8. Program No 8
Write a program that inputs 3 numbers and displays the smallest number using nested if
statement.
#include<iostream>
using namespace std;
int main()
{ int a, b, c;
cout<<"Enter first number";
cin>>a;
if(a<b)
{
if(a<c)
cout<<a<<" is the smallest number";
else
cout<<c<<" is the smallest number";
}
Else
{
if(b<c)
cout<<b <<" is the smallest number";
else
cout<<c<<" is the smallest number";
}
}
4.2.9. Program No 9
Write a program that inputs three numbers, and check whether they are equal or not.
#include<iostream>
using namespace std;
int main()
{ int a, b, c;
cout<<"Enter first number";
cin>>a;
if(a==b)
{
if(a==c)
cout<<"All numbers are equal";
else
cout<<"Numbers are different";
}
else
3.8.10. Program No 10
Write a program in C++ that takes a character from the user and tells whether it is a ‘SMALL
CASE’ or ‘CAPITAL CASE’ letter using nested if-else statement only.
#include<iostream>
int main()
char ch;
if (ch>='a')
if(ch<='z')
else if(ch>='A')
if(ch<='Z')
else