You are on page 1of 24

Fundamentals of

Programming (Lab)
Assignment Week 3

Meer Muhammad Ansari

BS-1 AI

IBA Sukkur
Arrow:
/* Exercise 01: Arrow using setw
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(10)<<"*"<<endl;
cout<<setw(11)<<"**"<<endl;
cout<<setw(12)<<"*********"<<endl;
cout<<setw(11)<<"**"<<endl;
cout<<setw(10)<<"*"<<endl;

system("pause");
return 0;
}

Output:
Pyramid:
/* Exercise 01: Pyramid using setw
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(10)<<"^"<<endl;
cout<<setw(9)<<"^"<<setw(2)<<"^"<<endl;
cout<<setw(8)<<"^"<<setw(2)<<"^"<<setw(2)< "^"<<endl;
cout<<setw(7)<<"^"<<setw(2)<<"^"<<setw(2)<<"^"<<setw(2)<<"^"<<endl;

system("pause");
return 0;
}

Output:

Hourglass:
/* Exercise 01: Hourglass using setw
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(10)<<"%%%%%"<<endl;
cout<<setw(9)<<"%%%"<<endl;
cout<<setw(8)<<"%"<<endl;
cout<<setw(9)<<"%%%"<<endl;
cout<<setw(10)<<"%%%%%"<<endl;

system("pause");
return 0;
}

Output:

Diamond:
/* Exercise 01: Diamond using setw
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(13)<<"*"<<endl;
cout<<setw(14)<<"***"<<endl;
cout<<setw(15)<<"*****"<<endl;
cout<<setw(16)<<"*******"<<endl;
cout<<setw(17)<<"*********"<<endl;
cout<<setw(18)<<"***********"<<endl;
cout<<setw(19)<<"*************"<<endl;
cout<<setw(20)<<"***************"<<endl;
cout<<setw(19)<<"*************"<<endl;
cout<<setw(18)<<"***********"<<endl;
cout<<setw(17)<<"*********"<<endl;
cout<<setw(16)<<"*******"<<endl;
cout<<setw(15)<<"*****"<<endl;
cout<<setw(14)<<"***"<<endl;
cout<<setw(13)<<"*"<<endl;

system("pause");
return 0;
}

Output:
/* Exercise 1: Triangle and rectangle using setw and endl
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(12)<<"*"<<endl;
cout<<setw(11)<<"*"<<setw(2)<<"*"<<endl;
cout<<setw(10)<<"*"<<setw(4)<<"*"<<endl;
cout<<setw(9)<<"*"<<setw(6)<<"*"<<endl;
cout<<setw(8)<<"*"<<setw(8)<<"*"<<endl;
cout<<setw(7)<<"*"<<setw(10)<<"*"<<endl;
cout<<setw(6)<<"*"<<setw(12)<<"*"<<endl;
cout<<setw(19)<<"* * * * * * * *"<<endl<<endl;
cout<<setw(22)<<"* * * * * * * * * *"<<endl;
cout<<setw(4)<<"*"<<setw(18)<<"*"<<endl;
cout<<setw(4)<<"*"<<setw(18)<<"*"<<endl;
cout<<setw(4)<<"*"<<setw(18)<<"*"<<endl;
cout<<setw(4)<<"*"<<setw(18)<<"*"<<endl;
cout<<setw(4)<<"*"<<setw(18)<<"*"<<endl;
cout<<setw(22)<<"* * * * * * * * * *"<<endl;

system("pause");
return 0;
}
Output:

/* Exercise 1: Double cone using setw and endl


This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setw(10)<<"*"<<endl;
cout<<setw(11)<<"* *"<<endl;
cout<<setw(8)<<"*"<<setw(4)<<"*"<<endl;
cout<<setw(7)<<"*"<<setw(6)<<"*"<<endl;
cout<<setw(6)<<"*"<<setw(8)<<"*"<<endl;
cout<<setw(5)<<"*"<<setw(10)<<"*"<<endl;
cout<<setw(4)<<"*"<<setw(12)<<"*"<<endl;
cout<<setw(5)<<"*"<<setw(10)<<"*"<<endl;
cout<<setw(6)<<"*"<<setw(8)<<"*"<<endl;
cout<<setw(7)<<"*"<<setw(6)<<"*"<<endl;
cout<<setw(8)<<"*"<<setw(4)<<"*"<<endl;
cout<<setw(11)<<"* *"<<endl;
cout<<setw(10)<<"*"<<endl;

system("pause");
return 0;
}

Output:

/* Exercise 2: Students Subjects and Marks table


This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<setprecision(2)<<fixed;
cout<<"Students"<<setw(10)<<"English"<<setw(8)<<"Maths"<<setw(8)<<"
ICT"<<setw(16)<<"Programming"<<endl;
cout<<"--------------------------------------------------"<<endl;
cout<<"Tom"<<setw(13)<<"40.00"<<setw(10)<<"95.00"<<setw(10)<<"94.0"
<<setw(8)<<"54.00"<<endl;
cout<<"Jerry"<<setw(11)<<"15.60"<<setw(10)<<"34.00"<<setw(10)<<"43.
00"<<setw(8)<<"67.00"<<endl;
cout<<"Tweety"<<setw(10)<<"30.00"<<setw(10)<<"45.67"<<setw(10)<<"46.
00"<<setw(8)<<"32.67"<<endl;
cout<<"Bunny"<<setw(11)<<"25.00"<<setw(10)<<"12.00"<<setw(10)<<"23.
00"<<setw(8)<<"12.00"<<endl;
cout<<"Dora"<<setw(12)<<"50.00"<<setw(10)<<"56.00"<<setw(10)<<"85.0
0"<<setw(8)<<"45.45"<<endl;
cout<<"Donald"<<setw(10)<<"79.00"<<setw(10)<<"89.00"<<setw(10)<<"78.
79"<<setw(8)<<"12.00"<<endl;

system("pause");
return 0;
}

Output:

/* Exercise 3a: Print Hello using setw and endl


This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"Hello"<<endl<<setw(18)<<"Hello"<<endl<<setw(31)<<"Hello"<<en
dl;

system("pause");
return 0;
}

Output:

/* Exercise 3b: Print a statement using setw and endl and backslash
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"Programming language makes use of different operators that
are:"<<endl;
cout<<setw(51)<<"Logical operator \\Relational Operator\\ "<<endl;

return 0;
}
Output:

/* Exercise 3c: Use of setw value as input


This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 31st August, 2023 */
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int num1;
int num2;
cout<<"Input two numbers"<<endl;
cin>>num1;
cin>>num2;

cout<<setw(num1)<<num1<<endl;
cout<<setw(num2)<<num2<<endl;

return 0;
}
Output:

/* Exercise 4: Use of Variables and Manipulators to create Table of


Marks of Students
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week 3
Date: 1st September, 2023 */
#include <iostream>
#include <iomanip>
using namespace std;

int main() {

string std1, std2, std3;


int marks1, marks2, marks3;

cout << "Enter Name of Student 1: ";


cin >> std1;
cout << "Enter Marks of " << std1 << ": ";
cin >> marks1;

cout << "Enter Name of Student 2: ";


cin >> std2;
cout << "Enter Marks of " << std2 << ": ";
cin >> marks2;

cout << "Enter Name of Student 3: ";


cin >> std3;
cout << "Enter Marks of " << std3 << ": ";
cin >> marks3;

// Calculate the sum and average of the marks


int sum = marks1 + marks2 + marks3;
double average = static_cast<double>(sum) / 3;

// Marks Table
cout << setw(40) << "The Marks of Students" << endl;
cout << left << setw(20) << "Student" << right << setw(10) <<
"Marks" << endl;
cout << left << setw(20) << std1 << right << setw(10) << marks1 <<
endl;
cout << left << setw(20) << std2 << right << setw(10) << marks2 <<
endl;
cout << left << setw(20) << std3 << right << setw(10) << marks3 <<
endl;
cout << left << setw(20) << "Sum " << right << setw(10) << sum <<
endl;
cout << left << setw(20) <<"Average " << right << setw(13) << fixed
<< setprecision(2) << average << endl;

system("pause");
return 0;
}

Output:
/* Exercise 5: Price of Beverages.
Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
// Define the prices of the beverages
double apple_juice_price = 199.99;
double orange_juice_price = 180.88;
double banana_shake_price = 170.77;

// Print the prices of the beverages with exactly two decimal


places
cout<<setw(40)<<"WELCOME TO ICE-COOL\n\n";
cout << fixed << setprecision(2);
cout <<setw(34) << "Apple Juice : " << apple_juice_price << endl;
cout <<setw(34) << "Orange Juice : " << orange_juice_price << endl;
cout <<setw(34) << "Banana Shake : " << banana_shake_price << endl;

system("pause");
return 0;
}
Ouput:

/* Exercise 6: Student Progress Card.


Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */
#include <iostream>
#include <iomanip>
using namespace std;
int main(){

string stdname;
int rollnumber;
double maths, physics, chemistry;

cout<<"Enter Your Name"<<endl;


cin>>stdname;
cout<<"Enter Your Roll No"<<endl;
cin>>rollnumber;
cout<<"Enter Marks of Maths"<<endl;
cin>>maths;
cout<<"Enter Marks of Physics"<<endl;
cin>>physics;
cout<<"Enter Marks of Chemistry"<<endl;
cin>>chemistry;
double total = maths+chemistry+physics;
double average = static_cast<double>(total)/3;
double percentage = (total/300)*100;

cout<<"----Student Progress Card-----"<<endl;


cout<<"Student Name: "<<setw(10) <<stdname <<endl;
cout<<"Roll Number: "<<setw(10) <<rollnumber <<endl;
cout<<"------------------------------"<<endl;
cout<<"Subjects"<<setw(13) <<"Marks"<<endl;
cout<<"------------------------------"<<endl;
cout<<"Maths"<<setw(13) <<maths<<endl;
cout<<"Physics"<<setw(11) <<physics<<endl;
cout<<"Chemistry"<<setw(9) <<chemistry<<endl;
cout<<"------------------------------"<<endl;
cout<<"Total"<<setw(14) <<total<<endl;
cout<<"Average"<<setw(11) <<average<<endl;
cout<<"Percentage"<<setw(8) <<percentage<<"%"<<endl;
cout<<"------------------------------"<<endl;

system("pause");
return 0;

Output:
/* Exercise 7 : Write a short program that declares and initializes
double variables one, two, three, four, and five to the
values 1.000, 1.414, 1.732, 2.000, and 2.236, respectively.
Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include<iostream>
#include<iomanip>
using namespace std;
int main(){

double one = 1.000;


double two = 1.414;
double three = 1.732;
double four = 2.000;
double five = 2.236;

cout<<fixed<<setprecision(3);
cout<<"N"<<setw(19)<<"Square Root\n\n";
cout<<"1"<<setw(9)<<(int)one<<endl;
cout<<"2"<<setw(13)<<two<<endl;
cout<<"3"<<setw(13)<<three<<endl;
cout<<"4"<<setw(9)<<(int)four<<endl;
cout<<"5"<<setw(13)<<five<<endl;

system("pause");
return 0;
}

Output:
/* Exercise 8 : Game of Mad Lib
Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main() {
string name_of_instructor, name, food, adjective, color, animal;
int num;

cout << "Game of Mad Lib" << endl << endl;


cout << "Enter Name of your Instructor" << endl;
getline(cin, name_of_instructor);
cout << "\nEnter Your Name" << endl;
getline(cin, name);
cout << "\nWhat's Your Favourite Food?" << endl;
getline(cin, food);
cout << "\nEnter any Adjective" << endl;
cin >> adjective;
cout << "\nWhat's Your Favourite Color?" << endl;
cin >> color;
cout << "\nWhat's Your Favourite Animal?" << endl;
cin>>animal;
cout << "\nEnter any Number between 100-120" << endl;
cin >> num;

cout << "\nDear Instructor " << name_of_instructor << "," << endl
<< endl;
cout << "I am sorry that I am unable to turn in my homework at this
time. First, I ate a rotten " << food << ", which made\nme turn " <<
color << " and extremely ill. I came down with a fever of " << num << ".
Next, my " << adjective << "\npet " << animal << " must have smelled
the remains of the " << food << " on my homework, because he ate it. I
am \ncurrently rewriting my homework and hope you will accept it late"
<< endl << endl;
cout << "Sincerely," << endl << endl;
cout << name << endl << endl;

system("pause");
return 0;
}

Output:
/* Exercise 9 : Score Board
Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main() {
string player1, player2;
double score1, score2;

cout << "Enter Player1 Name" << endl;


getline(cin, player1);
cout << "\nEnter Player2 Name" << endl;
getline(cin, player2);
cout << "\nEnter Score of " << player1 << ":" << endl;
cin >> score1;
cout << "\nEnter Score of " << player2 << ":" << endl;
cin >> score2;

cout << "\n\nScore Board Display" << endl << endl;


cout << setw(15) << left << "Player Name:";
cout << setw(1) << left << "";
cout << setw(15) << left << "Score:" << endl << endl;
cout << setw(15) << left << player1;
cout << setw(1) << left << "";
cout << setw(15) << left << score1 << endl << endl;

cout << setw(15) << left << player2;


cout << setw(1) << left << "";
cout << setw(15) << left << score2 << endl << endl;

if (score1 > score2) {


cout << setw(30) << right << player1 << " is Winner" << endl;
}
else if (score2 > score1) {
cout << setw(30) << right << player2 << " is Winner" << endl;
}
else {
cout <<setw(30) << right << "TIE" << endl << endl;
}

system("pause");
return 0;
}

Output:
/* Exercise 10 : Contact Information Formatter
Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main(){

string name, email;


long long num;

cout << "Enter Your Name" << endl << endl;


getline(cin, name);
cout << "\nEnter Your Email Address" << endl << endl;
cin >> email ;
cout << "\nEnter Your Phone Number" << endl << endl;
cin >> num ;

cout << "\n\nContact Information Formatter" << endl << endl;


cout << setw(15) << left << "Name:";
cout << setw(1) << left << "";
cout << setw(15) << left << name << endl << endl;

cout << setw(15) << left << "Email Address:";


cout << setw(1) << left << "";
cout << setw(15) << left << email << endl << endl;

cout << setw(15) << left << "Phone Number:";


cout << setw(1) << left << "";
cout << setw(15) << left << num << endl << endl;

system("pause");
return 0;
}
Output:

/* Exercise 11 : User Profile Summary


Fundamentals of Programming
This code is written by Meer Muhammad Ansari
BS-1(AI)
Week3
Date: 1st September, 2023 */

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main() {
string name, color;
int age;
cout << "Enter Your Name: ";
getline(cin, name);

cout << "\nEnter Your Age: ";


cin >> age;

cin.ignore();

cout << "\nEnter Your Favorite Color: ";


getline(cin, color);

cout << "\n\nUSER PROFILE SUMMARY" << endl << endl;


cout << setw(20) << left << "Name:";
cout << setw(1) << left << "";
cout << setw(20) << left << name << endl << endl;

cout << setw(20) << left << "Age:";


cout << setw(1) << left << "";
cout << age << " years" << endl << endl;

cout << setw(20) << left << "Favorite Color:";


cout << setw(1) << left << "";
cout << setw(20) << left << color << endl << endl;

system("pause");
return 0;
}

Output:

You might also like