0% found this document useful (0 votes)
25 views

Important Practice of C++

The document contains multiple C++ code snippets demonstrating the use of loops, functions, and conditionals to perform tasks like calculating factorials, printing tables, swapping variables, and calculating bills. Some key snippets include a for loop to print the first 5 multiples of a user-input number, nested for loops to print multiplication tables, and an if/else conditional to calculate residential or business customer bills based on input type.

Uploaded by

Muskun Rubab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Important Practice of C++

The document contains multiple C++ code snippets demonstrating the use of loops, functions, and conditionals to perform tasks like calculating factorials, printing tables, swapping variables, and calculating bills. Some key snippets include a for loop to print the first 5 multiples of a user-input number, nested for loops to print multiplication tables, and an if/else conditional to calculate residential or business customer bills based on input type.

Uploaded by

Muskun Rubab
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 81

#include<iostream>

using namespace std;


void main()
{
int n, r[5];
cout << "Enter any number for which you want a first five multiples " <<
endl;
cin >> n;
cout << "The first five multiple of " << n << " is: " << endl;
for (int i = 1; i <= 5; i++)
{
r[i-1] = i * n;
cout << i << ": " << r[i - 1] << endl;
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
cout << "N\tN*10\tN*100\tN*1000" << endl;
for (int i = 1; i <= 5; i++)
{
cout << i << "\t" << i * 10 << "\t" << i * 100 << "\t" << i * 1000 <<
endl;
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int P = 10000,N=4 ;
float R = 0.07, Amount;

Amount = P * (1 + R) * N;
cout << "The amount of money is: " << Amount << endl;
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int n[4],sum=0;
float average, average_bill;

for (int i = 1; i <= 4; i++)


{
cout << "\nPlease input your water bill for quarter: " << i << ": ";
cin >> n[i];
sum = sum + n[i];
}
average = sum / 4;
average_bill = average / 3;
cout << "\nYour average monthly bill is $" << average_bill<<".";
if (average_bill < 25) {
cout << "Wow! You conserve the water." << endl;
}
else if (average_bill > 25 && average_bill < 75)
{
cout << "You are using typical amount of water" << endl;
}
else if (average_bill > 75)
{
cout << "You are using excessive amount of water" << endl;
}
system("pause");
}

/*.Write a program for a cable billing company. First understand the following
situation
This program calculates a customer�s bill for a local cable company. There are two
types of customers: residential and business. There are two rates for calculating a
cable bill: one for residential customers and one for business customers.
For residential customers, the following rates apply:
� Bill processing fee: $4.50
� Basic service fee: $20.50
� Premium channels: $7.50 per channel
For business customers, the following rates apply:
� Bill processing fee: $15.00
� Basic service fee: $75.00 for first 10 connections, $5.00 for each additional
connection
� Premium channels: $50.00 per channel for any number of connections
The program should ask the user for an account number (an integer) and a customer
code. Assume that R or r stands for a residential customer, and B or b stands for a
business customer
Input : The customer�s account number, customer code, number of premium channels
to which the user subscribes, and, in the case of business customers, number of
basic service connections.
Output: Customer�s account number and the billing amount.
Algorithm:
1. Prompt the user for the account number and customer type.
2. Based on the customer type, determine the number of premium channels and basic
service connections, compute the bill, and print the bill:
a. If the customer type is R or r,
i. Prompt the user for the number of premium channels.
ii. Compute the bill.
iii. Print the bill.
b.If the customer type is B or b,
i. Prompt the user for the number of basic service connections and number of
premium channels.
ii. Compute the bill.
iii. Print the bill.

*/
#include<iostream>
#include<string>
using namespace std;

void main()
{
int account_number, basic_connection, premium_channels,
additional_connection;
float bill_processing_fee, basic_processing_fee, additional_processing_fee,
channel_fees, total_bill;
cout << "Enter your account number: ";
cin >> account_number;
string customer_type;
cout << "Enter your customer type\nEnter B or b for Business customers\nEnter
R or r for Residential customer " << endl;
getline(cin, customer_type);
if (customer_type == "b" || customer_type == "B")
{
cout << "Enter the number of the basic connection:" << basic_connection
<< endl;
cout << "Enter the number of premium channels: " << premium_channels
<< endl;
bill_processing_fee = 4.50;
if (basic_connection < 11)
{
basic_processing_fee = 75;
}
else if (basic_connection >= 11)
{
additional_connection = basic_connection - 10;
additional_processing_fee = additional_connection * 5;
basic_processing_fee = 75 + additional_processing_fee;
}
channel_fees = 50 * premium_channels;
total_bill = bill_processing_fee + basic_processing_fee + channel_fees;

}
else if ((customer_type == "R") || (customer_type == "r"))
{
cout << "Enter the number of premium channels : " << premium_channels
<< endl;
bill_processing_fee = 15.00;
basic_processing_fee = 20.50;
channel_fees = 7.50 * premium_channels;
total_bill = bill_processing_fee + basic_processing_fee + channel_fees;
}
cout << "Account_number of user: " << account_number << endl;
cout << "Total billing amount is : " << total_bill << endl;
system("pause");
}

#include<iostream>
#include<string>
using namespace std;
void main()
{
int c, d,e;
cout << "Enter the value of c " << endl;

cin >> c;
cout << "Enter the value of d " << endl;
cin >> d;
e = d;
d = c;
c = e;

cout << "The value of c is : " << c << " The value of d is: " << d << endl;

/*sqrt
pow
log
ceil
floor
sin
tan
cos*/
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
cout << "Enter a number " << endl;
float n;
cin >> n;
cout << "The square root of the number is " << sqrt(n) << endl;
cout << "The tangent of the number is " << tan(n) << endl;
cout << "The cos of the number is " << cos(n) << endl;
cout << "The sin of the number is " << sin(n) << endl;
cout << "The floor of the number is " << floor(n) << endl;
cout << "The ceil of the number is " << ceil(n) << endl;
cout << "The log of the number is " << log(n) << endl;
cout << "The power of the number is " << pow(2, n) << endl;
system("pause");
}

#include <iostream>
using namespace std;
void main()
{
int n;
int largest1 = INT_MIN;
char c;
do {
cout << "Enter a number" << endl;
cin >> n;
cout << "Enter C for continue and E for exit" << endl;
cin >> c;
if (c == 'E')
{
cout << "Program is terminated" << endl;
}
else if (c == 'C')
{
cout << " " << endl;
}
else
{
cout << "Your enter a wrong option " << endl;
}
if (n>largest1)
{
largest1 = n;
}
} while (c != 'E');
cout << "The largest number is " << largest1 << endl;
system("pause");
}

#include<iostream>
using namespace std;
void main() {
/*toupper function is only work for the char not for strings*/
char c ,y;
cout << "Enter any character to convert it into upper case " << endl;
cin >> c;
y = toupper(c);
cout << "Your capitalized letter is " << y<<endl;
system("pause");
}

#include<iostream>
using namespace std;
void main() {
/*tolower function is only work for the char not for strings*/
char c ,y;
cout << "Enter any character to convert it into lower case " << endl;
cin >> c;
y = tolower(c);
cout << "Your small letter is " << y<<endl;
system("pause");
}

#include<iostream>
using namespace std;
void main() {
/*tolower function is only work for the char not for strings
for islower or isupper we have to use bool y also
answer is one if condition is true
answer is zero if condition is false*/
char c ;
bool y;
cout << "Enter any character to check whether it is in lower case " << endl;
cin >> c;
y = islower(c);
cout << "Your small letter is " << y<<endl;
system("pause");
}

#include<iostream>
using namespace std;
void main() {
/*tolower function is only work for the char not for strings
for islower or isupper we have to use bool y also
answer is one if condition is true
answer is zero if condition is false*/
char c ;
bool y;
cout << "Enter any character to check whether it is in upper case " << endl;
cin >> c;
y = isupper(c);
cout << "Your small letter is " << y<<endl;
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
for (int i = 5; i <= 10; i++)
{
cout << "Table of " << i << " is: " << endl;
for (int j = 1; j <= 10; j++)
{
cout << i<<" * " << j << " = " << j * i << endl;
}
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int n;
int factorial = 1;
cout << "Enter the number for which you want the factorial " << endl;
cin >> n;
for (int i = n; i >= 1; i = i - 1)
{
factorial = factorial * i;
}
cout << "The factorial of the number is: " <<factorial<< endl;
system("pause");
}
#include<iostream>
using namespace std;
void main()
{
for (int i = 1; i <= 2; i++) {

for (int j = 1; j <= 1; j++)


{
cout << "++++" << endl;
}
for (int k = 1; k <= 1; k++)
{
cout << "====" << endl;
}
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
for(int l=1;l<=4;l++){
for (int i = 1; i <= 2; i++) {

for (int j = 1; j <= 1; j++)


{
cout << "+" ;
}
for (int k = 1; k <= 1; k++)
{
cout << "=" ;
}
}
cout << "\n";
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
for(int i=1;i<=4;i++)
{
for (int j = 1; j <= 4; j++)
{
if (j == 1 || j == 3)
{
cout << "+" ;
}
else {
cout << "=";
}

}
cout << "\n";
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
cout << "5";
}
cout << "\n";
}
system("pause");
}

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

for (int j = 1; j <= 5; j++)


{
cout << "*"<<endl;
}

system("pause");
}

/*Use a sentinel controlled do while loop to calculate the average of the numbers
entered by the user. Continue until the user tells you to stop.
P: Ask user for a number
E: exit the program after displaying the average.*/
#include<iostream>
using namespace std;
void main()
{
int n, sum = 0,count=0;
float result;
char c;
do {
cout << "Enter a number " << endl;
cin >> n;
sum = sum + n;
count = count + 1;
cout << "Please enter your choice\n Enter P for entering more numbers \
nEnter E for exit and checking the average" << endl;
cin >> c;
if (c == 'P')
{
cout << " " << endl;
}
else if (c == 'E')
{
cout << "The average of numbers is : ";
}
else {
cout << "Enter right option" << endl;
}
} while (c != 'E');
result = sum / count;
cout << result << endl;
system("pause");
}

/*Use a sentinel controlled do while loop to calculate the count of the even
numbers entered by the user. Continue until the user tells you to stop.
P: Ask user for a number
E: exit the program after displaying the count.*/
#include<iostream>
using namespace std;
void main()
{
int n,count=0;
char c;
do {
cout << "Enter a number " << endl;
cin >> n;
cout << "Enter your choice P for continous and E for exit" << endl;
cin >> c;
if (c == 'P')
{
cout << " "<<endl;
}
else if (c == 'E')
{
cout << "The number of even number you entered is: " << endl;
}
else {
cout << "Please enter a right option" << endl;
}
if (n % 2 == 0) {
count = count + 1;
}
} while (c != 'E');
cout << count;
system("pause");
}

/*Use a sentinel controlled do while loop to find the smallest of the numbers
entered by the user. Continue until the user tells you to stop.
P: Ask user for a number
E: exit the program after displaying the smallest number entered.*/
#include<iostream>
using namespace std;
void main()
{
int smallest = INT_MAX;
int n;
char c;
do {
cout << "Enter a number: " << endl;
cin >> n;
cout << "Enter your choice\nEnter P for continoUS \n Enter E for exit"
<< endl;
cin >> c;
if (c == 'E')
{
cout << "The smallest number is : " << endl;
}
else if (c == 'P')
{
cout << " ";
}
else
{
cout << "Please enter a right option" << endl;
}
if (n < smallest)
{
smallest = n;
}
} while (c != 'E');
cout << smallest << endl;
system("pause");
}
/*Use a sentinel controlled do while loop to find the largest of the numbers
entered by the user. Continue until the user tells you to stop.
P: Ask user for a number
E: exit the program after displaying the largest number entered.
*/
#include<iostream>
using namespace std;
void main()
{
int largest = INT_MIN;
int n;
char c;
do {
cout << "Enter a number: " << endl;
cin >> n;
cout << "Enter P if you want to continue \nEnter E for exit." << endl;
cin >> c;
if (c == 'P')
{
cout << " " << endl;
}
else if (c == 'E')
{
cout << "The largest number is : " << endl;
}
else
{
cout << "Please enter right option" << endl;
}
if (n > largest)
{
largest = n;
}
} while (c != 'E');
cout << largest << endl;
system("pause");
}

/*Use a sentinel controlled do while loop to calculate the product of numbers


entered by the user. Continue until the user tells you to stop.

P: Ask user for a number and include it in the product


E: exit the program after displaying the product.*/
#include<iostream>
using namespace std;
void main()
{
int n;
char c;
int product = 1;
do {
cout << "Enter a number :" << endl;
cin >> n;
cout << "Enter P for continue\n Enter E for exit" << endl;
cin >> c;
if (c == 'E')
{
cout << "The product of all the numbers is : " << endl;
}
else
{
cout << "Please enter right option" << endl;
}
product = product * n;
} while (c != 'E');
cout << product << endl;
system("pause");
}

/*Use a sentinel controlled do while loop to calculate the product of numbers


entered by the user. Continue until the user tells you to stop.

P: Ask user for a number and include it in the product


E: exit the program after displaying the product.*/
#include<iostream>
using namespace std;
void main()
{
int n;
char c;
int product = 1;
do {
cout << "Enter a number :" << endl;
cin >> n;
cout << "Enter P for continue\n Enter E for exit" << endl;
cin >> c;
if (c == 'E')
{
cout << "Program is terminated" << endl;
}
else
{
cout << "Please enter right option" << endl;
}
product = product * n;
cout << "The product of all numbers is " << product << endl;
} while (c != 'E');

system("pause");
}

/*Menu
F : Calculate factorial of the number
T: Display table of the number
S: Display square root of the number
L: log value of the number
E: Exit

Use Do While*/

#include<iostream>
#include<cmath>
using namespace std;
void main()
{
int n, factorial=1;

char c;
do {
cout << "Enter a number " << endl;
cin >> n;
cout << "Enter your choice\n F : Calculate factorial of the number\nT:
Display table of the number\nS : Display square root of the number\nL : log value
of the number\nE : Exit" << endl;
cin >> c;
if (c == 'F')
{
for (int i = n; i >= 1; i = i - 1)
{
factorial = factorial * i;
}
cout << "The factorial of the number is " << factorial << endl;
}
else if (c == 'T')
{
cout << "The table of " << n << " " << endl;
for (int i = 1; i <= 10; i++)
{
cout << n << " * " << i << " = " << i * n << endl;
}
}
else if (c == 'S')
{
cout << "The square root of the number is " << sqrt(n) << endl;
}
else if (c == 'L')
{
cout << "The logrithm of the number is " << log(n) << endl;
}
else if (c == 'E')
{
cout << "Thank you for using this program" << endl;
}
else
{
cout << "Please enter the right option " << endl;
}
} while (c != 'E');
system("pause");
}

#include<iostream>
#include<string>
using namespace std;
void main()
{
int n1;
int n2; string choice; float result;
do{

cout << "Menu" << endl;


cout << "Enter + for sum\nEnter - for substraction\nEnter * for
multipication\nEnter / for division\nEnter E for exit" << endl;
getline(cin, choice);

if (choice == "+") {
cout << "Enter first number: ";
cin >> n1;
cout << "Enter another number: ";
cin >> n2;
result = n1 + n2;
cout << "The sum of two numbers is: " << result << endl;
}
else if (choice == "-") {
cout << "Enter first number: ";
cin >> n1;
cout << "Enter another number: ";
cin >> n2;
result = n1 - n2;
cout << "The substraction of two numbers is: " << result << endl;
}
else if (choice == "*") {
cout << "Enter first number: ";
cin >> n1;
cout << "Enter another number: ";
cin >> n2;
result = n1 * n2;
cout << "The product of two numbers is: " << result << endl;
}
else if (choice == "/") {
cout << "Enter first number: ";
cin >> n1;
cout << "Enter another number: ";
cin >> n2;
result = n1 / n2;
cout << "The division of two numbers is: " << result << endl;
}
else if (choice == "E")
{
cout << "Thank you for usage!" << endl;
}
} while (choice != "E");

system("pause");
}

/*Menu
1. Character is lower case or not
2. Character is upper case or not
3. Terminate program

Use a do while loop*/


#include<iostream>
using namespace std;
void main()
{
char choice;
char character;
bool result;
do
{
cout << "Menu" << endl;
cout << "Enter A to check whether your character is in lower case or not.\
nEnter B to check whether your character is in upper case or not\nEnter E for exit
" << endl;
cin >> choice;
if (choice == 'A')
{
cout << "Enter any character " << endl;
cin >> character;
result = islower(character);
if (result == 1)
{
cout << "Your character is in lowercase " << endl;
}
else if (result == 0)
{
cout << "Your character is not in lowercase " << endl;
}
}
else if (choice == 'B')
{
cout << "Enter your character" << endl;
cin >> character;
result = isupper(character);
if (result == 1)
{
cout << "Your character is in upper case" << endl;
}
else if (result == 0)
{
cout << "Your character is not in uppercase" << endl;
}
}
else if(choice=='E')
{
cout << "Thankyou for usage!" << endl;
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 'E');
system("pause");
}

/*Menu
1. Check if even or odd
2. Check if positive or negative
3. Exit program

Use a DOWHILE loop*/


#include<iostream>
using namespace std;
void main()
{
int choice;
int number;
do
{
cout << "Menu" << endl;
cout << "Enter 1 for checking the number is even or odd\nEnter 2 for
checking the number is positive or negative\nEnter 3 for exit" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Enter a number" << endl;
cin >> number;
if (number % 2 == 0)
{
cout <<number<< " is even " << endl;
}
else if (number % 2 != 0)
{
cout << number << " is old " << endl;
}
else
{
cout << "Please enter a number" << endl;
}
}
else if (choice == 2)
{
cout << "Enter a number" << endl;
cin >> number;
if (number > 0)
{
cout << number << " is positive " << endl;
}
else if (number < 0)
{
cout << number << " is negative " << endl;
}
}
else if (choice == 3)
{
cout << "Thank you for usage!" << endl;
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 3);
system("pause");
}

/*1. Write a menu-driven program that provides three options to the user:
Performing Operations on a Character
Performing Operations on a Number
Exit
Once the user selects the desired option, the program should then display a sub-
menu. For a Character,
the program can find if the character entered is a digit, a lower-case letter, or
convert it to upper case. For
number, the program can round the number or find the absolute value of the
number.*/
#include<iostream>
using namespace std;
void main()
{
int choice;
int choice1;
float number;
char character;
bool result;
char y;
do
{

cout << "Menu\nEnter 1 for performing operations on character \nEnter 2


for performing operation on number \nEnter 3 for Exit" << endl;
cin >> choice;
if (choice == 1)
{
do{
cout << "Sub-Menu\nEnter 1 for checking whether it is digit
or not\nEnter 2 for checking whether it is in lower case or not\nEnter 3 for
converting into upper case\nEnter 4 for exit " << endl;
cin >> choice1;
if (choice1 == 1)
{
cout << "Enter a character" << endl;
cin >> character;
result = isdigit(character);
if (result == 1)
{
cout << "Your character is digit " << endl;
}
else if (result == 0)
{
cout << "Your character is not digit " << endl;
}
}
else if (choice1 == 2)
{
cout << "Enter a character " << endl;
cin >> character;
result = islower(character);
if (result == 1)
{
cout << "Your character is in lower case " <<
endl;
}
else if (result == 0)
{
cout << "Your character is not in lower case "
<< endl;
}
}
else if (choice1 == 3)
{
cout << "Enter a character" << endl;
cin >> character;
y = toupper(character);
cout << "The upper case of " << character << " is "
<< y<< endl;
}
else if (choice1 == 4)
{
cout << "Thank you for using operation on character"
<< endl;
}
else
{
cout << "Please enter right option" << endl;
}
} while (choice1 != 4);
}
else if (choice == 2)
{
do
{
cout << "Menu\nEnter 1 for rounding off the number\
nEnter 2 to convert into absolute form\nEnter 3 for exit from the processing on the
number" << endl;
cin >> choice1;
if (choice1 == 1)
{
cout << "Enter a number " << endl;
cin >> number;
cout << "The result of rounding the number " <<
number << " is " << round(number) << endl;
}
else if (choice1 == 2)
{
cout << "Enter a number " << endl;
cin >> number;
cout << "The absolute form of the number " <<
number << " is " << abs(number) << endl;

}
else if (choice1 == 3)
{
cout << "Thank you for using the processing on
number " << endl;
}
else
{
cout << "Please enter the right option" <<
endl;
}

} while (choice1 != 3);

}
else if (choice == 3)
{
cout << "Thank you for using this program " << endl;
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 3);
system("pause");
}
/*Write a program that prompts user to
enter two strings and check if those string are equal in size or not.*/
#include<iostream>
#include<string>
using namespace std;
void main()
{
string string1;
string string2;
int size1;
int size2;
cout << "Program to check out the whether that these two strings are equal in
size or not" << endl;
cout << "Enter a first string" << endl;
getline(cin, string1);
size1 = size(string1);
cout << "Enter another string " << endl;
getline(cin, string2);
size2 = size(string2);
if (size1==size2)
{
cout << "Both strings are equal in size " << endl;
}
else
{
cout << "Both string are not equal in size" << endl;
}
system("pause");
}

/*2. Initialize str1 and str2 with your first and last name respectively. Then
display your complete name
a. By using +
b. By using append*/
#include<iostream>
#include<string>
using namespace std;
void main()
{
string str1;
string str2;
cout << "Enter your first name" << endl;
getline(cin, str1);
cout << "Enter your second name" << endl;
getline(cin, str2);
cout << "Your full name is " << str1 +" "+ str2 << endl;
cout << "Your full name is " << str1.append(" ").append(str2) << endl;
system("pause");
}

/*3. Input your complete registration number. Then find CS, SE or IT in your
registration number.
If CS is found display Computer Scientist,
if SE is found display Software Engineer and if IT is found, display IT Engineer on
screen*/
#include<iostream>
#include<string>
using namespace std;
void main()
{
string registration_number;
cout << "Enter your registration number " << endl;
getline(cin, registration_number);
;
if (registration_number.find("CS")<20)
{
cout << "Computer Science" << endl;
}
else if (registration_number.find("SE")<20)
{
cout << "Software Engineering" << endl;
}
else if (registration_number.find("IT")<20)
{
cout << "Information Technology" << endl;
}
else
{
cout << "You are not attending any computer related program. " << endl;

}
system("pause");

/*Initialize str1 with �cup� and str2 with �cake�.


Then insert str2 in str1 such that the result is �cupcake�.*/
#include<iostream>
#include<string>
using namespace std;
void main()
{
string str1 ="cup";
string str2 = "cake";
cout << "The result is " << str1.insert(3,str2) << endl;
system("pause");
}

/*When a student transfers from one degree program to another,


her registration number remains same except for the degree program initials.
e.g. if 1234-FBAS-BSCS-F19 transfers to software engineering degree program her
new registration number would be 1234-FBAS-BSSE-F19. Write a C++ program that
asks the student her original registration number and her new degree program
initials.
Then your program displays her new registration number. (Include CS, SE and IT)*/
#include<iostream>
#include<string>
using namespace std;
void main()
{
string registration_number;
string new_degree_program;
cout << "Enter your original registration number" << endl;
getline(cin, registration_number);
cout << "Enter your new degree program:\nEnter SE for software engineering\
nEnter CS for computer sciences\nEnter IT for Information Technology " << endl;
getline(cin, new_degree_program);
cout << "Your new registration number is " << registration_number.replace(11,
2, new_degree_program)<<endl;
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int n1[4] = {10,20,30,40};
for (int i = 0; i <= 3; i++)
{
cout <<n1[i] << endl;
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int n1[4] = { 10,20,30,40 };
cout << "Index\tValue" << endl;
for (int i = 0; i <= 3; i++)
{
cout << i << "\t" << n[i] << endl;
}
system("pause");
}
#include<iostream>
using namespace std;
void main()
{
int n1[4] = { 10,20,30,40 };
for (int i = 3; i >= 0; i = i - 1)
{
cout << n1[i] << endl;
}
system("pause");
}

#include<iostream>
#include<string>
using namespace std;
void main()
{
string names[5];
cout << "Please enter the names of your group members" << endl;
for (int i = 0; i <= 4; i++)
{
cout << "Enter the of member of your group " << i + 1 << ": " ;
getline(cin, names[i]);
}
for (int j = 0; j <= 4; j++)
{
cout<<names[j]<<", " ;
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int number[10];
cout << "Program to check out which number is greater than 5 out of 10
numbers that you entered" << endl;
cout << "Here you enter 10 numbers" << endl;
for (int i = 0; i <= 9; i++)
{
cout << "Enter the number " << i+1 << " : ";
cin >> number[i];

}
cout << "These are the list of number that you entered which are greater than
5" << endl;
for (int i = 0; i <= 9; i++)
{
if (number[i] > 5)
{
cout << number[i] << ", ";
}
}
system("pause");

/*Declare an array of integers of size 10.


Initialize the array taking input from user.
Then display the odd array elements only.*/
#include<iostream>
using namespace std;
void main()
{
int numbers[10];

for (int i = 0; i <= 9; i++)


{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
}
for (int j = 0; j <= 9; j=j++)
{
if(numbers[j]%2!=0){
cout << numbers[j] << ", " ;
}
}
system("pause");

/*Declare an array of characters for storing P for passing students and F for
failure.
If the class contains 15 students, ask each student to enter her marks.
If her marks are less than 60, store F in the character array otherwise store P.*/
#include<iostream>
using namespace std;
void main()
{
char grade[15];
int marks[15];
for (int i = 0; i <= 14; i++)
{
cout << "Enter the marks of the student " << i + 1 << ": " << endl;
cin >> marks[i];

}
for(int j = 0; j <= 9;j++)
{
if (marks[j] >= 60)
{
grade[j] = 'P';
}
else if (marks[j] < 60)
{
grade[j] = 'F';
}
}
cout << "Sr no.\tMarks\tpass/fail" << endl;
for (int k = 0; k <= 9; k++)
{

cout << k + 1 << "\t" << marks[k] << "\t" << grade[k] << endl;
}

system("pause");
}

/*Write a C++ program to store and calculate the product of


5 numbers entered by the user using arrays.*/
#include<iostream>
using namespace std;
void main()
{
int numbers[5];
int product = 1;
for (int i = 0; i <= 4; i++)
{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
product = product * numbers[i];
}
cout << "The list of the number that u entered is : " << endl;
for (int j = 0; j <= 4; j++)
{
cout << numbers[j] << endl;
}
cout << "The product of these numbers is : " << product << endl;
system("pause");

/*Write a program that stores ten numbers as input from user in an array
and calculates their average.
Remember that average of ten numbers is equal to sum of the ten numbers divided by
10.*/
#include<iostream>
using namespace std;
void main()
{
int numbers[10];
int sum = 0;
float average;
for (int i = 0; i <= 9; i++)
{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
sum = sum + numbers[i];
}

average = sum / 10;


cout << "The average of these number is : " << average << endl;
system("pause");
}

/*Write a C++ program that takes ten numbers from users


in an array and displays the count of even numbers present in the array.*/
#include<iostream>
using namespace std;
void main()
{
int numbers[10];
int count = 0;
for (int i = 0; i <= 9; i++)
{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
if (numbers[i] % 2 == 0)
{
count = count + 1;
}

}
cout << "The count of even numbers you entered is: " << count << endl;
system("pause");
}

/* Write a C++ program to calculate and store the cube of 5 numbers entered by the
user using arrays
*/
#include<iostream>
using namespace std;
void main()
{
int numbers[5];
int cubes[5];
for (int i = 0; i <= 4; i++)
{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
cubes[i] = numbers[i]* numbers[i]* numbers[i];

}
for (int j = 0; j <= 4; j++)
{
cout << cubes[j] << ", ";
}
system("pause");
}

/*Write a C++ program that stores the factorial of


five numbers entered by user in an array of size 5.*/
#include<iostream>
using namespace std;
void main()
{
int numbers[5];
int factorials[5];

for (int i = 0; i <= 4; i++)


{
cout << "Enter a number " << i + 1 << ":";
cin >> numbers[i];
}
for (int j = 0; j <= 4; j++)
{
int factorial = 1;
for (int k = numbers[j]; k >= 1; k = k - 1)
{
factorial = factorial * k;
}
factorials[j] = factorial;
}
cout << "Numbers\tFactorials" << endl;
for (int l = 0; l <= 4; l++)
{
cout << numbers[l] << "\t" << factorials[l] << endl;
}
system("pause");
}

/*Write C++ program that performs multiplication between corresponding elements of


two arrays and stores result of multiplication in a third array. Display in tabular
format*/
#include<iostream>
using namespace std;
void main()
{
int number1[5];
int number2[5];
int product[5];
for (int i = 0; i <= 4; i++)
{
cout << "Enter a number in first array " << i + 1 << ": ";
cin >> number1[i];
}
for (int j = 0; j <= 4; j++)
{
cout << "Enter a number in second array " << j + 1 << ": ";
cin >> number2[j];
}
for (int k = 0; k <= 4; k++)
{
product[k] = number1[k] * number2[k];
}
cout << "First \tSecond \tProduct" << endl;
for (int l = 0; l <= 4; l++)
{
cout << number1[l] << "\t" << number2[l] << "\t" << product[l] << endl;

}
system("pause");
}

/*Write a C++ program that uses multiple arrays to store input of 5 numbers,
square of those numbers, square root of those numbers, double of those numbers
and halves of those numbers. Display in tabular format.
*/
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
int numbers[5],squares[5],doubles[5];
float squareroots[5], halves[5] ;
for (int i = 0; i <= 4; i++)
{
cout << "Enter the number " << i + 1 << ": ";
cin >> numbers[i];
squares[i] = numbers[i] * numbers[i];
squareroots[i] = sqrt(numbers[i]);
doubles[i] = numbers[i] + numbers[i];
halves[i] = numbers[i] / 2;
}
cout << "Numbers\tSquare\tSquareroots\tDoubles\tHalves" << endl;
for (int j = 0; j <= 4; j++)
{
cout << numbers[j] << "\t" << squares[j] <<"\t" << squareroots[j] <<"\
t\t" << doubles[j] << "\t" << halves[j] << endl;
}
system("pause");
}

/*A project consists of 5 students.


Write a C++ program that uses multiple arrays to store registration number of a
student,
first name of student, marks obtained by student and Status
(store pass or fail according to marks obtained by student).
Use appropriate data type of arrays. Display the group member�s information in
tabular format.*/
#include<iostream>
using namespace std;
void main()
{
string registration_number[5];
string name[5];
int marks[5];
string status[5];
for (int i = 0; i <= 4; i++)
{
cout << "Student " << i + 1 << endl;
cout << "Enter registration number : ";
cin >> registration_number[i];
cout << "Enter full name : ";
cin >> name[i];
cout << "Enter marks : ";
cin >> marks[i];
if (marks[i] >= 60)
{
status[i] = "Pass";
}
else if (marks[i] < 60)
{
status[i] = "Fail";
}
}
cout << "Tabular Display of all the students data" << endl;
cout << "Sr No.\tName\tRegistration No\tMarks\tStatus" << endl;
for (int j = 0; j <= 4; j++)
{
cout << j + 1 << "\t" << name[j] << "\t" << registration_number[j] <<
"\t\t" << marks[j] << "\t" << status[j] << endl;
}
system("pause");
}
/*Improve the LT3 program and display the count of students
who are pass and students who are fail in the group and average marks obtained as
well.*/
#include<iostream>
using namespace std;
void main()
{
string registration_number[5];
string name[5];
int marks[5];
string status[5];
int countpass = 0;
int countfail = 0;
int summarks = 0;
float average;
for (int i = 0; i <= 4; i++)
{
cout << "Student " << i + 1 << endl;
cout << "Enter registration number : ";
cin >> registration_number[i];
cout << "Enter full name : ";
cin >> name[i];
cout << "Enter marks : ";
cin >> marks[i];
summarks = summarks + marks[i];
if (marks[i] >= 60)
{
status[i] = "Pass";
countpass = countpass + 1;
}
else if (marks[i] < 60)
{
status[i] = "Fail";
countfail = countfail + 1;
}
}
cout << "Tabular Display of all the students data" << endl;
cout << "Sr No.\tName\tRegistration No\tMarks\tStatus" << endl;
for (int j = 0; j <= 4; j++)
{
cout << j + 1 << "\t" << name[j] << "\t" << registration_number[j] <<
"\t\t" << marks[j] << "\t" << status[j] << endl;
}
cout << "Statistics" << endl;
cout << "The number of passed students is: " << countpass << endl;
cout << "The number of failed students is: " << countfail << endl;
average = summarks / 5;
cout << "The average marks of the class is: " << average << endl;
system("pause");
}

/*Improve the LT3 program and display the count of students


who are pass and students who are fail in the group and average marks obtained as
well.*/
#include<iostream>
using namespace std;
void main()
{
string registration_number[5];
string name[5];
int marks[5];
string status[5];
int countpass = 0;
int countfail = 0;
int summarks = 0;
float average;
int largest = INT_MIN;
int smallest = INT_MAX;
for (int i = 0; i <= 4; i++)
{
cout << "Student " << i + 1 << endl;
cout << "Enter registration number : ";
cin >> registration_number[i];
cout << "Enter full name : ";
cin >> name[i];
cout << "Enter marks : ";
cin >> marks[i];
summarks = summarks + marks[i];
if (marks[i] >= 60)
{
status[i] = "Pass";
countpass = countpass + 1;
}
else if (marks[i] < 60)
{
status[i] = "Fail";
countfail = countfail + 1;
}
if (marks[i] > largest)
{
largest = marks[i];
}
if (marks[i] < smallest)
{
smallest = marks[i];
}
}
cout << "Tabular Display of all the students data" << endl;
cout << "Sr No.\tName\tRegistration No\tMarks\tStatus" << endl;
for (int j = 0; j <= 4; j++)
{
cout << j + 1 << "\t" << name[j] << "\t" << registration_number[j] <<
"\t\t" << marks[j] << "\t" << status[j] << endl;
}
cout << "Statistics" << endl;
cout << "The number of passed students is: " << countpass << endl;
cout << "The number of failed students is: " << countfail << endl;
average = summarks / 5;
cout << "The average marks of the class is: " << average << endl;
cout << "The highest marks of the class is: " << largest << endl;
cout << "The lowest marks of the class is: " << smallest << endl;
system("pause");
}

/*A family consists of 7 members. Write a C++ program that uses multiple arrays to
store first name,\
last name, age, height, weight, BMI value (store according to formula given) and
comment
(store comment according to the value of bmi).
A BMI (body mass index) value can be calculated using the formula
BMI = Weight in kilograms � (height in meters � height in meters)
Store �Underweight� in the comments array if the BMI result is less than 18.5
Store �Normal� in the comments array if the BMI result is between 18.5 and 24.9
Store �Overweight� in the comments array if the BMI result is between 25 and 29.9
Store �Obese� in the comments array if the BMI result is 30 or greater.
Use appropriate data type of arrays. Display the family member�s information in
tabular format.*/
#include<iostream>
using namespace std;
void main()
{
string firstname[7],
lastname[7],comments[7],oldest,youngest,highestbmi,tallest;
int age[7], height[7],
weight[7],largest=INT_MIN,smallest=INT_MAX,countunderweight=0,sumheight=0;

float bmi[7], averageheight = 0;

for (int i = 0; i <= 6; i++)


{
cout << "Member No." << i + 1 << endl;
cout << "Enter first name:";
cin >> firstname[i];
cout << "Enter last name: ";
cin >> lastname[i];
cout << "Enter age in years";
cin >> age[i];
if (age[i] > largest)
{
largest = age[i];
oldest = firstname[i]+" "+lastname[i];
}
if (age[i] < smallest)
{
smallest = age[i];
youngest= firstname[i] + " " + lastname[i];
}
cout << "Enter height in meters";
cin >> height[i];
sumheight = sumheight + height[i];
if (height[i] > largest)
{
largest = height[i];
tallest= firstname[i] + " " + lastname[i];
}
cout << "Enter weight in kilograms";
cin >> weight[i];
bmi[i] = weight[i] * (height[i] * height[i]);
if (bmi[i] > largest)
{
largest = bmi[i];
highestbmi= firstname[i] + " " + lastname[i];
}
if (bmi[i] < 18.5)
{
comments[i] = "Underweight";
countunderweight = countunderweight + 1;
}
else if (bmi[i] >= 18.5 && bmi[i] < 24.9)
{
comments[i] = "Normal";
}
else if (bmi[i] >= 24.9 && bmi[i] < 29.9)
{
comments[i] = "Overweight";
}
else if (bmi[i] >= 29.9 )
{
comments[i] = "Obese";
}

}
cout << "Sr No.\tFirst Name\tLast Name\tAge\tHeight\tWeight\tBmi\tStatus" <<
endl;
for (int j = 0; j <= 6; j++)
{
cout << j + 1 << "\t" << firstname[j] << "\t\t" << lastname[j] << "\t\
t" << age[j] << "\t" << height[j] << "\t" << weight[j] << bmi[j] <<
comments[j]<<endl;

}
averageheight = sumheight / 7;
cout << "The oldest person of the family is : " << oldest << endl;
cout << "The youngest person of the family is : " << youngest << endl;
cout << "The highest BMI holder of the family is: " << highestbmi << endl;
cout << "The tallest member of the family is: " << tallest << endl;
cout << "The count of underweight members of family is : " <<
countunderweight << endl;
cout << "The average height of all the members of family is: " <<
averageheight << endl;
system("pause");
}

#include <iostream>
using namespace std;
int main()
{
int test[3][2] = { {2, -5 },{4,0},{9, 1} }; // declare and initialize the array
for (int i = 0; i < 3; ++i) //for each of the 3 rows
{
for (int j = 0; j < 2; ++j) //for each of the 2 columns
{
cout << "test[" << i << "][" << j << "] = " <<test[i][j]<<endl;
}
cout << endl;
}
return 0;
}

#include<iostream>
using namespace std;
void main()
{
int twodarray[3][2] = { {2,-5},{4,0},{9,1} };
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "test [" << i << "][" << j << "] = " << twodarray[i][j]
<< endl;
}
}
system("pause");
}

#include<iostream>
using namespace std;

void main()
{
int mat[2][3]; //matrix can have max 2 rows and 3 cols
//inputting values
for (int i = 0; i < 2; i++) //outer loop iterates over each row
{
for (int j = 0; j < 3; j++) //inter loop iterates over each column
{
cout << " Enter value of" << "mat[" << i << "][" << j << "] :";
cin >> mat[i][j];
}
}
//outputting values
cout << endl << endl << "MATRIX STORED" << endl;
for (int i = 0; i < 2; i++) // each row
{
for (int j = 0; j < 3; j++) //each column
{
cout << mat[i][j] << "\t";
}
cout << endl;
}
system("pause");
}

#include<iostream>
using namespace std;
void main()
{
int arr[2][3];
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 2; j++)
{
cout << "Enter the value of [" << i << "][" << j << "] : ";
cin >> arr[i][j];
}
}
cout << "Matrix Stored" << endl;
for (int k = 0; k <= 1; k++)
{
for (int l = 0; l <= 2; l++)
{
cout << arr[k][l] << "\t";
}
cout << endl;
}
system("pause");
}

/*1)
Declare and initialize a 2D
array having 3 rows and 2 columns. Display the last row only.*/
#include<iostream>
using namespace std;
void main()
{
int arr[3][2] = { {6,5},{4,3},{2,1} };
cout << "Last row"<<endl;
for (int i = 2; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr[i][j]<<"\t";
}
}
system("pause");
}

/*Declare a 2D array having 3


rows and 2 columns. Initialize the array taking input from user. Display the
last row only.*/
#include<iostream>
using namespace std;
void main()
{
int arr[3][2];
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter the value of [" << i << "][" << j << "]: ";
cin >> arr[i][j];
}
}
cout << "last row" << endl;
for (int i = 2; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr[i][j] << "\t";
}
}
system("pause");
}

/*Declare a 2D array having 3 rows


and 2 columns. Initialize the array taking input from user. Display the first
column only.*/
#include<iostream>
using namespace std;
void main()
{
int arr[3][2];
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter the value of [" << i << "][" << j << "]=" ;
cin >> arr[i][j];
}
}
cout << "first column" << endl;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 0; j++)
{
cout << arr[i][j] << endl;
}
}
system("pause");
}

/*Declare a 2D array (matrix)


having 4 rows and 4 columns. Initialize the array taking input from user.
Display the values on the diagonal.
*/
#include<iostream>
using namespace std;
void main()
{
int arr[4][4];
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
cout << "Enter the value of [" << i << "][" << j << "]= ";
cin >> arr[i][j];
}
cout << endl;
}
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
if(i==j){
cout << arr[i][j]<<"\t";
}
}
cout << endl;
}
system("pause");
}

/*Declare a 2D array of integers


having 5 rows and 2 columns. Initialize the array taking input from user. Then
display the array elements having values greater than 5 on screen.
*/
#include<iostream>
using namespace std;
void main()
{
int arr[5][2];
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter a value of [" << i << "][" << j << "]=" ;
cin >> arr[i][j];
}
cout << endl;
}
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 1; j++)
{
if (arr[i][j] > 5)
{
cout << arr[i][j]<<"\t";
}
}
cout << endl;
}
system("pause");
}

/*Now declare a 3D array of size


of your own choice. Input data into it. Display data from it. (Hint you will
need to specify three sizes during declaration, you need three loops for inputs
as well as for output)*/
#include <iostream>
using namespace std;
void main()
{
int arr[3][3][3];
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
for (int k = 0; k <= 2; k++)
{
cout << "Enter the value of array of [" << i << "][" << j
<< "][" << k << "]=";
cin >> arr[i][j][k];
}
}
}
cout << "3d matrix stored" << endl;
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
for (int k = 0; k <= 2; k++)
{
cout << arr[i][j][k] << "\t";
}
cout << endl;
}
cout << endl;
}
system("pause");
}

/*Declare a 2D array of integers


having 2 rows and 5 columns. Check if the elements in the first and second rows
are same. If they are completely same display �identical� otherwise display
�not identical�.
*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][5];
int identical = 0;

for (int i = 0; i <= 1; i++)


{
for (int j = 0; j <= 4; j++)
{
cout << "Enter the value of array of [" << i << "][" << j <<
"]=";
cin >> arr[i][j];
}
cout << endl;
}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 4; j++)
{
if (arr[0][j] == arr[1][j])
{
identical = identical + 1;
}

}
cout << endl;
}

if (identical ==10 )
{
cout << "Identical" << endl;
}
else if (identical < 10)
{
cout << "Not identical" << endl;
}
system("pause");
}

/*Declare a 2D array of integers


having 2 rows and 5 columns. Check if the elements in the first row and first
column are same. If they are completely same display �identical� otherwise
display �not identical�.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][5];
int identical = 0;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 4; j++)
{
cout << "Enter the value of array of [" << i << "][" << j <<
"]=";
cin >> arr[i][j];
}
cout<<endl;
}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 4; j++)
{
if (arr[0][j] == arr[i][2])
{
identical = identical + 1;
}
}
cout << endl;
}
cout << identical;
if (identical >= 2)
{
cout << "identical" << endl;
}
else if (identical < 2)
{
cout << "not identical" << endl;
}
system("pause");
}
/*Declare a 2D array of integers
having 2 rows and 5 columns. Initialize the array taking input from user. Then
display a message telling whether all elements in 2nd column are 0
or not.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][5];
int zero = 0;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 4; j++)
{
cout << "Enter a value of array of [" << i << "][" << j << "]=";
cin >> arr[i][j];
}
cout << endl;
}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 4; j++)
{
if (arr[i][1] == 0)
{
zero = zero + 1;
}
}
cout << endl;
}
if (zero >= 2)
{
cout << "All the elements in 2 nd coloumn are 0" << endl;
}
else
{
cout << "All the elements in 2nd column are not 0" << endl;
}
system("pause");
}

/*Write a C++ program that initializes a 2D of 4 rows and 3 columns.


Display the sum of all elements of first column only.
*/
#include<iostream>
using namespace std;
void main()
{
int arr[4][3];
int sum = 0;
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 2; j++)
{
cout << "Enter a value of array of ["<<i<<"]["<<j<<"]=";
cin >> arr[i][j];
}
cout << endl;
}
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 2; j++)
{
if (j==0)
{
sum = sum + arr[i][0];
}
}
cout << endl;
}
cout << "The sum of all the elements in first column is " << sum << endl;
system("pause");
}

/*Write a C++ program that declares a 2D array of size 3x4.


Then display the average of all elements in last row only.
*/
#include<iostream>
using namespace std;
void main()
{
int sum = 0;
float averrage;
int arr[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 3; j++)
{
if (i == 2)
{
sum = sum + arr[i][j];
}
}
}
averrage = sum / 4;
cout << "The average of all the elements in a last row is: " << averrage <<
endl;
system("pause");
}

/*Write a C++ program that declares a 2D array of size 3x4.


Then display the product of all elements in the whole array.*/
#include<iostream>
using namespace std;
void main()
{
int product = 1;
int arr[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 3; j++)
{
product = product * arr[i][j];
}
}
cout << "The product of all the elements of the array is " << product <<
endl;
system("pause");

/*Write a program that displays the count of all the even elements
for each column in a matrix. The matrix has four rows and two columns.
(two columns means two count values need to be calculated)*/
#include<iostream>
using namespace std;
void main()
{
int arr[4][2] = { {1,2} ,{3,4} ,{5,6} ,{7,8} };
int count1 = 0, count2 = 0;
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr[i][j] << "\t";

}
for (int k = 0; k <= 0; k++)
{
if (arr[i][0] % 2 == 0)
{
count1 = count1 + 1;
}
}
for (int k = 1; k <= 1; k++)
{
if (arr[i][1] % 2 == 0)
{
count2 = count2 + 1;
}
}
cout << endl;
}
cout << "The count of even numbers in first column is: " << count1 << endl;
cout << "The count of even numbers in second column is: " << count2 << endl;
system("pause");
}
/*Write a C++ program that input values from user but stores the square of those
values in a 2D array.
E.g. if a user enters 3, you should store 6 in the array. Size of array is 2x2.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][2];
int arr1[2][2];
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter a value of array of [" << i << "][" << j << "]= ";
cin >> arr[i][j];
if (arr[i][j] == 3)
{
arr1[i][j] = arr[i][j] * 2;
}
else
{
arr1[i][j] = arr[i][j];
}
}
cout << endl;
}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr1[i][j]<<"\t";
}
cout << endl;
}
system("pause");

/*A determinate of a 2x2 matrix is calculated


using the formula given. Write a C++ program that inputs a
2x2 matrix and displays its determinate.
Display whether a matrix is singular or not.
A matrix is singular if its determinate is zero.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][2];
int product = 1;
int product1 = 1;
int determinant;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter the value of the array of [" << i << "][" << j <<
"]=";
cin >> arr[i][j];
if (i == j)
{
product = product * arr[i][j];
}
else
{
product1 = product1 * arr[i][j];
}
}
cout << endl;
}
determinant = product - product1;
cout << "The determinant of the matrix: " << determinant << endl;
if (determinant == 0)
{
cout << "Matrix is singular" << endl;
}
else
{
cout << "Matrix is not singular" << endl;
}
system("pause");
}

/*The transpose of a matrix is a new matrix whose rows are


the columns of the original and vice versa.
Write a C++ program that displays transpose of a square matrix.
*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][2];
int arr1[2][2];
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter a value of the array of [" << i << "][" << j <<
"]=";
cin >> arr[i][j];
arr1[j][i] = arr[i][j];
}
cout << endl;
}
cout << "The matrix is " << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << "The transpose of the matrix is " << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr1[i][j] << "\t";
}
cout << endl;
}
system("pause");
}

/*1)Write a C++ program that initializes a 2x2 matrix from user. Display the
following menu
a. Press T to calculate transpose of matrix
b. Press S to check if it is singular matrix
c. Press D to calculate determinate of matrix
d. Press I to check if it is an identity matrix
e. Press E to exit
The program must continue execution repeatedly until the user enters E to exit.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][2];
int arr1[2][2];
int sum = 0,count=0,count1=0;
int product = 1;
int product1 = 1;
char choice;
int determinant;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter the value of array of [" << i << "][" << j <<
"]=";
cin >> arr[i][j];
arr1[j][i] = arr[i][j];
if (i == j)
{
product = product * arr[i][j];
}
else
{
product1 = product1 * arr[i][j];
}
}
cout << endl;
}
determinant = product1 - product;
do{
cout << "Menu\nPress T to calculate transpose of matrix\nPress S to check if
it is singular matrix \nPress D to calculate determinate of matrix \nPress I to
check if it is an identity matrix \nPress E to exit " << endl;
cin >> choice;
if(choice =='T'){
cout << "The transpose of the matrix is :" << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr1[i][j]<<"\t";
}
cout << endl;
}
}

else if (choice == 'D')


{
cout << "The determinant of the matrix is :" << determinant << endl;
}
else if(choice=='S'){
if (determinant == 0)
{
cout << "The matrix is singular " << endl;
}
else
{
cout << "The matrix is non singular" << endl;
}
}
else if(choice=='I')
{

for (int i = 0; i <= 1; i++)


{
for (int j = 0; j <= 1; j++)
{
if (i == j)
{
if (arr[1][1] = arr[i][j])
{
count = count + 1;
}
}
else
{
if (arr[i][j] == 0)
{
count1 = count1 + 1;
}
}
}
if (count >= 2 && count1 >= 2)
{
cout << "The matrix is identity matrix " << endl;
}
else
{
cout << "The matrix is not identity matrix" << endl;
}

}
}
else if (choice = 'E')
{
cout << "Thank you for using this program!" << endl;
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 'E');
system("pause");
}

/*2)Write a C++ program that initializes two 2x2 matrices from user.
Display the following menu
a. Press A to add both matrices
b. Press S to subtract both matrices
c. Press M to multiply first matrix by user entered number
d. Press D to divide second matrix by user entered number
e. Press E to exit
The program must continue execution repeatedly until the user enters E to exit.*/
#include<iostream>
using namespace std;
void main()
{
int arr[2][2];
int arr1[2][2];
int sum[2][2];
int sub[2][2];
int multiply1[2][2];
int multiply;
int divide1[2][2];
int divide;
char choice;
cout << "Enter the value of the matrix 1" << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter a value of array of [" << i << "][" << j << "]=";
cin >> arr[i][j];
}
cout << endl;
}
cout << "Enter the value of the matrix 2" << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << "Enter a value of array of [" << i << "][" << j << "]=";
cin >> arr1[i][j];
}
cout << endl;
}
cout << "The first matrix is :" << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr[i][j] << "\t";
}
cout << endl;
}
cout << "The second matrix is :" << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << arr1[i][j] << "\t";
}
cout << endl;
}

do{
cout << "Menu\n Press A to add both matrices \n Press S to subtract both
matrices \n Press M to multiply first matrix by user entered number \n. Press D to
divide second matrix by user entered number \nPress E to exit " << endl;
cin >> choice;
if (choice == 'A')
{
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
sum[i][j] = arr[i][j] + arr1[i][j];
}
cout << endl;
}
cout << "The sum of both matrixes is : " << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << sum[i][j] << "\t";
}
cout << endl;
}
}
else if (choice == 'S')
{
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
sub[i][j] = arr[i][j] - arr1[i][j];
}
cout << endl;
}
cout << "The substraction of both matrixes is : " << endl;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << sub[i][j] << "\t";
}
cout << endl;
}
}
else if (choice == 'M')
{
cout << "Enter the value by which you want to multiply the matrix " <<
endl;
cin >> multiply;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
multiply1[i][j] = multiply * arr[i][j];
}

}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << multiply1[i][j] << "\t";
}
cout << endl;
}

}
else if (choice == 'D')
{
cout << "Enter the value by which you want to divide the matrix " <<
endl;
cin >> divide;
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
divide1[i][j] = arr[i][j]/divide;
}

}
for (int i = 0; i <= 1; i++)
{
for (int j = 0; j <= 1; j++)
{
cout << divide1[i][j] << "\t";
}
cout << endl;
}

}
else if (choice == 'E')
{
cout << "Thank you for using this program" << endl;
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 'E');
system("pause");
}

//C++ Program that assigns data to members of a structure variable and display it.
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
string ID;
string city;
int age;
char grade;
};
void main()
{
student s1;
cout << "Enter your name: " << endl;
getline(cin, s1.name);
cout << "Enter your ID: " << endl;
getline(cin, s1.ID);
cout << "Enter your city name: " << endl;
getline(cin, s1.city);
cout << "Enter your age: " << endl;
cin >> s1.age;
cout << "Enter your grades; " << endl;
cin >> s1.grade;

cout << "All the information about student no:1 " << endl;
cout << "Name: " << s1.name << endl;
cout << "ID: " << s1.ID << endl;
cout << "City: " << s1.city << endl;
cout << "Age: " << s1.age << endl;
cout << "Grades: " << s1.grade << endl;

system("pause");
}

/*Create a struct Book, with fields: name,


author, numberOfPages. Declare three structure variables and create THREE books
which are initialized with the following values at declaration.
Name:
ABC, Author: ABC, numberOfPages: 100
Name:
DEF, Author: DEF, numberOfPages: 1000
Name:
GHI, Author: GHI, numberOfPages: 500
Now
print data of all attributes of all books on screen.
*/
#include<iostream>
using namespace std;
struct Book
{
string name, author;
int numberofPages;
};
void main()
{
Book b[3];
b[0] = {"ABC","ABC",100};
b[1] = {"DEF","DEF",1000};
b[2] = { "GHI","GHI",500 };

for (int i = 0; i <= 2; i++)


{
cout << "Name:\n" << b[i].name << " Author: " << b[i].author << "
Number of Pages: " << b[i].numberofPages << endl;
}
system("pause");
}
/*Create a struct Rectangle which contains TWO
fields width and height. Declare two rectangles, one with width: 4 height: 10,
other with width: 3.5 height: 35.9. Print area of both rectangles.*/
#include<iostream>
using namespace std;
struct Rectangle
{
float height, width,area ;

};
void main()
{
Rectangle r[2];
r[0] = { 4,10 };
r[1] = { 3.5,35.9 };
for (int i = 0; i <= 1; i++)
{
r[i].area = r[i].width * r[i].height;
cout << "The area of rectangle of " << i + 1 << " = " << r[i].area <<
endl;
}

system("pause");
}

/*Create a structure called Employee that


contains three members id, name and salary. Ask user to fill in data for three
employees and then display information for each employee.*/
#include<iostream>
#include<string>
using namespace std;
struct Employee
{
string id, name;
int salary;
};
void main()
{
Employee e[3];
for (int i = 0; i <= 2; i++)
{
cout << "Employee " << i + 1 << endl;

cout << "Enter the name: " << endl;


getline(cin, e[i].name);
cin.ignore(-1);
cout << "Enter the ID: " << endl;
getline(cin, e[i].id);
cout << "Enter the salary: " << endl;
cin >> e[i].salary;
cin.ignore();
}
for (int i = 0; i <= 2; i++)
{
cout << "Information about " << i + 1 << "Employee" << endl;
cout << "Name: " << e[i].name << "\nID:" << e[i].id << "\nSalary: " <<
e[i].salary <<"\n" << endl;
}
system("pause");
}

/*A phone number, such as (212) 767-8900, can be


thought of as having three parts: the area code (212), the exchange (767), and
the number (8900). Write a program that uses a structure to store these three
parts of a phone number separately. Create two structure variables of type
phone. Initialize one during declaration, and have the user input values for
the other one. Then display both numbers like the example given below. The
interchange might look like this:

Enter your area code 415

Enter your Exchange 555

Enter your number: 1212

My number is
(212) 767-8900

Your number is (415)


555-1212*/
#include<iostream>
using namespace std;
struct number {
int areacode, exchange, number;
};
void main()
{
number n;
cout << "Enter the area code: " << endl;
cin >> n.areacode;
cout << "Enter the Exchange: " << endl;
cin >> n.exchange;
cout << "Enter the number: " << endl;
cin >> n.number;
cout << "Your number is : (" << n.areacode << ")" << n.exchange << "-" <<
n.number << endl;
system("pause");
}
/*Create a struct named University, with attributes: name, city, isGovernment.
Declare THREE
universities, assign values to these universities by taking them from user and
print only government universities.
Test
case:Enter university name: IIUI
Enter city: Islamabad
Is university government? (Y/N): Y
Enter university name: AU
Enter city: Islamabad
Is university government? (Y/N): Y
Enter university name: UOL
Enter city: Lahore
Is university government? (Y/N): N
Government universities are:
IIUI*/
#include<iostream>
#include<string>
using namespace std;
struct University
{
string name, city;
char isGovernment;
};
void main()
{
University u[3];
for (int i = 0; i <= 2; i++)
{
cout << "Enter university name: ";
getline(cin, u[i].name);
cin.ignore(-1);
cout << "Enter city: ";
getline(cin, u[i].city);
cin.ignore(-1);
cout << "Is university government? (Y/N):";
cin >> u[i].isGovernment;
cin.ignore();
}
cout << "Goverment universities are: " << endl;
for (int i = 0; i <= 2; i++)
{
if (u[i].isGovernment == 'Y')
{
cout << i + 1 << ": " << u[i].name<<endl;
}
}
system("pause");

/*(Array inside Structure) Create a struct Student with fields: name,


registrationNumber, subjects and semester. A student can register 6 subjects
therefore an array is required.

Declare 5 students. Assign values to the


attributes of these students by taking them from user. And then print data of
all the students with 2nd semester only.*/
#include<iostream>
using namespace std;
struct student {
string name, registrationumber,subjects[6];
int semester;
};
void main()
{
student s[5];
s[0] = { "afshan","490-FBAS/BSIT/F20",
{"pf","iti","itm","qs","ecai","maths"},2 };
s[1] = { "arslan","23-ics",{"comp","maths","phy","eng","is","urdu"},1 };
s[2] = { "aiman","5th",{"engl","urdu","maths","gk","sci","isl"},2 };
for (int i = 0; i <= 2; i++)
{
if (s[i].semester == 2)
{
cout << endl;
cout<<"Name: "<<s[i].name<<"\nRegistration Number:
"<<s[i].registrationumber<<"\n";
for (int j = 0; j <= 5; j++)
{
cout << j + 1 << ": " << s[i].subjects[j] << endl;
}
cout << "Semester: " << s[i].semester << endl;
}
}
system("pause");
}

/*(Array inside Structure) Create a struct Student with fields: name,


registrationNumber,
subjects, hobbies and semester. A student can register 6 subjects therefore an
array is required. Similarly, a student can have top three hobbies so we need
and array for that too. Declare 5 students. Assign values to the attributes of
these students by taking them from user. And then print data of all the
students studying Programming Fundamentals.*/
/*(Menu based Question) ) Create a struct Student with fields: name,
registrationNumber, subjects, hobbies
and semester. A student can register 6 subjects therefore an array is required.
Similarly, a student can have top three hobbies so we need and array for that
too. Declare 5 students. Assign values to the attributes of these students by
taking them from user. After inputting values of all 5 students from user, display
a menu
having following options
a. Press P to print data of all students studying
Programming Fundamentals
b. Press C to print data of all students studying
Calculus
c. Press U to print data of all students studying
Understanding Quran
d. Press S to search for a student when user
enters his reg No. Display his data on screen. Else display student not found.
e. Press E to exit.
The program repeatedly executes by showing
menu and displaying data as per user choice until the user presses E to exit.
*/
#include <iostream>
#include<string>
using namespace std;
struct student
{
string name, registrationnumber, subjects[6], hobbies[3];
int semester;
};
void main()
{
char choice; string reg_no;
student s[5];
for (int i = 0; i <= 4; i++)
{
cout << "Enter the name : ";
getline(cin, s[i].name);
cin.ignore(-1);
cout << "Enter the registration number: ";
getline(cin, s[i].registrationnumber);
cin.ignore(-1);
cout << "Enter your semester: ";
cin >> s[i].semester;
cin.ignore();
cout << "Enter names of your subjects" << endl;
for (int j = 0; j <= 5; j++)
{
cout << j + 1 << ": ";
getline(cin, s[i].subjects[j]);
cin.ignore(-1);
}
cout << "Enter names of your hobbies" << endl;
for (int k = 0; k <= 2; k++)
{
cout << k + 1 << ": ";
getline(cin, s[i].hobbies[k]);
cin.ignore(-1);
}
}
do {
cout << "Menu\n Press P to print data of all students studying
Programming Fundamentals\n Press C to print data of all students studying
calculus\nPress U to print data of all students studying Understanding Quran\n
Press S to search for a student when user enters his reg No.\n Press E to exit." <<
endl;
cin >> choice;
cin.ignore();
if(choice=='P')
{
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 5; j++)
if (s[i].subjects[j] == "Programming Fundamentals")
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " <<
s[i].registrationnumber << endl;
cout << "Semester" << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int k = 0; k <= 5; k++)
{
cout << k << ": " << s[i].subjects[k] << endl;
}
cout << "Hobbies: " << endl;
for (int l = 0; l <= 2; l++)
{
cout << l << ": " << s[i].hobbies[l] << endl;
}
}

}
}
else if (choice == 'C')
{
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 5; j++)
if (s[i].subjects[j] == "Calculus")
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " <<
s[i].registrationnumber << endl;
cout << "Semester" << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int k = 0; k <= 5; k++)
{
cout << k << ": " << s[i].subjects[k] <<
endl;
}
cout << "Hobbies: " << endl;
for (int l = 0; l <= 2; l++)
{
cout << l << ": " << s[i].hobbies[l] <<
endl;
}
}

}
}
else if (choice == 'U')
{
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 5; j++)
if (s[i].subjects[j] == "Understanding Quran")
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " <<
s[i].registrationnumber << endl;
cout << "Semester" << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int k = 0; k <= 5; k++)
{
cout << k << ": " << s[i].subjects[k] <<
endl;
}
cout << "Hobbies: " << endl;
for (int l = 0; l <= 2; l++)
{
cout << l << ": " << s[i].hobbies[l] <<
endl;
}
}

}
}
else if (choice == 'S')
{
cout << "Enter the registration number of the desired student: "
<< endl;
getline(cin, reg_no);
cin.ignore(-1);
for (int i = 0; i <= 4; i++)
{

if (s[i].registrationnumber==reg_no)
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " <<
s[i].registrationnumber << endl;
cout << "Semester" << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int k = 0; k <= 5; k++)
{
cout << k << ": " << s[i].subjects[k] <<
endl;
}
cout << "Hobbies: " << endl;
for (int l = 0; l <= 2; l++)
{
cout << l << ": " << s[i].hobbies[l] <<
endl;
}
}

}
}
} while (choice != 'E');
system("pause");
}

/*Create a structure called Employee that contains


three members id, full name and salary. The member full name itself is a
structure containing first name and last name.
Ask user to fill in data for three employees and then display
information for each employee.*/
#include <iostream>
#include<string>
using namespace std;
struct Employee
{
string id;
int salary;
fullname f;
};
struct fullname {
string first_name;
string last_name;
};
void main()
{
Employee e[3];

for (int i = 0; i <= 2; i++)


{
cout << "Enter your first name: " << endl;
getline(cin, e[i].f.first_name);
cin.ignore(-1);
cout << "Enter your last name: " << endl;
getline(cin, e[i].f.last_name);
cin.ignore(-1);
cout << "Enter your ID: " << endl;
getline(cin, e[i].id);
cin.ignore(-1);
cout << "Enter your salary: " << endl;
cin >> e[i].salary;
cout << "\n\n\n" << endl;
}
for (int i = 0; i <= 2; i++)
{
cout << "Information about employee " << i + 1 << endl;
cout << "Name: " << e[i].f.first_name + e[i].f.last_name << endl;
cout << "ID: " << e[i].id << endl;
cout << "Salary: " << e[i].salary << endl;
cout << "\n\n\n" << endl;
}
system("pause");
}

/*Create a structure called Employee that


contains four members id, full name, skills and salary. The member full name
itself is a structure containing first name and last name. An employee is
required to tell his top three skills so an array is required. Ask user to fill
in data for three employees and then display information for each employee. */
#include <iostream>
#include<string>
using namespace std;
struct Employee
{
string id;
int salary;
fullname f;
string skills[3];
};
struct fullname {
string first_name;
string last_name;
};
void main()
{
Employee e[3];

for (int i = 0; i <= 2; i++)


{
cout << "Enter your first name: " << endl;
getline(cin, e[i].f.first_name);
cin.ignore(-1);
cout << "Enter your last name: " << endl;
getline(cin, e[i].f.last_name);
cin.ignore(-1);
cout << "Enter your ID: " << endl;
getline(cin, e[i].id);
cin.ignore(-1);
cout << "Enter your salary: " << endl;
cin >> e[i].salary;
cin.ignore();
cout << "Enter your skills" << endl;
for (int j = 0; j <= 2; j++)
{
cout << j + 1 << ": ";
getline(cin, e[i].skills[j]);
cin.ignore(-1);
}
cout << "\n\n\n" << endl;
}
for (int i = 0; i <= 2; i++)
{
cout << "Information about employee " << i + 1 << endl;
cout << "Name: " << e[i].f.first_name + e[i].f.last_name << endl;
cout << "ID: " << e[i].id << endl;
cout << "Salary: " << e[i].salary << endl;
cout << "\n\n\n" << endl;
}
system("pause");
}

#include<iostream>
using namespace std;
void show(char Parameterch);
void main()
{
show('a');
system("pause");
}
void show(char Parameterch)
{
cout << "My function is showing my parameter " << Parameterch << endl;
}

#include<iostream>
#include<string>
using namespace std;
void show(char Parameterch, string name, int age );
void main()

{
show('a',"Afshan Yasmeen",19);
system("pause");
}
void show(char Parameterch, string name, int age)
{
cout << "My function is showing my parameter " << Parameterch << endl;
cout << "My name is : " << name << endl;
cout << "My age is : " << age << endl;
}

#include<iostream>
using namespace std;
void show(int number);
void main()
{
for (int i = 0; i <= 4; i++)
{
show(i);
}
system("pause");
}
void show(int number)
{
cout << "My number is " << number << endl;
}

/*Write a C++ program that


has four functions namely welcome, mySelf, transcriptMessage and myUniversity.
a. Function welcome display a
welcome message for the user. It takes no parameter and returns no value.

b. Function mySelf takes two


parameters name and year of birth. It prints name on screen and uses year of
birth to calculate current age of user and dispalys age on screen as well.

c. Function myUniversity
takes the short name of your university as parameter. If the parameter is IIUI
then it prints International Islamic University Islamabad on screen otherwise
it prints University not recognizable.

d. Function transcriptMessage which takes a student�s cgpa as its parameter


and
displays relevant transcript message according to the following criteriaa.
Cgpa between 0.0 to 0.99 -- Failed Semesterb.
Cgpa between 1.0 to 1.99 � On probationc.
Cgpa between 2.0 to 2.99�Satisfactoryd.
Cgpa between 3.0 to 4.0 �Good Performance
*/
#include<iostream>
#include<string>
using namespace std;
void welcome(void);
void myself(string name, int year_of_birth);
void myUniversity(string short_name);
void transcriptMessage(float students_cgpa);
void main()
{
int year_of_birth1,age,current_year; string name1,university;
float cgpa;
welcome();
cout << "Enter your name: ";
getline(cin, name1);
cin.ignore(-1);
cout << "Enter your year of birth: ";
cin >> year_of_birth1;
cin.ignore();
cout << "Enter the current year: ";
cin >> current_year;
cin.ignore();
age = current_year - year_of_birth1;
cout << "Enter your university short name: ";
getline(cin, university);
cin.ignore(-1);
cout << "Enter your cgpa: ";
cin >> cgpa;
cin.ignore();

myself(name1,age);
myUniversity(university);
transcriptMessage(cgpa);
system("pause");
}
void welcome(void)
{
cout << "************ASSALAM-O-ALIKUM************\n************Welcome to our
Program!************" << endl;
}
void myself(string name, int year_of_birth)
{
cout << "Your name is: " << name << endl;
cout << "Your age is: " << year_of_birth << endl;
}
void myUniversity(string short_name)
{
cout << "Your university's short name is:" << short_name << endl;
if (short_name == "IIUI" || short_name == "iiui")
{
cout << "Your university's full name is : Islamic International
University Islamabad" << endl;
}
else
{
cout << "University is not recognizable" << endl;
}
}
void transcriptMessage(float students_cgpa)
{
cout << "Your cgpa is : " << students_cgpa << endl;
if (students_cgpa >= 0.0 && students_cgpa <= 0.99)
{
cout << "You failed the semester" << endl;
}
else if (students_cgpa >= 1.00 && students_cgpa <= 1.99)
{
cout << "You results are On probationc" << endl;
}
else if (students_cgpa>=2.00&& students_cgpa<=2.99)
{
cout << "Your results are Satisfactory" << endl;
}
else if (students_cgpa >= 3.00 && students_cgpa <= 4.00)
{
cout << "Your are performing good" << endl;
}
else
{
cout << "Invalid" << endl;
}
}

/*Write a C++ program that


has a function display which takes your name and marks as parameters. It then
displays your name and a message of Pass or Fail depending on the marks. If
your marks are below 60, the function should print your name and Status Fail on
screen otherwise it should print your name and Status Pass on screen.
*/
#include<iostream>
#include<string>
using namespace std;
void self(string name, int marks);
void main()
{
string name1; int marks1;
cout << "Enter your name: ";
getline(cin, name1);
cin.ignore(-1);
cout << "Enter your marks: ";
cin >> marks1;
cin.ignore();
self(name1, marks1);
system("pause");
}
void self(string name, int marks)
{
cout << "Your name is: "<<name<<endl;
if (marks >= 60)
{
cout << "Your status is pass" << endl;
}
else if (marks < 60)
{
cout << "Your status is fail" << endl;
}
}

#include<iostream>
using namespace std;
int calculate_square(int number);
void main()
{
int number1,answer;
cout << "Enter a number for which you wanna a square: " << endl;
cin >> number1;
answer=calculate_square(number1);
cout << "The square of " << number1 << " is : " <<answer;

system("pause");
}
int calculate_square(int number)
{
int square = number * number;
return square;
}
/*Write a C++ program having a user defined function
factorial that takes a number as its parameter and returns the factorial of the
number.
Call the function using value entered by the user.*/
#include<iostream>
using namespace std;
int factorial(int number);
void main()
{
int number1,factorial2;
cout << "Enter a number " << endl;
cin >> number1;
cin.ignore();
factorial2 = factorial(number1);
cout << "The factorial of " << number1 << " is: " << factorial2 << endl;
system("pause");
}
int factorial(int number)
{
int factorial1 = 1;
for (int i = number; i >= 1; i = i - 1)
{
factorial1 = factorial1 * i;
}
return factorial1;
}

///*Write a C++ program that stores marks of five students in an array.


//Use a function to display the contents of the array.*/

#include<iostream>
#include<string>
using namespace std;
void marks(int m[5]);
void main()
{
int arr1[5] = { 23,34,56,67,78 };
marks(arr1);
system("pause");
}
void marks(int m[5])
{
for (int i = 0; i <= 4; i++)
{
cout << "The marks of student " << i + 1 << " is: " << m[i] << endl;
}
}

/*Write a C++ program that stores 5


numbers entered by the user in an array. It then calls a user defined function
calculateSum that takes the array as argument and returns the sum of all
numbers in the array. Display sum in main function.*/
#include<iostream>
using namespace std;
int calculatesum(int m[5]);
void main()
{
int sum;
int arr[5];
for(int i=0;i<=4;i++){
cout << "Enter a number; ";
cin >> arr[i];
}
sum=calculatesum(arr);
cout << "The sum of all the numbers that you entered is " << sum << endl;
system("pause");
}
int calculatesum(int m[5])
{
int sum1=0;
for (int i = 0; i <= 4; i++)
{
sum1 = sum1 + m[i];
}
return sum1;
}

/*Write a C++ program having structure Person containing


name and age of a person. Create a
structure variable , insert values in it and use a function to display the
contents of the structure variable
Add another function checkVotingEligibility to it, that takes as
argument the age of a person and displays whether he/she can cast a vote or
not. ( Everyone 18 and above can cast a vote in Pakistan) */
#include<iostream>
#include<string>
using namespace std;
struct Person {
string name;
int age;
};
Person p;
void pserson(void);
void checkVotingEligibility( int age1);
void main()
{

cout << "Enter your name: ";


getline(cin, p.name);
cin.ignore(-1);
cout << "Enter your age in years: ";
cin >> p.age;
cin.ignore();

pserson();
checkVotingEligibility(p.age);
system("pause");
}
void pserson(void)
{
cout << "Your name is: " << p.name << endl;
cout << "Your age is: " << p.age << endl;
}
void checkVotingEligibility(int age1)
{
if (age1 >= 18)
{
cout << "You can cast vote" << endl;
}
else
{
cout << "You can't cast vote" << endl;
}
}

TASK 2
-------------------------------------------------------------------------------
Create a struct Student with fields: name, registrationNumber, subjects, hobbies
and semester. A student can register 6 subjects. Similarly, a student can have
top three hobbies. Declare a student. Assign values to the attributes of this
student by taking them from user(Make a function inputData for that) . Use a
user defined function displayMenu() to display a menu having following options

a. Press P to print �You are a Student of Programming


Fundamentals� if he is studying PF. ( Make a function checkPF for that)

b. Press C to print �You are a Student of Calculus� if he


is studying Calculus. ( Make a function checkCal for that)

c. Press D to print complete data of student on


screen (Make a function displayData)

d. Press E to exit.
The program repeatedly executes by showing
menu and displaying data as per user choice until the user presses E to exit.
*/
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name, registration_number,subjects[6],hobbies[3];
int semester;
};
student s; char choice;
void student1(void);
void displaymenu(void);
void main()
{
cout << "Enter your name: " << endl;
getline(cin, s.name);
cin.ignore(-1);
cout << "Enter your registration number" << endl;
getline(cin, s.registration_number);
cin.ignore(-1);
cout<<"Enter your subjects name : "<<endl;
for (int i = 0; i <= 5; i++)
{
cout << i + 1 << " : ";
getline(cin, s.subjects[i]);
cin.ignore(-1);
}
cout << "Enter your hobbies :" << endl;
for (int i = 0; i <= 2; i++)
{
cout << i + 1 << ": ";
getline(cin, s.hobbies[i]);
cin.ignore(-1);
}
cout << "Enter your current semester: ";
cin >> s.semester;
cin.ignore();

do{
displaymenu();
cin >> choice;
cin.ignore();
if (choice == 'P')
{
for(int i=0;i<=5;i++)
if (s.subjects[i] == "Programming Fundamentals")
{
cout << "You are a Student of Programming Fundamentals" <<
endl;
}
}
else if (choice == 'C')
{
for (int i = 0; i <= 5; i++)
if (s.subjects[i] == "Calculus")
{
cout << "You are a Student of Calculus." << endl;
}
}
else if(choice=='D')
{
student1();
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 'E');
system("pause");
}
void student1(void)
{
cout << "Your name is " << s.name << endl;
cout << "Your registration number is " << s.registration_number << endl;
cout << "Your current semester is : " << s.semester << endl;
cout << "Your subjects are: " << endl;
for (int i = 0; i <= 5; i++)
{
cout << i + 1 << ": " << s.subjects[i] << endl;
}
cout << "Your hobbies are: " << endl;
for (int i = 0; i <= 2; i++)
{
cout << i + 1<<": "<<s.hobbies[i]<<endl;
}
}
void displaymenu(void)
{
cout << "Menu" << endl;
cout << "Press P to check out whether you are the student of Programming
fundamentals or not " << endl;
cout << "Press C to check out whether you are the student of Calculus or
not" << endl;
cout << "Press D to check out all the data of the student" << endl;
cout << "Press E for exit " << endl;
}

/*Write a C++ program that stores 5


numbers entered by the user in an array. Your program should have 5 user defined
functions in it
( calculateSum, calculateProduct, countEven,findSmallest and calculateAverage) such
that
* function
calculateSum that takes the array as argument and returns the sum of all
numbers in the array. Displays sum in main function.

* function
calculateProduct that takes the array as argument and
returns the product of all numbers in the array. Displays product in main
function.

* function countEven
that takes the array as argument and returns the count of all even
numbers in the array. Displays count in main function.

* function
findSmallest that takes the array as argument and returns the smallest
of all numbers in the array. Displays smallest number found in main function.

* function
calculateAverage that takes the array as argument, then
calls calculateSum to find the sum of all elements, then divides the sum with 5
to calculate the value of average and returns the average of all numbers in the
array to main function. Displays average in main function.

Display a menu of options to the user

a.
Press 1 to calculate Sum of all
numbers in the array

b.
Press 2 to calculate Product of
all numbers in the array

c.
Press 3 to count all even
numbers in the array

d.
Press 4 to find smallest number
in the array

e.
Press 5 to calculate Average of
all numbers in the array

f.
Press 6 to exit

The program
continues execution repeatedly until the user chooses to exit by pressing 6

*/
#include<iostream>
using namespace std;
void displaymenu(void);
int calculatesum(int m[5]);
int calculateproduct(int m[5]);
int counteven(int m[5]);
int smallest(int m[5]);
int calculateaverage(int m[5]);
int arr[5],sum,product,smallest1,even, choice;
float average;
void main()
{
for (int i = 0; i <= 4; i++)
{
cout << "Enter a number " << i + 1 << ": ";
cin >> arr[i];
cin.ignore(-1);
}
sum = calculatesum(arr);
product = calculateproduct(arr);
smallest1 = smallest(arr);
even = counteven(arr);
average = calculateaverage(arr);
do {
displaymenu();
cin >> choice;
cin.ignore();
if (choice == 1)
{
cout << "The sum of all the number that you entered is : " << sum
<< endl;
}
else if (choice == 2) {
cout << "The product of all the number that you entered is : " <<
product << endl;
}
else if (choice == 4) {
cout << "The smallest of all the numbers that you entered is : "
<< smallest1 << endl;
}
else if (choice == 3) {
cout << "The count of all even numbers are : " << even << endl;
}
else if (choice == 5) {
cout << "The average of all the numbers you entered is : " <<
average << endl;
}
else
{
cout << "please enter the right value" << endl;
}
} while (choice != 6);
system("pause");
}
void displaymenu(void)
{
cout << "*****Menu********" << endl;
cout << "Press 1 to calculate Sum of all numbers in the array" << endl;
cout<< "Press 2 to calculate Product of all numbers in the array" << endl;
cout<<"Press 3 to count all even numbers in the array" << endl;
cout<<"Press 4 to find smallest number in the array" << endl;
cout<< "Press 5 to calculate Average of all numbers in the array" << endl;
cout<< "Press 6 to exit" << endl;
}

int calculatesum(int m[5])


{
int sum1 = 0;
for (int i = 0; i <= 4; i++)
{
sum1 = sum1 + m[i];
}
return sum1;
}
int calculateproduct(int m[5])
{
int product1 = 1;
for (int i = 0; i <= 4; i++)
{
product1 = product1 * m[i];
}
return product1;
}
int smallest(int m[5])
{
int smallest2 = INT_MAX;
for(int i=0;i<=4;i++){
if (m[i] < smallest2)
{
smallest2= m[i];
}
}
return smallest2;
}
int counteven(int m[5])
{
int count = 0;
for(int i=0;i<=4;i++)
{
if( m[i]%2==0)
{
count = count + 1;
}
}
return count;
}
int calculateaverage(int m[5])
{

int sum1 = 0,count=0;


float average;
for (int i = 0; i <= 4; i++)
{
sum1 = sum1 + m[i];
count = count + 1;
}
average = sum1 / count;
return average;
}
/*Write a C++ program having structure Person containing
name and age of a person. Create an
array of three persons , insert values in it and use a function to display the
contents of the array of structure.*/
#include<iostream>
#include<string>
using namespace std;
struct person
{
string name;
int age;
};
person p[3];
void personsdata(void);
void main()
{
for (int i = 0; i <= 2; i++)
{
cout << "Person " << i + 1 << endl;
cout << "Enter the name: " << endl;
getline(cin, p[i].name);
cin.ignore(-1);
cout << "Enter the age: " << endl;
cin >> p[i].age;
cin.ignore();
}
personsdata();
system("pause");
}
void personsdata(void)
{
for (int i = 0; i <= 2; i++)
{
cout << "Person " << i + 1 << endl;
cout << "Name: " << p[i].name << endl;
cout << "Age: " << p[i].age << endl;
}
}
/*Create a struct Student with fields: name, registrationNumber, subjects and
semester.
A student can register six subjects. A class has 5 students
. Assign values to the attributes of these students by taking them from user.
(Make a function inputData for that).
Use a user defined function displayMenu() to display a menu having following
options
a. Press T to print data of all students studying in semester 2
(make a user defined function SecondSemesterStudents for that)
b. Press P to print data of all students studying Programming Fundamentals
(make a user defined function PF_Students for that)
c. Press S to search for a student when user enters his reg No.
Display his data on screen. Else display student not found.
(make a user defined function search_Student for that)
d. Press E to exit.The program repeatedly executes by showing menu
and displaying data as per user choice until the user presses E to exit.
*/
#include<iostream>
#include<string>
using namespace std;
struct student {
string name, registration_name, subjects[6];
int semester;
};
student s[5]; char choice;
void inputdata(void);
void displaymenu(void);
void SecondSemesterStudents(void);
void PF_Students(void);
void search_Student(void);
void main()
{
inputdata();
do{
displaymenu();
cin >> choice;
cin.ignore();
if (choice == 'T')
{
SecondSemesterStudents();
}
else if (choice == 'P')
{
PF_Students();
}
else if (choice == 'S')
{
search_Student();
}
else
{
cout << "Please enter the right option" << endl;
}
} while (choice != 'E');
system ("pause");
}
void inputdata(void)
{

for (int i = 0; i <= 4; i++)


{
cout << "Enter the name: " << endl;
getline(cin, s[i].name);
cin.ignore(-1);
cout << "Enter your registration number " << endl;
getline(cin, s[i].registration_name);
cin.ignore(-1);
cout << "Enter your current semester: " << endl;
cin >> s[i].semester;
cin.ignore();
cout << "Enter your subjects: " << endl;
for (int j = 0; j <= 5; j++)
{
cout << j + 1 << ": ";
getline(cin, s[i].subjects[j]);
cin.ignore(-1);
}
}
}
void displaymenu(void)
{
cout << "Press T to print data of all students studying in semester 2 " <<
endl;
cout << "Press P to print data of all students studying Programming
Fundamentals" << endl;
cout << "Press S to search for a student when user enters his reg No" <<
endl;
cout << "Press E to exit." << endl;
}
void SecondSemesterStudents(void)
{
for (int i = 0; i <= 4; i++)
{
if (s[i].semester == 2)
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " << s[i].registration_name <<
endl;
cout << "Current Semester: " << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int j = 0; j <= 5; j++)
{
cout << j + 1 << " : " << s[i].subjects[j]<<endl;
}
}
}
}
void PF_Students(void)
{
for (int i = 0; i <= 4; i++)
{
for(int j=0;j<=5;j++)
if (s[i].subjects[j] == "Programming Fundamentals")
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " << s[i].registration_name
<< endl;
cout << "Current Semester: " << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int k = 0; k <= 5; k++)
{
cout << k + 1 << " : " << s[i].subjects[k] << endl;
}
}
}

}
void search_Student(void)
{
string registration;
cout << "Enter your registration number: " << endl;
getline(cin, registration);
cin.ignore(-1);
for (int i = 0; i <= 4; i++)
{
if (registration == s[i].registration_name)
{
cout << "Name: " << s[i].name << endl;
cout << "Registration Number: " << s[i].registration_name <<
endl;
cout << "Current Semester: " << s[i].semester << endl;
cout << "Subjects: " << endl;
for (int j = 0; j <= 5; j++)
{
cout << j + 1 << " : " << s[i].subjects[j] << endl;
}
}
}
}

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

fstream MyFile;
void main()
{
MyFile.open("createfile.txt", ios::out);
if (!MyFile)
{
cout << "File creation failed" << endl;
}
else
{
cout << "New file created" << endl;
MyFile.close();
}
system("pause");
}

#include <iostream>
#include<fstream>
#include<string>
using namespace std;
string name;
fstream MyFile;
void main()
{
MyFile.open("createfile.txt", ios::out);
if (!MyFile)
{
cout << "File creation failed" << endl;
}
else
{
cout << "New file created" << endl;
cout << "Enter your name: " << endl;
getline(cin, name);
cin.ignore(-1);
MyFile << "Hello ";
MyFile << " " << name;
MyFile.close();
}
system("pause");
}
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
fstream MyFile;
void main()
{
MyFile.open("createfile.txt",ios::in );
if (!MyFile)
{
cout << "New file is not created" << endl;
}
else
{
cout << "File is created" << endl;
char ch;
while (MyFile >> ch)
{
cout << ch << endl;
}
MyFile.close();
}
system("pause");
}

/*Write a program that inputs the three integers from user and writes them in a
file sum.txt.*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int number,sum=0;
fstream MyFile;
void main()
{
MyFile.open("afshan.txt", ios::app);
if (!MyFile)
{
cout << "File is not created" << endl;
}
else
{
for(int i=0;i<=4;i++)
{
cout << "Enter the number: " << endl;
cin >> number;
cin.ignore();
sum = sum + number;
}
cout << "The sum of all the numbers is: " << sum << endl;
MyFile<< "The sum of all the numbers is: " << sum << endl;
MyFile.close();
}

system("pause");

/*Write a program that inputs the three integers from user and writes them in a
file sum.txt.*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int number,sum=0;
fstream MyFile;
void main()
{
MyFile.open("afshan.txt", ios::app);
if (!MyFile)
{
cout << "File is not created" << endl;
}
else
{
for(int i=0;i<=4;i++)
{
cout << "Enter the number: " << endl;

cin >> number;


MyFile << i + 1 << ": " << number << endl;
cin.ignore();
sum = sum + number;
}
cout << "The sum of all the numbers is: " << sum << endl;
MyFile<< "The sum of all the numbers is: " << sum << endl;
MyFile.close();
}

system("pause");

}
/*Write
a C++ program that reads integers from the file sum.txt (it can containing any
amount of integers) and prints their sum. (�Hint: read the values in an integer
variable)*/
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int number,sum=0;

void main()
{
fstream MyFile;
MyFile.open("aiman.txt", ios::in);
if (!MyFile)
{
cout << "File is not created" << endl;
}
else
{
while (MyFile >> number)
{
cout << number << endl;
sum = sum + number;
}
cout << "The sum of all the numbers is: " << sum << endl;
MyFile.close();
}

system("pause");

You might also like