0% found this document useful (0 votes)
66 views13 pages

Programming Fundamental Lab 3

The document contains 11 programming tasks related to basic C++ concepts like operators, input/output, functions, and mathematical expressions. The tasks include writing programs to: 1) Display city names and number of schools in a formatted table 2) Demonstrate the use of various assignment operators 3) Calculate the area of a sphere given the radius 4) Find the average of 3 game scores entered by the user 5) Calculate the circumference of a circle given the radius

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
66 views13 pages

Programming Fundamental Lab 3

The document contains 11 programming tasks related to basic C++ concepts like operators, input/output, functions, and mathematical expressions. The tasks include writing programs to: 1) Display city names and number of schools in a formatted table 2) Demonstrate the use of various assignment operators 3) Calculate the area of a sphere given the radius 4) Find the average of 3 game scores entered by the user 5) Calculate the circumference of a circle given the radius

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

Page 1 of 13

TASK #1:
What are Increment & decrement operator? Explain with proper example.
SOLUTION:
Increment Operators are used to increase the value of the variable by one and Decrement Operators are
used to decrease the value of the variable by one in C/C++ programs. Both increment and decrement
operator are used on a single operand or variable, so it is called as a unary operator.
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator: – – i ; i – – ;
POST and PRE-INCREMENT or DECREMENT:
Below table will explain the difference between pre/post increment and decrement operators in C/C++
programming language.
Operator Operator/Description
Pre increment operator (++i) value of i is incremented before assigning it to the variable i
Post increment operator value of i is incremented after assigning it to the variable i
(i++)
Pre decrement operator (--i) value of i is decremented before assigning it to the variable i
Post decrement operator (i--) value of i is decremented after assigning it to variable i

Task #2:
What are Assignment Statements/Operators? Explain with proper example.
SOLUTION:
Assignment operators are used to assign values to variables. In the example below, we use
the assignment operator (=) to assign the value 10 to a variable called x:
Example: int x = 10;

The following table lists the assignment operators supported by the C/C++ language.
Operator Description Example
= Simple assignment operator. Assigns values from right side C = A + B will assign the
operands to left side operand value of A + B to C
+= Add AND assignment operator. It adds the right operand to the C += A is equivalent to
left operand and assign the result to the left operand. C=C+A
-= Subtract AND assignment operator. It subtracts the right operand C -= A is equivalent to
from the left operand and assigns the result to the left operand. C=C-A
*= Multiply AND assignment operator. It multiplies the right operand C *= A is equivalent to
with the left operand and assigns the result to the left operand. C=C*A
/= Divide AND assignment operator. It divides the left operand with C /= A is equivalent to
the right operand and assigns the result to the left operand. C=C/A
%= Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to
operands and assigns the result to the left operand. C=C%A
<<= Left shift AND assignment operator. C <<= 2 is same as
C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as
C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
Page 2 of 13

TASK #3:
Create the source file and observe the output of the following program?
#include<iostream.h>
#include<iomanip.h>
main ()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<" City "<<"\t No. of Schools";
cout<<"\n Durban \t"<<Durban;
cout<<"\n Moscow \t"<<Moscow;
cout<<"\n Sydney \t"<<Sydney;
cout<<"\n Paris \t"<<Paris;
}

SOLUTION:
Page 3 of 13

TASK #4:
Create the source file and observe the output of the following program?
#include<iostream.h>
#include<iomanip.h>
main ()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<setw(10)<<" City "<<setw(30)<<" No. of Schoolss \n";
cout<<setw(10)<<" Durban"<<setw(20)<<Durban<<"\n";
cout<<setw(10)<<" Moscow"<<setw(20)<<Moscow<<"\n";
cout<<setw(10)<<" Sydney"<<setw(20)<<Sydney<<"\n";
cout<<setw(10)<<" Paris"<<setw(20)<<Paris<<"\n";
}

SOLUTION:
Page 4 of 13

TASK #5:
Create the source file and observe the output of the following program?
#include<iostream.h>
#include<conio.h>
main ()
{
int a = 21;
int c ;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c = a;
cout<<" (c = a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c += a;
cout<<"(c += a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c -= a;
cout<<"(c -= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c *= a;
cout<<"(c *= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c /= a;
cout<<"(c /= a) Value of c = "<<c<<"\n";
c = 200;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c %= a;
cout<<"(c %= a) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c <<= 2;
cout<<"(c <<= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c >>= 2;
cout<<"(c >>= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c &= 2;
cout<<"(c &= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c ^= 2;
cout<<"(c ^= 2) Value of c = "<<c<<"\n";
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c |= 2;
cout<<"( c |= 2) Value of c = "<<c<<"\n";
}
Page 5 of 13

SOLUTION:
Page 6 of 13

TASK #6 (A):
Create the source file and observe the output of the following program?
#include<iostream.h>
#include<conio.h>
main ()
{
int a = 20;
int b=0;
clrscr();
b=a++;
cout<<"After b=a++ Value of b ="<<b<<" and value of a="<<a<<endl;
b=++a;
cout<<"After b=++a Value of b ="<<b<<" and value of a="<<a<<endl;
b=a--;
cout<<"After b=a-- Value of b ="<<b<<" and value of a="<<a<<endl;
b=--a;
cout<<"After b=--a Value of b ="<<b<<" and value of a="<<a<<endl;
}

SOLUTION:
Page 7 of 13

TASK #6 (B):
Write a program to solve the following algebraic expression that takes the values of variables
from user.
2
Equation is: 3𝑥 2 + +1
4𝑎
(Take power of variable from power function)
SOLUTION:
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int x, a, p, q, y;
cout<<"Enter x and a value respectively: ";
cin>>x,a;
p = pow(x , 2 ); //Power function = x.x
q = 4*a;
y = (3*p) + (2/q) + 1;
cout<<"Solution is: " << y;
}

TASK #7:
Write a program to solve the following algebraic expression that takes the values of variables
from user.
𝑦2
Equation is: 3𝑥𝑦𝑧 + 3𝑥 3 −
4𝑧
(Take power of variable from power function)
Page 8 of 13

SOLUTION:
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
int x, y, z, px, py, i, j, k, result;
cout<<"Enter values of x, y, z respectively: ";
cin >> x >> y >> z;
px = pow(x , 3); //Power x^3
i = 3*px;
py = pow(y , 2); //Power y^2
j = 3*(x*y*z);
k = py/(4*z);
result = (j + i) - k ;
cout<<"Solution is: " << result;
}
Page 9 of 13

TASK #8:
Write the program, which takes radius from the user and calculate the area of the sphere.
𝒊. 𝒆 𝐴𝑟𝑒𝑎 = 4𝜋𝑟 2
𝑯𝒊𝒏𝒕: 𝜋 = 3.1416
Area = 4*3.1416*r*r
SOLUTION:
#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
float r, area, pi=3.1416;
cout<<"Enter radius of Sphere: ";
cin >> r;
area = 4*pi*r*r;
cout<<"Area of Sphere is: " << area;
}

TASK #9:
Write a program that asks the user to enter a score for three games, and then displays the
average of those three scores.
SOLUTION:
#include<iostream>
using namespace std;
int main ()
Page 10 of 13

{
float avg, a, b, c;
cout<<"Enter score of Game1: ";
cin >> a;
cout<<"Enter score of Game2: ";
cin >> b;
cout<<"Enter score of Game3: ";
cin >> c;
avg = (a+b+c)/3;
cout<<"Average is: " << avg;
}

TASK #10:
Write a program that calculates the circumference of a circle. Radius should be user defined.
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
float r, circum, pi=3.1416;
cout<<"Enter radius of Circle: ";
cin >> r;
circum = 2*pi*r;
cout<<"Circumference of Circle is: " << circum;
}
Page 11 of 13

TASK #11:
A car with a 20-gallon gas tank average 21.5 miles per gallon when drive in town and 26.8 miles
per gallon when driven on the highway. Write a program that calculates and displays the
distance the car can travel on one tank of gas when driven in town and when driven on the
highway.
SOLUTION:
#include <iostream>
using namespace std;

int main( )
{
float Gas_Tank_S = 20.0, Distance_Driven;
float Town = 21.5, Highway = 26.8;

// when in town
Distance_Driven = Gas_Tank_S * Town;
cout << "On one tank of gas the car can travel " << Distance_Driven
<< " miles when driven in town.\n\n";

// when on highway
Distance_Driven = Gas_Tank_S * Highway;
cout << "On one tank of gas the car can travel " << Distance_Driven
<< " miles when driven on the highway." << endl;
}
Page 12 of 13

TASK #12:
An electronic company sells circuit boards at a 40 percent profit. Write a program that will
calculate the selling price of circuit boards that casts $12.67. Display the result on the screen.
SOLUTION:
#include<iostream>
using namespace std;
int main ()
{
float price=12.67, calc, profit;
cout<<"Original price is: "<<price<<"$";
profit = (40.0/100)*price; //40% profit
calc = price + profit; //final customer price
cout<<"\n\nSelling price is: "<<calc<<"$"<<endl;
cout<<"(Your profit is: " << profit<<"$"<<")";
}
Page 13 of 13

Prepared By: Khawar Khalil

You might also like