crossorigin="anonymous"> C++

Tasks & Solution of C++ Language


Topic 1: Introduction to C++


Task 1: Find whether the sum of two numbers is greater than 25.

Output:

1.1 image

Source Code

#include iostream
using namespace std;
int main()
{
int a;
int b;
cout << "Enter a value for variable a \n";
cin >> a;
cout << "Enter another value for variable b \n";
cin >> b;
int c = a+b;
if(c > 25){
cout << "Sum of variable a and b is greater than 25";
} else {
cout << "Sum of a and b is less than 25";
}
return 0;
}

Task 2: Find the volume of the rectangular box. Note: volume = length x width x height

Output:

1.2 image

Source Code

#include iostream
using namespace std;
int main()
{
int w;
int h;
int l;
cout << "Enter width of a Rectangular box \n";
cin >> w;
cout << "Enter height of a rectangular box \n";
cin >> h;
cout << "Enter length of a rectangular box \n";
cin >> l;
int V = w * h * l;
cout << "Volume of a rectangular box: " << V;
return 0;
}

Task 3: Find the value of A such that A = (4x – 3y) / 2z

Output:

1.3 image

Source Code

#include iostream
using namespace std;
int main()
{
double x;
double y;
double z;
cout << "Enter a value for variable x \n";
cin >> x;
cout << "Enter a value for variable y \n";
cin >> y;
cout << "Enter a value for variable z \n";
cin >> z;
double A = (4*x-3*y)/2*z;
cout << "A = " << A;
return 0;
}

Task 4: Write a program to display your personal information. (Name, age, address, father's name, college name, NIC, phone number etc.)

Output:

1.4 image

Source Code

#include iostream
using namespace std;
int main()
{
cout << "Name:Muhammad Sharjeel Baig\n";
cout << "Age: 18\n";
cout << "Gulistan-e-johar Karachi Pakistan \n";
cout << "Father's name: Muhammad Mujeeb Baig \n";
cout << "collage name: Government collage hyderabad\n";
cout << "Nic: 413034-205109-3\n";
cout << "Phone Number: +92-3432595361";
return 0;
}

Task 5: Write a program to display your semester courses along with teacher name and credit hour.

Output:

1.5 image

Source Code

#include iostream
namespace std;
int main () {
cout << "-------------------------------------
---------------------------------------------
------------------------" << endl;
cout << "| \t Course Name \t \t \t | \t Teacher name \t \t \t | \t Credit Hour \t |" << endl;
cout << "=================================
=================================
===================================
=====" << endl;
cout << "| Computer Programming \t \t | Engr. Adnan ur Rehman \t \t | 3 + 1 \t \t |" << endl;
cout << "| Computing Fundamentals \t \t | Engr. Mahwish Khan \t \t \t | 2 + 1 \t \t |" << endl;
cout << "| Applied Physics \t \t \t | Engr. Rizwan Iqbal \t \t | 3 + 1 \t \t |" << endl;
cout << "| English I \t \t \t \t | Engr.Bushra fazal \t \t \t | 3 + 0 \t \t |" << endl;
cout << "---------------------------------------
---------------------------------------------
----------------------" << endl;
return 0;}

Topic 2: Variables & Data types


Task 1: Write a C++ program that finds the (add, sub, divide, multiple) two integers. The two integers should be taken as input from the user. At the end, display the numbers that are input by the user and their sum with proper cout statements.

Output:

2.1

Source Code

int main(int argc, char** argv) {
double a;
double b;
double sum;
double sub;
double mul;
double divide;
cout << "Enter value for a\n";
cin >> a;
cout << "Enter value for b\n";
cin >> b;
divide = a / b;
sum = a + b;
mul = a * b;
sub = a -b;
cout << "Result : \n";
cout << "sum : " << sum << endl;
cout << "sub : " << sub << endl;
cout << "mul : " << mul << endl;
cout << "divide : " << divide;
return 0;
}

Task 2: Write a program and print the output of first equation of the motion. For values take input from user.

Output:

2.3

Source Code

int main(int argc, char** argv) {
double vf;
double vi;
double a;
double t;
cout << "Enter value of Vi \n";
cin>> vi;
cout << "Enter value of acceleration \n";
cin >> a;
cout << "Enter value of time \n";
cin >> t;
vf = vi + (a*t);
cout << "Vf = " << vf;
return 0;
}

Task 3: Write a program that prompt user to input course name, obtained marks and total marks. Calculate the percentage using this formula and display the results as follows.
marks percentage = marks obtained / total * 100

Output:

2.3

Source Code

int main()
{
double m1,m2,m3,m4,m5;
double total=500;
double obtainedmarks;
double percentage;
string coursename1,coursename2,
coursename3,coursename4,coursename5;
cout<<"enter coursename"< cin>>coursename1;
cout<<"enter marks"< cin>>m1;
cout<<"enter coursename"< cin>>coursename2;
cout<<"enter marks"< cin>>m2;
cout<<"enter coursename"< cin>>coursename3;
cout<<"enter marks"< cin>>m3;
cout<<"enter coursename"< cin>>coursename4;
cout<<"enter marks"< cin>>m4;
cout<<"enter coursename"< cin>>coursename5;
cout<<"enter marks"< cin>>m5;
obtainedmarks=m1+m2+m3+m4+m5;
percentage=(obtainedmarks/total)*100;
cout<<"percentage : "< }

Task 4: Write a program that finds the value of X by using given formula. Take value of a and b from user.
X= (a + b)2 – 2ab

Output:

2.4

Source Code

int main(int argc, char** argv) {
double a,b,x;
cout << "Enter value of a\n";
cin >> a;
cout << "Enter value of b\n";
cin >> b;
double s = a+b;
double w = pow(2,s);
x = w-2*a*b;
cout << "X: " << x << endl;
return 0;
}

Topic 3: Conditional Statement


Task 1: Make a logic to recognize if a number is even or odd?

Output:

3.1

Source Code

int main()
{
int number;
cout << "Enter a number \n";
cin >> number;
if((number % 2) == 0){
cout << "Even";
} else {
cout << "Odd";
}
return 0;
}

Task 2: Write a program that inputs a character as input and tells whetehr the entered character is vowel or not.

Output:

3.2

Source Code

int main()
{
char character;
cout << "Enter a character \n";
cin >> character;
if(character == 'a'|| character == 'e'|| character == 'i'|| character =='o'|| character =='u'){
cout << "vowel";
} else {
cout << "not a vowel";
}
return 0;
}

Task 3: Write a program in which taking many inputs from user and print smallest input.

Output:



Source Code

int main()
{
int a,b,c;
cout << "Enter value for variable a\n";
cin >> a;
cout << "Enter value for variable b\n";
cin >> b;
cout << "Enter value for variable c\n";
cin >> c;
if(a
cout << "a is smallest";
}else if(b < a&&b cout << "b is smallest";
} else if(c cout << "c is smallest";
}
return 0;
}

Topic 4: Switch Statement & For Loop


Task 1: Write a program in which user input value and you will show name of month.

Output:

4.1

Source Code

int main (){
int month;
cout<<"Enter month: ";
cin>>month;
switch(month){
case 1:
cout<<"Jan"< break;
case 2:
cout<<"Feb"< break;
case 3:
cout<<"Mar"< break;
case 4:
cout<<"Apr"< break;
case 5:
cout<<"May"< break;
case 6:
cout<<"Jun"< break;
case 7:
cout<<"Jul"< break;
case 8:
cout<<"Aug"< break;
case 9:
cout<<"Sep"< break;
case 10:
cout<<"Oct"< break;
case 11:
cout<<"Nov"< break;
case 12:
cout<<"Dec"< break;
default: // default is for when you enter a number out of 1-12 range. for instance, 13
cout<<"invalid input!"< }
return (0);
}

Task 2: Write a program in which take any character and check whether it is digit or not.

Output:

4.2

Source Code

int main()
{
char num;
cout<<"Enter a character:"< cin>>num;
switch(num>='a'&& num<='z'||num>='A'&& num<='Z')
{
case 1:
cout<<"It's alphabet"< break;
case 0:
switch(num>='0' && num<='9')
{
case 1:
cout<<"It's digit"< break;
case 0:
cout<<"It's not alphabet and not digit"< break;
}
break;
}
}

Task 3: Write a program in which take number from user and print sum of all odd numbers until user's number.

Output:

4.3

Source Code

int main()
{
int i, num, sum=0;
cout<<"Enter any number: "< cin>>num;
for(i=1; i<=num; i+=2)
{
sum += i;
}
cout<<"Sum of all odd number between 1 to " << num << ": "< return 0;
}

Task 4: Write a program in which take number from user and print square of all numbers until user's number.

Output:

4.4

Source Code

int main()
{
int i,x,cub;
cout<<"Input number: ";
cin>>x;
for (i=1 ;i<=x; i++)
{
cub= i*i*i;
cout<<"Number is: "< }
return 0;
}

Task 5: Write a program in which take number from user and print table of number.

Output:

4.5

Source Code

int main()
{
int n = 5;
for (int i = 1; i <= 10; ++i) {
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}

Task 6: Write a program in which take numbers from user and print sum of all numbers.

Output:

4.6

Source Code

int main(){
int i;
int positiveNumber = 0;
for (i = 1; i <= 10;i++){
int number;
cout << "Enter a number \n";
cin >> number;
if (number >= 0){
positiveNumber += number;
}
}
cout << "Sum of positive numbers: "<< positiveNumber;
return 0;
}

Topic 5: While & Do-while Loop


Task 1: Write a while loop that counts backwards from 20 to 10

Output:



Source Code

int main()
{ int a=20;
while(a>=10)
{ cout<< "value of a: "< a=a-1;
}
return 0;
}

Task 2: Write a C++ Program that displays a series of alphabets in descending order from ‘Z’ to ‘A

Output:



Source Code

int main()
{ char c= 'Z';
cout<<"---Alphabets in decending order---"<
while(c >='A')
{ cout< c--;
} return 0;
}

Task 3: Write a C ++ program to count number of digits in a number.

Output:



Source Code

int main()
{
int num, tot=0;
cout<<"Enter the Number: ";
cin>>num;
while(num>0)
{ tot++;
num=num/10;
}
cout< cout<<"Total digits are: "< cout< return 0;
}

Task 4: Write a program that takes an integer value and decide if it is even or odd. Continue asking for. Numbers till the user enters a negative value. The program then displays the total number of even and odd integers given. Use do While statement

Output:



Source Code

int main()
{ int num;
int even=0;
int odd=0;
do
{
cout<<"Enter a number \n";
cin>>num;
if(num %2 ==0)
{
cout<<"Number is Even \n";
even++;
}
else
{
cout<<"Number is Odd \n";
odd++;
}
}
while(num>0);
cout<<"Even numbers: "< cout<<"Odd numbers: "< return 0;
}

Task 5: Write a program that inputs an integer larger than 1 and calculates the sum of squares from 1 to that integer. For example, if the integer equals 4, the sum of squares is 30 (1+4+9+16). The output should be the value of the integer and the sum, properly labeled. Continue the program till the user enters a value less than 1 which signals the end of the program. Use a do While statement.

Output:



Source Code

int main(){
int n;
do{
cout << "Enter a value\n";
cin>> n;
int formula = (n * (n + 1) / 2) * (2 * n + 1) / 3;
cout << formula << endl;
} while(n != 1);
return 0;
}

Task 6: Write an exec with a DO WHILE loop that asks passengers on a commuter airline if they want a window seat and keeps track of their responses. The flight has 8 passengers and 4 window seats. Discontinue the loop when all the window seats are taken. After the loop ends, display the number of window seats taken and the number of passengers questioned.

Output:



Source Code

int main()
{
int window_seats=0;
int passenger=0;
char x;
do
{
cout<<"Do you want a window seat press y otherwise n: "< cin>>x;
if(x=='y')
{
window_seats++;
passenger++;
}
else
passenger ++;
}while(passenger<=8&&window_seats<4);
cout<<"total no of window seats"< cout<<"total no of passenger"< return 0;
}

Topic 6: Nested Loop


Task 1: Write a nested if statement to print the appropriate activity depending on the value of a variable temperature and humidity as in the table below: Assume that the temperature can only be warm and cold, and the humidity can only be dry and humid.

Output:

6.1

Source Code

int main() {
string temperature;
cout << "Enter Temperature\n";
cin >> temperature;
string humidity;
cout << "Enter humidity\n";
cin >> humidity;
if(temperature == "warm")
{
if(humidity == "dry"){
cout << "play tennis";
} else if(humidity == "humid"){
cout<<"Swim";
} else {
cout << "Invalid Value";
}
} else if(temperature == "cold"){
if(temperature == "dry"){
cout << "play basketball";
} else if(temperature == "humid"){
cout << "watch tv";
}
} else{
cout << "Invalid Values Entered";
}
return 0;
}

Task 2: Generate Stars using 2 for loops
*
**
***
****
*****
******

Output:

6.2

Source Code

int main()
{
int rows = 6;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}

Task 3: Generate stars using nested for loop?
**********
*********
********
*******
******
*****
****
***
**
*

Output:

6.3

Source Code

int main()
{
int i, j, n;
n=10;
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}

Task 4: Write a c# program using nested loop print chap 1 to chap 5 each chapter contain 5 section.

Output:

6.4

Source Code

int main()
{
for(int i = 1; i <= 5; i++){
cout<< "Chapter " << i< for(int j = 1; j<=5;j++){
cout<< "Section " << j< }
}
return 0;
}

Task 5: Print the time table of 1 to 9 using nested loop.(hint: you need to use an if-else statement to check whether the product is single-digit or double-digit)and print an additional space if needed.
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18

Output:

6.5

Source Code

int main()
{
int i,j,c;
for(i=1; i<=10; i++)
{
for(j=1; j<=9; j++)
{
c=i*j;
cout< }
cout<<"\n";
}
}

Task 6: Write a program in C++ to make such a pattern like a pyramid with an asterisk.

Output:

6.6

Source Code

int main()
{
int i,j,spc,rows,k;
rows = 5;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<"*"<<" ";
cout< spc--;
}
}

Task 7: Array


Task 1: Write a program in C++ to count the positive, negative and zero values present in an array.

Output:

7.1

Source Code

int main(){
int input[100], count, i, nCount=0, pCount=0, zCount=0;
cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
for(i = 0; i < count; i++){
cin >> input[i];
}
for(i = 0; i < count; i++){
if(input[i] < 0) {
nCount++;
} else if(input[i] > 0) {
pCount++;
} else {
zCount++;
}
}
cout << "Negative Numbers : " << nCount << endl;
cout << "Positive Numbers : " << pCount << endl;
cout << "Zeroes : " << zCount << endl;
return 0;
}

Task 2: Write a C program to search an element entered by user from array and display the searched element and its location.
Sample Inputs:

Output:

7.2

Source Code

int main()
{
int array[5];
cout<<"Enter Array Elements:"< for(int i=0; i<=5; i++)
{
cin>>array[i];
}
for(int i=0; i<=5; i++)
{
if(array[i]==6)
{
cout<<"Item found at position "< }
}
return 0;
}

Task 3: Make a program in C++ in which take 5 numbers from user and then give max and min value. Using arrays.

Output:

7.3

Source Code

int main()
{
int array[5];
int i, max, min;
for(i=0; i<5; i++)
cin>>array[i];
max = array[0];
min = array[0];
for(i=1; i<5; i++)
{
if(array[i] > max)
max = array[i];
if(array[i] < min)
min = array[i];
}
cout<<"\nMaximum element =" << max << "\n";
cout<<"Minimum element =" << min;
return 0;
}

Task 4: Write a C++ program that ask user to enter 10 integer values. Store those values in one dimension array. Create another array of same size, and store the values of first array in reverse order.

Output:

7.4

Source Code

int main()
{
int arr[10], i;
cout<<"Enter 10 Array Elements:\n ";
for(i=0; i<10; i++)
cin>>arr[i];
cout<<"The Original Array is: \n";
for(i=0; i<10; i++)
cout< cout<<"The Reverse of Given Array is \n: ";
for(i=(10-1); i>=0; i--)
cout< return 0;
}

Topic 8: Function


Task 1: Create a calculator that takes a number, a basic math operator (+,-,*,/,^), and a second number all from user input, and have it print the result of the mathematical operation. The mathematical operations should be wrapped inside of functions.

Output:



Source Code

double calculateSalary(double grade,double house,double providance){
int netSalary=0;
cout<<"Enter a Basic Salary"< double basicSalary;
cin>>basicSalary;
providance= providance*basicSalary;
house=house*basicSalary;
netSalary=basicSalary+house-providance-grade;
cout<<"your net salary : "< }
int main()
{
double a=1000;
double b=500;
double c=200;
cout<<"Select Grade"< cout<<"A For Grade A"< cout<<"B For Grade B"< cout<<"C For Grade C"< char grade;
cin>>grade;
switch(grade)
{
case 'a':
calculateSalary(a,0.10,0.07);
break;
case 'b' :
calculateSalary(b,(7/100),(7/100));
break;
case 'c':
calculateSalary(c,(5/100),(7/100));
break;
default:
cout<<"Invalid input"< }
return 0;
}

Task 2: Write a program to print the factorial of a number by defining a function named 'Factorial'.
Factorial of any number n is represented by n! And is equal to 1*2*3*....*(n-1)*n. E.g.-
4! = 1*2*3*4 = 24
3! = 3*2*1 = 6
2! = 2*1 = 2
Also,
1! = 1
0! = 0

Output:



Source Code

double calculateSalary(double grade,double house,double providance){
int netSalary=0;
cout<<"Enter a Basic Salary"< double basicSalary;
cin>>basicSalary;
providance= providance*basicSalary;
house=house*basicSalary;
netSalary=basicSalary+house-providance-grade;
cout<<"your net salary : "< }
int main()
{
double a=1000;
double b=500;
double c=200;
cout<<"Select Grade"< cout<<"A For Grade A"< cout<<"B For Grade B"< cout<<"C For Grade C"< char grade;
cin>>grade;
switch(grade)
{
case 'a':
calculateSalary(a,0.10,0.07);
break;
case 'b' :
calculateSalary(b,(7/100),(7/100));
break;
case 'c':
calculateSalary(c,(5/100),(7/100));
break;
default:
cout<<"Invalid input"< }
return 0;
}

Task 3: Write a method table() which generates multiplicative table of an integer. The function receives three integers as its arguments. The first argument determine the table to be generated while the second and the third integer tell the starting and ending point respectively. Ask the user to provide the three integer as input in the main().

Output:



Source Code

void table(int table, int starter, int ender){
for(int i = starter; i <= ender; i++){
cout << table << " x " << i << " = "<< table * i< int main()
{
table(5,1,50);
return 0;}

Task 4: Write a method named square_cube() that computes the square and cube of the value passed to it and display the result. Ask the user to provide the integer input in the main() and then call the function.

Output:

9.2

Source Code

void square_cube(int input){
cout << "Square: " << input*input;
cout << "\nCube: "<< input*input*input;
}
int main()
{
int input;
cout << "Enter an input\n";
cin >> input;
square_cube(input);
return 0;
}

Task 5: Write a program in which swap two numbers.(call by value and call by reference )

Output:

9.3

Source Code

void swap(int num1,int num2){
int reserved;
reserved = num1;
num1 = num2;
num2 = reserved;
cout << "number 1:
"<< num1< cout << "number 2:
"<< num2 << endl;
}
int main()
{
int num1,num2;
cout << "Enter number 1: ";
cin >> num1;
cout << "Enter number 2: ";
cin >> num2;
swap(num1,num2);
return 0;
}

Task 6: Write a program in C++ that receives temperature in degree Fahrenheit and prints its equivalent value in degree using function Celsius call by value .

Output:

9.4

Source Code

double celsius(double temp){
double fereinheight = (temp - 32) * 5/9;
return fereinheight;
}
int main()
{
double fereinheight;
cout << "Enter Fereignheight\n";
cin >> fereinheight;
cout << celsius(fereinheight);
return 0;
}

Task 7: Write a C++ program to find the grade of student using function (pass by reference) Whether the student should Re-sit or Redo depending on the following criteria. 33% or more on probation in exam Less than 33% repeat course.
Program should prompt user for the number of subjects for which he/she wants to calculate the grade 2 or 3.
calcourse(s1,s2) // for two subjects.
calcourse(s1,s2,s3) // for three subjects.
calgrades():
These functions determine the grade of student on following:
A grade: 87 - 100
B grade: 75 – 86
C grade: 65 – 74
D grade: 50 – 64
F grade: < 50

Output:

9.5

Source Code

string grade(float &p)
{
if(p >86 && p<101)
{
return " Grade: A";
}
else if(p >74 && p<87)
{
return " Grade: B";
}
else if(p >64 && p<74)
{
return " Grade: C";
}
else if(p >49 && p<65)
{
return " Grade: D";
}
else if(p <50)
{
return " Grade: F";
}
}
int course1()
{
int s1;
cout<<" Enter 1-Course Marks out of 100: ";
return s1;
}
int course2()
{
int s2;
cout<<" Enter 2-Course Marks out of 100: ";
return s2;
}
int course3()
{
int s3;
cout<<" Enter 3-Course Marks out of 100: ";
return s3;
}
string repeatcourse1(int s1)
{
if(s1<33)
{
return " Course1: Resit";
}
else
{
return " Course1: Redo";
}
}
string repeatcourse2(int s2)
{
if(s2<33)
{
return " Course2: Resit";
}
else
{
return " Course2: Redo";
}
}
string repeatcourse3(int s3)
{
if(s3<33)
{
return " Course3: Resit";
}
else
{
return " Course3: Redo";
} }
int main ()
{
float obtain_marks;
float p; int s1,s2,s3; s1=course1();
cin>>s1;
s2=course2();
cin>>s2;
s3=course3();
cin>>s3;
obtain_marks=s1+s2+s3;
cout <<" obtained marks is: "< p=(obtain_marks/300)*100;
cout<<" percentage is: "< cout< cout< cout< cout< }

Topic 9: String


Task 1: Find the length of the string (the String should be inserted by the user)

Output:



Source Code

int main() {
string myString;
getline(cin,myString);
cout << "Length : " << myString.length();
return 0;
}

Task 2: Take 2 strings as an input and concatenate them.

Output:

10.2

Source Code

int main()
{
string str1, str2, newstr;
cout << "Enter string 1: ";
getline (cin, str1);
cout << "\n Enter string 2: ";
getline (cin, str2);
newstr = str1 + str2;//concatenation
cout << "\n Concated String: "<< newstr;
return 0;
}

Topic 10: Pointers


Task 1: Initialize an integer array of 5 elements. Display values of all elements along with their addresses using pointers.

Output:

11.1

Source Code

int main()
{
string array[5] = { "lorem ipsum", "shazi",
"welcome" , "OmG", "hi there" };
string* arraypointer= array;
cout<<"Adresses:"endl;
for(int i=0; i<5; i++ )
{
cout<<&arraypointer[i]< }
cout< cout< cout<<"values:"< for(int i=0; i<5; i++)
{
cout<< *(arraypointer + i)< }
return 0;
}

Task 2: Write a program which uses a pointer variable pointing to three different integer values, one at a time. Alter the input integers by performing multiplication using the pointer variable and display the updated values.

Output:

11.2

Source Code

void numswap(int &x, int &y)
{
int z=x;
x=y;
y=z;
}
int main()
{
int firstnum;
int secondnum;
cout<<"Enter first number"< cin>>firstnum;
cout<<"Enter number 2"< cin>>secondnum;
numswap(firstnum, secondnum);
cout<<"after swap"< cout<<"first number:"< cout<<"second number:"< return 0;
}

Task 3: Write a program which use one function that swap the values by reference

Output:

11.3

Source Code

void getnumber(int *variable)
{
cout<<"enter value"< cin>>*variable;
}
void doublevalue(int *variable)
{
*variable=*variable*2;
}
int main()
{
int variableToStore;
getnumber(&variableToStore);
cout< doublevalue(&variableToStore);
cout< return 0;
}

Task 4: Write a program which uses two functions and passes pointer as an argument .the function getNumber takes an input from the user and stores it in the pointer variable. The function double value doubles the value of the same.

Output:

11.4

Source Code

int main()
{
int a,b,c;
a=2;
b=6;
c=8;
int*pointer1 = &a;
int* pointer2= &b;
int* pointer3= &c;
*pointer1 =a*2;
cout< *pointer2 =b*3;
cout< *pointer3 = c*4;
cout< cout< cout< cout< return 0;
}

Topic 11: File Handling


Task 1: Write a C++ that perform following task:
· Create a function Student () that prompt user to a student record. Student contains following details.
ID, Full Name, Email, Department and Phone Number.
· Store the input in file student.txt
· In main program call function Student()
· Program should prompt user that you want to enter a new record. If ‘Y’ ask user for new details. If ‘N’, program should terminate. (Use event controlled loop)

Output:

12.1

Source Code

void student(){
int id,phone;
string fullName,email,department;
cout << "Enter student Id";
cin >> id;
cout << "Enter student full name";
cin >> fullName;
cout << "Enter student email";
cin >> email;
cout << "Enter student department";
cin >> department;
cout << "Enter student phone";
cin >> phone;
ofstream writeToStudent("student.txt");
writeToStudent <<"full Name: "< <<"Email:"< int main() {
bool Continue;
do{
student();
cout << "do you want to continue(Y/N)";
cin >> Continue;
} while(Continue);
return 0;
}

Task 2: Write a program and print the following output.

Output:

12.2

Source Code

int main() {
string text;
ifstream student("student.txt");
while(getline(student,text)){
cout << text< }
student.close();
return 0;
}

Task 3: Write a C++ program for billing system of MovInPeak restaurant. The program should perform following tasks:
· Show menu to customer for orders.
· Allow the customer to select more than one item from the menu.
· Calculate and print the bill.
Assume that the restaurant offers the following items (the price of each item is shown to the right of the item):
· Omlet $1.45
· French Omlet $2.45
· Muffin $0.99
· French Toast $1.99
· Fruit Basket $2.49
· Cereal $0.69
· Coffee $0.75
· Tea $0.50

Output:

12.3

Source Code

int main() {
cout << "Menu\n";
cout << "1 - Omlet ($1.45)\n";
cout << "2 -French Omlet ($2.45)\n";
cout << "3 -Muffin ($0.99)\n";
cout << "4 -French Toast ($1.99)\n";
cout << "5 -Fruit Basket ($2.49)\n";
cout << "6 -Cereal ($0.69)\n";
cout << "7 -Coffee ($0.75)\n";
cout << "8 -Tea ($0.50)\n";
ofstream writeToBill("bill.txt");
bool Continue;
int option;
do{
cout << "Enter food number";
cin >> option;
if(option == 1){
writeToBill << "1.45\n";
} else if(option == 2){
writeToBill << "2.45\n";
} else if(option == 3){
writeToBill << "0.99\n";
} else if(option == 4){
writeToBill << "1.99\n";
} else if(option == 5){
writeToBill << "2.49\n";
} else if(option == 6){
writeToBill << "0.69\n";
} else if(option == 7){
writeToBill << "0.75\n";
} else if(option == 8){
writeToBill << "0.50\n";
} else{
cout << "Invalid number";
}
cout << "Do you want to continue\n";
cin >> Continue;
} while(Continue);
return 0;
}

Topic 12: Structures


Task 1: Write a program in C++ using structures and print properties of 3 cell phone.

Output:

13.1

Source Code

struct Phone{
string manufecturer;
string os;
int ram;
float processor;
};
int main() {
Phone galaxy;
galaxy.manufecturer = "Samsung";
galaxy.os = "Android";
galaxy.ram = 6;
galaxy.processor = 2.5;
Phone iphone;
iphone.manufecturer = "Apple Inc.";
iphone.os = "IOS";
iphone.ram = 4;
iphone.processor = 3.5;
Phone lumia;
lumia.manufecturer = "Nokia";
lumia.os = "Windows-Mobile";
lumia.ram = 3;
lumia.processor = 1.5;
cout << "1-----Galaxy\n";
cout << "manufecturer: "< cout << "os:
"< cout << "ram:
"<< galaxy.ram<<" gb"< cout << "processor:
"< cout << "2-----Iphone\n";
cout << "manufecturer:
"< cout << "os:
"< cout << "ram:
"< cout << "processor:
"< cout << "3-----Lumia\n";
cout << "manufecturer:
"< cout << "os:
"< cout << "ram:
"< cout << "processor:
"< return 0;}

Task 2: Write a program in C++ using structures and print properties of 5 Books.

Output:

13.2

Source Code

struct Book{
string name;
string author;
int pages;
};
int main() {
Book myBook;
myBook.name = "Meri Book";
myBook.author = "Sharjeel Baig";
myBook.pages = 120;
Book book2;
book2.name = "Harry Potter";
book2.author = "JK Rowling";
book2.pages = 500;
Book book3;
book3.name = "Think and grow rich";
book3.author = "Napolian Hill";
book3.pages = 125;
Book book4;
book4.name = "Power of positive thinking";
book4.author = "Norman Vincent";
book4.pages = 126;
Book book5;
book5.name = "Fantastic Beasts";
book5.author = "J.K Rowlings";
book5.pages = 680;
cout << "[[[[[[[Book1]]]]]]\n";
cout << "Name:
"< cout << "Author:
"< cout << "Name:
"< cout << "[[[[[[[Book2]]]]]]\n";
cout << "Name:
"< cout << "Author:
"< cout << "Name:
"< cout << "[[[[[[[Book3]]]]]]\n";
cout << "Name:
"< cout << "Author:
"< cout << "Name:
"< cout << "[[[[[[[Book4]]]]]]\n";
cout << "Name:
"< cout << "Author:
"< cout << "Name:
"< cout << "[[[[[[[Book5]]]]]]\n";
cout << "Name:
"< cout << "Author:
"< cout << "Name:
"< return 0;}

Task 3: Write a program in C++ using structures, take input of employees basic information and salary details (including basic salary (BS), house rent (HR), transport rent (TR), medical allowance (MA)) and print properties of 3 employees along with the total salary of that employee. FORMULA: Total Salary= (BS + TR + HR + MA)

Output:

13.3

Source Code

struct Employee {
double bs;
double tr;
double hr;
double ma;
double pf;
};
Employee getData(Employee e) {
cout << "Enter BS: ";
cin>>e.bs;
cout << "Enter TR: ";
cin>>e.tr;
cout << "Enter HR: ";
cin>>e.hr;
cout << "Enter MA: ";
cin>>e.ma;
cout << "Enter PF: ";
cin>>e.pf;
return e;
}
void displayData(Employee e) {
double totalSalary = (e.bs+e.tr+e.hr+e.ma) - e.pf;
cout << "Total Salary: "<< totalSalary< }
int main() {
Employee e1,e2,e3;
Employee employeeArray[3]={e1,e2,e3};
for(int i = 0;i<3;i++){
cout << "for employee "< employeeArray[i] = getData(employeeArray[i]);
}
for(int i = 0; i<3; i++){
cout << "information of employee "< displayData(employeeArray[i]);
}
return 0;}