Assignment
C++
Submitted by: Aurangzaib
Roll No: 5025
Class: BS(HONS)IT 2nd Semester Eve-A
Instructor: Madam Sobia Yaqoob
University of Okara
Problem 1: Write a program that prompts the user to enter an integer. If the number is
a multiple of 5, print HiFive. If the number is divisible by 2, print HiEven.
Solution:
#include<iostream>
using namespace std;
int main()
//initialization of variable
int number=0;
//user input
cout<<"enter an integer number: "<<endl;
cin>>number;
if (number%5==0)
cout<<"hiFive";
if (number%2==0)
cout<<"hiEven";
return 0;
}
Problem 2: Write a program that prompts the user to enter a weight in pounds and
height in inches and display the Body Mass Index. Note that one pound is 0.45359237
kilograms and one inch is 0.0254 meters. Where:
BMI Interpretation
below 16 seriously underweight
16–18 underweight
18–24 normal weight
24–29 overweight
29–35 seriously overweight
above 35 gravely overweight
Solution:
#include<iostream>
using namespace std;
int main()
int height,weight;
float weight_kilograms,height_meter,MBI;
cout<<"enter weight in pounds: "<<endl;
cin>>weight;
cout<<"enter height in inches: "<<endl;
cin>>height;
weight_kilogram=weight*0.45359237;
cout<<"weight_kilograms";
height_meter=height *0.0254;
cout<<"height_meter";
MBI=weight_kilograms/(height_meter)^2
cout<<"MBI";
return 0;
Problem 3: Write program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number. (Note: An
even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd
integer less than or equal to the square root of the number.)
Solution:
#include<iostream>
using namespace std;
int main()
int number,i,count=0;
cout<<"enter a positive number: "<<endl;
cin>>number;
//check for prime number
for (i = 1; i <= number; i++)
if ((number % i) == 0)
count++;
}
if (count == 2)
cout<< number << "is a prime number."<<endl;
else
cout<< number << "is not a prime number."<<endl;
return 0;
Problem 4: Write a program that sorts three integers. The integers are entered from the
user input and
stored in variables num1, num2, and num3, respectively. The program sorts the
numbers so that
num1<=num2<=num3.
Solution:
#include<iostream>
using namespace std;
int main()
int num1,num2,num3,highest ,middle,lowest;
cout<<"enter three numbers: ";
cin>>num1>>num2>>num3;
if(num1>num2)
{
middle=num1;
lowest=num2;
else
middle=num2;
lowest=num1;
if(middle>num3)
highest=middle;
if(lowest>num3)
middle=num3;
else
middle=num3;
else
highest=num3;
}
cout<<"Lowest: "<<lowest<<endl;
cout<<"Highest: "<<highest<<endl;
cout<<"Middle: "<<middle<<endl;
return 0;
Problem 5: Write a program that prompts the user to enter the month and year and
displays the number of
days in the month. For example, if the user entered month 2 and year 2000, the
program should display that
February 2000 has 29 days. If the user entered month 3 and year 2005, the program
should display that
March 2005 has 31 days.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
int y,m,days;
cout<<"Enter a month and year: ";
cin>>m>>y;
if(m==1)
cout<<"January "<<y<<" has 31 days.";
else if(m==2)
if(y%4!=0)
cout<<"February "<<y<<" has 28 days.";
else
cout<<"February "<<y<<" has 29 days.";
else if(m==3)
cout<<"March "<<y<<" has 31 days.";
else if(m==4)
cout<<"April "<<y<<" has 30 days.";
else if(m==5)
cout<<"May "<<y<<" has 31 days.";
else if(m==6)
cout<<"June "<<y<<" has 30 days.";
else if(m==7)
cout<<"July "<<y<<" has 31 days.";
else if(m==8)
cout<<"August "<<y<<" has 31 days.";
else if(m==9)
cout<<"September "<<y<<" has 30 days.";
else if(m==10)
cout<<"October "<<y<<" has 31 days.";
else if(m==11)
cout<<"November "<<y<<" has 30 days.";
else if(m==12)
cout<<"December "<<y<<" has 31 days.";
else
cout<<"The valid input for month is from 1 to 12.";
return 0;