Important Practice of C++
Important Practice of C++
#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;
/*.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++) {
#include<iostream>
using namespace std;
void main()
{
for(int l=1;l<=4;l++){
for (int i = 1; i <= 2; i++) {
#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()
{
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");
}
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{
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
/*Menu
1. Check if even or odd
2. Check if positive or negative
3. Exit program
/*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
{
}
else if (choice1 == 3)
{
cout << "Thank you for using the processing on
number " << endl;
}
else
{
cout << "Please enter the right option" <<
endl;
}
}
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");
#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 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 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];
}
}
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");
}
}
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 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;
}
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");
}
}
cout << endl;
}
if (identical ==10 )
{
cout << "Identical" << endl;
}
else if (identical < 10)
{
cout << "Not identical" << 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");
/*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 = '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");
}
};
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");
}
My number is
(212) 767-8900
}
}
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");
}
#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;
}
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.
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;
}
}
#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;
}
#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;
}
}
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
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;
}
* 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.
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;
}
}
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;
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");