Manual # 2

You might also like

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

Manual # 2

Topic:
Basic C++ Programs including usage of
Formulae

Subject:
Introduction to Computing

Author:
Engr. Ali Faisal Murtaza

1
1. Declaration of Variable of Data Type (Integer)

(a). Assigning a Fixed Value to Variable

#include <iostream.h>

int main()
{
int length;

length = 7;

cout<<"The Length is: ";


cout<<length;

cout<<"\n\n";

return 0;
}

(b). Assign the Value to Variable through Keyboard


Remember to take value from keyboard, we use cin with operator >>.

#include <iostream.h>

int main()
{
int length;

cin>>length;

cout<<"The Length is: ";


cout<<length;

cout<<"\n\n";

return 0;
}

2. Calculate the Area of the Box

#include <iostream.h>

int main()
{
int length;

2
int width;

length=7;
width=5;

cout<<"The area is: ";


cout<<length*width;

cout<<"\n\n";

return 0;
}

// The above program can also be written as:

#include <iostream.h>

int main()
{
int length, width, area; //you can declare the variable in one line.

length=7;
width=5;

area = length * width; //you can store the result of multiplication in another
//variable
cout<<"The area is: ";
cout<<area;

cout<<"\n\n";

return 0;
}

// Program should take the value of length and width from User

#include <iostream.h>

int main()
{
int length, width, area; //you can declare the variable in one line.

cin>>length;
cin>>width;

area = length * width;

3
cout<<"The area is: ";
cout<<area;

cout<<"\n\n";

return 0;
}

// You can take the value from User in one line as Well.

#include <iostream.h>

int main()
{
int length, width, area; //you can declare the variable in one line.

cin>>length>>width;

area = length * width;


cout<<"The area is: ";
cout<<area;

cout<<"\n\n";

return 0;
}

// Now, from here on you should write your program in such a way, that it will give the
proper information.

#include <iostream.h>

int main()
{
int length, width, area; //you can declare the variable in one line.

cout<<"Enter the Length: ";


cin>>length;

cout<<"Enter the Width: ";


cin>>width;

area = length * width;


cout<<"The area is: ";
cout<<area;

4
cout<<"\n\n";

return 0;
}

-----------------------------------------------------------------------------------------------------------
-
Q1: Write a program which will calculate the Volume of the Box. The program should be
able to take values of length, width and height of the box from the user and then display
the result.
-----------------------------------------------------------------------------------------------------------
-

Declaration of Variable of Data Type (Float)

What is the difference of float data type from integer data type??

#include <iostream.h>

int main()
{
int a, b, c;

a=10;
b=3;
c=a/b;
cout<<"Answer is: ";
cout<<c;

cout<<"\n\n";

return 0;
}
// The output of this program is 3, but actually 10/3 = 3.33333, but whenever fraction
comes the int variable always takes the lower value like 10/6=1.66667 but the int variable
will not convert it to 2 it will take the lower value i.e. 1 and excludes the fraction.

So, for that the variable can be declared with float.

#include <iostream.h>

int main()
{
float a, b, c;

a=10;

5
b=3;
c=a/b;
cout<<"Answer is: ";
cout<<c;

cout<<"\n\n";

return 0;
}
//Answer is 3.3333, the float variable will show the result upto five decimals places.

-----------------------------------------------------------------------------------------------------------
-
Q2: Write a program which will convert the feet into meters. The program should be able
to take the feet value from the user and then converts it into meter.
Forumla is: meters = feet / 3.28
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-
Q3: Write a program which will calculate the value a from the following formulae:
a = p ( 1 + k)^r, where k = 10% of p.
Enter the value of p and r from the user.
-----------------------------------------------------------------------------------------------------------

Casting: (Leave it: We`ill Discuss it Later)

#include <iostream.h>

int main()
{
int a, b, c;

a=10;
b=3;
c=a/b;
cout<<"Answer is: ";
cout<<(float) c;

cout<<"\n";

cout<<"Answer is: ";


cout<<(float) a/b;

cout<<"\n\n";

return 0;

6
}

-----------------------------------------------------------------------------------------------------------
-
Q4: Calculate the following equation through C++ program:

-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-
Q5: Write a program for the calculation of the roots of Quardatic Equation. The program
should be designed in such a way that user should be able to enter the values of a, b, & c.
And the formula to calculate the roots of Quardatic Equation:

-----------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------------------
-
Q6: One large chemical company pays its salesperson on a commission basis. The
company pays salesman $200 per week plus 9 percent of his gross sale of that week.
For example, a salesperson sales $5000 worth of chemicals in a week, so company
pays him $200 + 9% of $5000 = $650.
Write a program for the company which will take the value from the user of total
worth in dollars sailed by the salesperson in one week and calculate his total earning of
that week.
-----------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------------------
-
Q7: A company pays Rs: 10,000 per month to its employee. If he worked extra hours
then the company paid Rs 35 for each extra hour.
Write a program which will take the total number of extra hours of a month from the
user and then calculate the total earning of an employee of that month.
-----------------------------------------------------------------------------------------------------------

If Statements

7
#include <iostream.h>
#include<math.h>

int main()
{
char ch;

cin>>ch;

if(ch=='a')
{
cout<<"Great";
}

cout<<"\n\n";

return 0;
}
-----------------------------------------------------------------------------------------------------------
-
-----------------------------------------------------------------------------------------------------------
-
Q8: Write a program using if statement that if the user entered the odd value then the
program should tell that the value entered is odd and if the value entered is even then the
program tells that it is an odd value.
Hint: Use two if statements
-----------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
-
Q9: Write a program using if statement that if the user entered the negative value the
program should tell that the user entered the negative value and if the user entered the
positive value the program should not display any thing.
Hint: Use abs command.
-----------------------------------------------------------------------------------------------------------

You might also like