# Tasks & Solution of C++ Language

## Topic 1: Introduction to C++

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### Source Code

#include iostream
using namespace std;
int main()
{
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;
}

#### Output: #### 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

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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 : "< }

#### Output: #### 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

#### Output: #### Source Code

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

#### Output: #### 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;
}

#### 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

#### Output: #### 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);
}

#### Output: #### 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;
}
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### Source Code

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

#### Output: #### 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

#### Output: #### Source Code

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

#### Output: #### Source Code

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

#### 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;
}

#### 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;
}

#### 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;
}

#### 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

#### Output: #### 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"){
} else if(temperature == "humid"){
cout << "watch tv";
}
} else{
cout << "Invalid Values Entered";
}
return 0;
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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";
}
}

#### Output: #### 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--;
}
}

#### Output: #### Source Code

int main(){
int input, 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;
}

#### Output: #### Source Code

int main()
{
int array;
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;
}

#### Output: #### Source Code

int main()
{
int array;
int i, max, min;
for(i=0; i<5; i++)
cin>>array[i];
max = array;
min = array;
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;
}

#### Output: #### Source Code

int main()
{
int arr, 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

#### Output: #### Source Code

int netSalary=0;
cout<<"Enter a Basic Salary"< double basicSalary;
cin>>basicSalary;
providance= providance*basicSalary;
house=house*basicSalary;
cout<<"your net salary : "< }
int main()
{
double a=1000;
double b=500;
double c=200;
{
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;
}

#### Output: #### Source Code

int netSalary=0;
cout<<"Enter a Basic Salary"< double basicSalary;
cin>>basicSalary;
providance= providance*basicSalary;
house=house*basicSalary;
cout<<"your net salary : "< }
int main()
{
double a=1000;
double b=500;
double c=200;
{
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;
}

#### 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;}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### Source Code

{
if(p >86 && p<101)
{
}
else if(p >74 && p<87)
{
}
else if(p >64 && p<74)
{
}
else if(p >49 && p<65)
{
}
else if(p <50)
{
}
}
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

#### Output: #### Source Code

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

#### Output: #### 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

#### Output: #### Source Code

int main()
{
string array = { "lorem ipsum", "shazi",
"welcome" , "OmG", "hi there" };
string* arraypointer= array;
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;
}

#### Output: #### 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;
}

#### Output: #### 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;
}

#### Output: #### 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

#### Output: #### 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;
}

#### Output: #### Source Code

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

#### Output: #### Source Code

int main() {
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

#### Output: #### 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;}

#### Output: #### 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;}

#### Output: #### 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={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;}