SHIFA TAMEER E MILLAT UNIVERSTY
PROGRAMMING
FUNDAMENTAL
ASSIGNMENT 1
NAME: Omama Hashim
PROGRAM: Bs CYs-Section 2
Submitted to: Sir Amer
Question 1
Code:
#include<iostream>
#include<cmath>
#include<string>
using namespace std;
int main()
{
double u,v;
string str;
cout<<"Line 1: 2 to the power of 6 ="
<<pow(2,6)<<endl;
u=12.5;
v=3.0;
cout<<"Line 4:"<< u <<"to the power of"<< v <<"="
<<pow(u,v)<<endl;
cout<<"Line 5: Square root of 24 = "
<<sqrt(24.0)<<endl;
u=pow(8.0,2.5);
cout<<"Line 7: u="<<u<<endl;
str="Programming with C++";
Page
1
cout<<"Line : Length of str ="
1
<<str.length()<<endl;
return 0;
}
1) What is the output of given program? Place
screenshot.
Output:
Figure 1: Output of Q1
2) What is the purpose of pow() function?
Ans: Pow keyword indicates power. It takes two values as base and
2
Page
3) What is the purpose of sqrt() function?
2
power or exponent. And return the calculated power of base. In the given
program it produced the output of 12.5 to the power of 3.
Ans: Sqrt short for square root. This function is used when we have to
calculate square root of a certain value. The value is written within the
parenthesis after keyword sqrt.
4) What is the purpose of str.length() function?
Ans: This function is used to count the number of letters, numeric or
symbols along with the spaces a string has.
5) What will be the error message if you will be
failed to add cmath header file?
Ans: In absence of cmath header file program will not understand
keywords sqrt and pow used in program and thus we will not be able to
perform mathematical calculations.
The error message will be:
Figure 2 Error message
QUESTION 2
Program that calculates and prints monthly paycheck
for an employee.
3
Code:
Page
#include<iostream>
3
using namespace std;
#include<string>
#include<iomanip>
int main()
{
string name;
float
grossamount,FIT=0.15,statetax=0.035,sstax=0.0575,medicare=0.0275,p
ension=0.05,insurance=75,netpay,total,final;
cout<<"Enter employee name"<<endl;
cin>>name;
cout<<"Enter gross amount"<<endl;
cin>>grossamount;
FIT=grossamount*FIT;
statetax=grossamount*statetax;
sstax=grossamount*sstax;
medicare=grossamount*medicare;
pension=grossamount*pension;
total=FIT+statetax+sstax+medicare+insurance+pension;
netpay=grossamount-total;
4
Page
cout<<name<<"\nGross
Amount:.......$"<<grossamount<<"\nFederal
4
cout<<fixed<<setprecision(2);
Tax:.........$"<<FIT<<"\nState tax:........$"<<statetax<<"\nSocial Security
Tax:.......$"<<sstax<<"\nMedicare/Medicaid
tax:........$"<<medicare<<"\nPension Plan:........$"<<pension<<"\nHealth
Insurance:........$"<<insurance<<"\nNet pay........$"<<netpay;
return 0;
}
Output
Figure 3 Output of q2
QUESTION 3
Program that accepts as input the mass, in grams and
density, in grams per cubic centimeters and output the
volume of object using formula: density=mass/volume.
Format your output to two decimal places.
Code:
Page
5
#include<iostream>
5
#include<iomanip>
using namespace std;
int main()
{
float mass,density,volume;
cout<<"Enter the mass in grams"<<endl;
cin>>mass;
cout<<"Enter the density in grams per cubic centimetres"<<endl;
cin>>density;
volume=mass/density;
cout<<fixed<<setprecision(2);
cout<<"Volume of the given object is = "<<volume<<"cm^3";
return 0;
}
Output:
Page
6
Figure 4 Output of q3
6
Question 4
Program that prompts a user to input a number then
prints the number with message saying whether the
number is positive, negative or zero.
Code:
#include<iostream>
using namespace std;
int main()
{
float num;
cout<<"Enter a number"<<endl;
cin>>num;
if(num>=0)
cout<<"The given number "<<num<<" is positive";
else if(num<=0)
cout<<"The given number "<<num<<" is negative";
else
cout<<"The given number is zero";
return 0;
}
Page
7
Output:
7
Figure 5 Output of q4
Question 5
Program that takes 3 numbers as input and print them in
ascending order
Code:
#include<iostream>
using namespace std;
int main()
{
float num1,num2,num3;
cout<<"Enter the first number"<<endl;
8
Page
cout<<"Enter the second number"<<endl;
8
cin>>num1;
cin>>num2;
cout<<"Enter the third number"<<endl;
cin>>num3;
if((num1>num2)&&(num1>num3)&&(num2>num3))
cout<<"Ascending order of given numbers is
="<<"\n"<<num3<<"\n"<<num2<<"\n"<<num1;
else if((num2>num1)&&(num2>num3)&&(num3>num1))
cout<<"ascending order of given numbers
is"<<"\n"<<num1<<"\n"<<num3<<"\n"<<num2;
else if((num3>num1)&&(num3>num2)&&(num1>num2))
cout<<"ascending order of given numbers
is"<<"\n"<<num2<<"\n"<<num1<<"\n"<<num3;
else if((num3>num1)&&(num3>num2)&&(num2>num1))
cout<<"Ascending order of given numbers
is"<<"\n"<<num1<<"\n"<<num2<<"\n"<<num3;
else if((num2>num1)&&(num2>num3)&&(num1>num3))
cout<<"Ascending order of given numbers
is"<<"\n"<<num3<<"\n"<<num1<<"\n"<<num2;
else if((num1>num3)&&(num1>num2)&&(num3>num2))
cout<<"Ascending order of given numbers
is"<<"\n"<<num2<<"\n"<<num3<<"\n"<<num1;
else
9
cout<<"invalid entry";
Page
return 0;
9
}
Output:
Page
10
Figure 6 Output of q5
10