0% found this document useful (0 votes)
18 views66 pages

C++ Codes - 250213 - 101950

This document is a laboratory record book for Procedural Programming with C++ submitted by Torgbenu-Banini Jessica Emefa from the University of Mines and Technology. It includes various exercises with detailed aims, algorithms, code, output, and results for multiple C++ programs focusing on fundamental programming concepts. The record book serves as a comprehensive guide to understanding and implementing basic programming techniques using C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views66 pages

C++ Codes - 250213 - 101950

This document is a laboratory record book for Procedural Programming with C++ submitted by Torgbenu-Banini Jessica Emefa from the University of Mines and Technology. It includes various exercises with detailed aims, algorithms, code, output, and results for multiple C++ programs focusing on fundamental programming concepts. The record book serves as a comprehensive guide to understanding and implementing basic programming techniques using C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

University of Mines and Technology

(Tarkwa)

PROCEDURAL PROGRAMMING WITH C++


LABORATORY RECORD BOOK

SUBMITTED BY:
NAME: TORGBENU – BANINI JESSICA EMEFA
INDEX NUMBER: BS424108020
DEPARTMENT: COMPUTER SCIENCE AND ENGINEERING
DATE OF SUBMISSION: 26TH MARCH 2021

FACULTY OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
P. O. BOX 237 TARKWA, WESTERN REGION GHANA
TABLE OF CONTENTS
EXERCISE 1 .......................................................................................................................................... 5
PROGRAM 1 .................................................................................................................................... 5
PROGRAM 2 .................................................................................................................................... 6
PROGRAM 3 .................................................................................................................................... 7
PROGRAM 4 ................................................................................................................................... 8
PROGRAM 5 .................................................................................................................................... 9
PROGRAM 6 (i) .............................................................................................................................. 11
PROGRAM 6 ii................................................................................................................................ 12
PROGRAM 6(iii) ............................................................................................................................. 13
PROGRAM 6(iv) ............................................................................................................................. 14
EXERCISE 2 ........................................................................................................................................ 15
PROGRAM 1 .................................................................................................................................. 15
PROGRAM 2 .................................................................................................................................. 16
PROGRAM 3 .................................................................................................................................. 17
PROGRAM 4 .................................................................................................................................. 19
PROGRAM 5 .................................................................................................................................. 20
PROGRAM 6 .................................................................................................................................. 21
PROGRAM 7 .................................................................................................................................. 22
PROGRAM 8 ................................................................................................................................. 24
EXERCISE 3 ........................................................................................................................................ 26
PROGRAM 1: ................................................................................................................................. 26
PROGRAM 2: ................................................................................................................................. 27
PROGRAM 3: ................................................................................................................................. 29
PROGRAM 4 : ................................................................................................................................ 31
PROGRAM 5: ................................................................................................................................. 32
PROGRAM 6: ................................................................................................................................. 33
PROGRAM 7: ................................................................................................................................. 34
PROGRAM 8 : ................................................................................................................................ 35
PROGRAM 9 .................................................................................................................................. 36
PROGRAM 10: .......................................................................................................................... 38
EXERCISE 4 ........................................................................................................................................ 39
PROGRAM 1: ................................................................................................................................. 39
PROGRAM 2: ................................................................................................................................. 40
PROGRAM 3: ................................................................................................................................. 41
PROGRAM 4: ................................................................................................................................. 42

2|C++ RECORD BOOK


PROGRAM 5: ................................................................................................................................. 43
PROGRAM 6: ................................................................................................................................. 44
PROGRAM 7 .................................................................................................................................. 46
PROGRAM 8: ................................................................................................................................. 47
Program 9(i-v) ............................................................................................................................... 48
EXERCISE 5 ........................................................................................................................................ 52
PROGRAM 1: ................................................................................................................................. 52
PROGRAM 2: ................................................................................................................................. 54
PROGRAM 3: ................................................................................................................................. 55
PROGRAM 4: ................................................................................................................................. 56
PROGRAM 5 .................................................................................................................................. 57
PROGRAM 6 .................................................................................................................................. 59
PROGRAM 7 .................................................................................................................................. 60
PROGRAM 8 .................................................................................................................................. 62
PROGRAM 9 .................................................................................................................................. 64
PROGRAM 10 ................................................................................................................................ 65

3|C++ RECORD BOOK


University of Mines and Technology

PROCEDURAL PROGRAMMING WITH C++

LAB EXERCISE 1

REPORT SUBMITTED BY

NAME: TORGBENU – BANINI JESSICA EMEFA


INDEX NUMBER: BS424108020

LAB DATE: 05TH FEBUARY 2021


SUBMISSION DATE: 26TH MARCH 2021

4|C++ RECORD BOOK


EXERCISE 1
PROGRAM 1
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write a C++ program to display “This is my first C ++ Program”
ALGORITHM:
1. Start program
2. Print “This is my first C++ Program”
3. End program
CODE:
#include <iostream>
#include <windows.h>
using namespace std;
int main ( )
{ //starts program
system (“Title Problem 1”); // gives title to the
window
cout << "THIS IS MY FIRST C++ PROGRAM"; // displays on
the screen
return 0; //ends program
}

OUTPUT:
THIS IS MY FIRST C++ PROGRAM

RESULT:
The program is successfully executed.

5|C++ RECORD BOOK


PROGRAM 2
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write a C++ program to add two numbers (2 and 6) and display its
sum.
ALGORITHM:
1. Start program
2. Declaration of variables
3. Assign values to variables
4. Find the sum
5. Display the sum
6. End program
CODE:

#include <iostream>

using namespace std;

int main( )

{ //start program

int q, v, x; //declaration of variables

q=2; // assigns values to q and v

v=6;

x=q+v; // finds the sum

cout << q << " + " << v << " = " << x; // displays the sum

return 0; //ends program

OUTPUT:
2+6=8

RESULT:
This program is executed successfully.

6|C++ RECORD BOOK


PROGRAM 3
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write a C++ program to multiply two numbers (4 and 5) and display
its product.
ALGORITHM:
1. Start program
2. Declaration of variables
3. Assign values to variables
4. Find the product
5. Display the product
6. End program
CODE:
#include <iostream>

using namespace std;

int main( )

{ //start program

int q, v, x; //declaration of variables

q=4; // assigns values to q and v

v=5;

x=q*v; // finds the product

cout << q << " x " << v << " = " << x; // displays the product

return 0; //ends program

OUTPUT:
4 x 5 = 20

RESULT:
This program is executed successfully.

7|C++ RECORD BOOK


PROGRAM 4
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write a C++ program to calculate area and circumference of a circle.
ALGORITHM:
1. Start program
2. Declaration of PI and other variables
3. User inputs the value for the radius to find the Area and
Circumference
4. Calculates for the Area and Circumference
5. Display the Area and the Circumference
6. End program
CODE:
#include <iostream>
#define PI 3.14159 //defines the value of π (pi)
using namespace std;
int main( ) //start program
{ float radius, area, circum; // declaration of variables
cout << "\n\n To Find the area and circumference of any circle :\n";
cout << "----------------------------------------------------\n\n\n";

cout<<" Input the radius (1/2 of diameter) of a Circle: \n ";


//user told to inputs the radius
cin>>radius; //user inputs the radius
circum = 2*PI*radius; //finds the circumference
area = PI*(radius*radius); //finds the area
cout<<" The area of the circle is : "<< area << endl; //displays
the area
cout<<" The circumference of the circle is : "<< circum << endl;
//displays the Circumference
cout << endl; // begins next line
return 0; // ends program
}

OUTPUT:
Find the area and circumference of any circle:
----------------------------------------------------
Input the radius (1/2 of diameter) of a circle:
8
The area of the circle is: 201.062
The circumference of the circle is: 50.2654

RESULT:
This program is executed successfully.
8|C++ RECORD BOOK
PROGRAM 5
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write a C++ program to perform addition, subtraction, division and
multiplication of two numbers.
ALGORITHM:
1. Start program
2. Declaration variables
3. User inputs two numbers
4. Calculate the Sum, Difference, Product and the division of that two
numbers
5. Display the Sum, Difference, Product and the division of those two
numbers
6. End program
CODE:
#include<iostream> //start program
using namespace std;
int main( )
{
float x, y, z; //declaration of variables
cout<<"Enter Two Numbers: \n"; //user told to input two
numbers
cin>>x>>y;
z = x+y; //finds the sum
cout<<endl<<"Addition Result = "<<z<<endl; //displays the
sum
z = x-y; //find the difference
cout<<endl<<"Subtraction Result = "<<z<<endl; // displays
the difference
z = x*y; // finds the product
cout<<endl<<"Multiplication Result = "<<z<<endl; //
displays the product
z = x/y; //finds the division
cout<<endl<<"Division Result = "<<z<<endl; //displays the
division
return 0; //ends program
}

OUTPUT:
Enter Two Numbers:
12
5
Addition Result = 17
Subtraction Result = 7
Multiplication Result = 60
Division Result = 2.4

9|C++ RECORD BOOK


RESULT:
This program is executed successfully.

10 | C + + R E C O R D B O O K
PROGRAM 6 (i)
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write C++ program to evaluate each of the following equations.
(i) V = u + at.
ALGORITHM:
1. Start program
2. Declaration variables
3. User inputs the value for initial velocity, acceleration and time
4. Calculation for Final Velocity
5. Display the Value of Final Velocity
6. End program

CODE:
#include <iostream>
using namespace std;
int main() {//start program
float v,u,a,t ; //declaration of variables
cout<<"Enter initial velocity in (m/s)\n" ; //user told to input
initial velocity(u)
cin>> u ; //user inputs u
cout<<"Enter acceleration in (m/s^2)\n" ; //user told to input
acceleration(a)
cin>> a ; //user inputs acceleration
cout<<"Enter time in (seconds) \n"; //user told to input time (t)
cin>> t ; //user inputs time
v = u + a*t ; //computation for velocity
cout<<"The Value Of Velocity is: "<<v<<" m/s "; //displays the value
of velocity
return 0; //end program
}

OUTPUT:
Enter initial velocity in (m/s)
4
Enter acceleration in (m/s^2)
9.81
Enter time in (seconds)
16
The Value Of Velocity is: 160.96 m/s

RESULT: This Program Is successfully executed.

11 | C + + R E C O R D B O O K
PROGRAM 6 ii
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write C++ program to evaluate the distance s, where S = ut + 1/2at2
ALGORITHM:
1. Start program
2. Declaration variables
3. User inputs the value for initial velocity, acceleration and time
4. Calculation for finding Distance (S)
5. Display the Value of the Distance
6. End program
CODE:
#include <iostream>
using namespace std;
int main() { //start program
float s,u,a,t ; //declaration of variables

cout<<"Enter initial velocity in (m/s)\n" ; //user told to input


initial velocity(u)

cin>> u ; //user inputs u

cout<<"Enter acceleration in (m/s^2)\n" ; //user told to input


acceleration(a)

cin>> a ; //user inputs acceleration

cout<<"Enter time in (seconds) \n"; //user told to input time (t)

cin>> t ; //user inputs time

s = ut + (a*t*t)/2 ; //computation for the Distance

cout<<"The Value Of The Distance is: "<<v<<" m "; //displays


the value of the distance
return 0; //end program
}

OUTPUT:
Enter initial velocity in (m/s)
5
Enter acceleration in (m/s^2)
6
Enter time in (seconds)
7
The Value Of The Distance is: 182 m

RESULT: This program is executed successfully


12 | C + + R E C O R D B O O K
PROGRAM 6(iii)
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write C++ program to evaluate T, where T=2*a+√b+9c
ALGORITHM:
1. Start program
2. Declaration variables
3. User inputs the value for a, b and c
4. Calculation for finding Time (T)
5. Display the Value of the Time (T)
6. End program
CODE:

#include <iostream> //start program


#include<math.h>
using namespace std;
int main( ) {
float c,b,a,T; //declaration of variables
cout<<"Enter the value of a \n" ; //user told to input a
cin>> a ; //user inputs a
cout<<"Enter b \n" ; //user told to input b
cin>> b; //user inputs b
cout<<"Enter c \n"; //user told to input c
cin>> c; //user inputs c
T = 2*a + sqrt(b+9*c); //computation for Time
cout<<"The Value Of Time (T) is: "<<T<<" seconds "; //displays
the value of Time
return 0;//end program
}

OUTPUT:
Enter initial velocity in (m/s)
5
Enter acceleration in (m/s^2)
6
Enter time in (seconds)
7
The Value of Distance is: 182 m
RESULT:
This Program is executed successfully.

13 | C + + R E C O R D B O O K
PROGRAM 6(iv)
AIM: 1. To be familiar with syntax and structure of C++ programming.
2. To learn problem solving techniques using C++ language
TITLE: Write C++ program to evaluate H, where H = √𝑏 2 + 𝑝2
ALGORITHM:
1. Start program
2. Declaration variables
3. User inputs the value for p and b
4. Calculation for finding Height (H)
5. Display the Value of the Height (H)
6. End program

CODE:
#include <iostream>

#include<math.h>

using namespace std;

int main() {//start program

float b,p,H; //declaration of variables

cout<<"Enter the value of b \n" ; //user told to input b

cin>> b ; //user inputs b

cout<<"Enter the Value of p \n" ; //user told to input b

cin>> p; //user inputs p

H = sqrt(b*b + p*p); //computation for H

cout<<"The Value Of Height (H) is: "<<H<<" m "; //displays the value
of H
return 0;//end program
}

OUTPUT:
Enter the value of b
3
Enter p
4
The Value Of Height (H) is: 5 m
RESULT:
This program is successfully executed.

14 | C + + R E C O R D B O O K
EXERCISE 2
PROGRAM 1
AIM: 1. To be familiar with different data types, Operators and Expressions

in C++.
TITLE: To write a program to calculate simple and compound interest.

ALGORITHM:
1. Start program
2. Declaration variables Principal(p),Rate r, Time (t),(CI )Compound
Interest, (SI) Simple Interest
3. User inputs the value for p, r, and t
4. Calculations for finding Compound Interest and Simple Interest
5. Display the Values of the Compound Interest and Simple Interest
6. End program

CODE
:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{//start program
float p,r,t,CI,SI; //declare variables
cout<<"Enter Principle, Rate and Time : \n";
cin>>p>>r>>t; //enter p, r and t
SI=(p*r*t)/100; //finds the Simple Interest
CI=p*pow((1+r/100),t); //finds the Compound Interest
cout<<"\nSimple Interest : "<<SI<<endl; //displays the Simple
interest
cout<<"\nCompound Interest : "<<CI; // diplays the Compound
Interest
return 0; //end Program
}

OUTPUT:
Enter Principle, Rate and Time :
2
4
46
Simple Interest : 3.68
Compound Interest : 12.1496

RESULT: This program is successfully executed


15 | C + + R E C O R D B O O K
PROGRAM 2
AIM: To be familiar with different data types, Operators and Expressions
in
C++.
TITLE: To write a program to swap values of two variables with and without
using third variable
ALGORITHM:
1. Start program
2. Declaration of variables a, b and temp.
3. Assign values to variable
4. Perform the swapping before and after
5. Display the Values After the swapping
6. End program

CODE:

#include <iostream> X

using namespace std;

int main()

{//start program

int a = 5, b = 10, temp; //declaration of variables

cout << "Before swapping." << endl;

cout << "a = " << a << ", b = " << b << endl;

temp = a; //swapping

a = b;

b = temp;

cout << "\nAfter swapping." << endl; //after swpping the


variabes

cout << "a = " << a << ", b = " << b << endl; //displays the
swap

return 0; //end program

OUTPUT:
Before swapping.
A = 5, b = 10
After swapping.
A = 10, b = 5

RESULT: This program is successfully executed.


16 | C + + R E C O R D B O O K
PROGRAM 3
AIM: To be familiar with different data types, Operators and Expressions
in C++.
TITLE: To write a program to display the size of every data type using
“sizeof” operator.
ALGORITHM:
1. Start program
2. Display the sizes of data types in byte(Char, int, float, double etc.
3. End program
CODE:
P.T.O

OUTPUT:
Size of char: 1 byte
Size of unsigned char: 1 bytes
Size of signed char: 1 bytes
Size of int: 4 bytes
Size of signed int: 4 bytes
Size of unsigned int: 4 bytes
Size of short int: 2 bytes
Size of unsigned short int: 2 bytes
Size of signed short int: 2 bytes
Size of long int: 4 bytes
Size of signed long int: 4 bytes
Size of unsigned long int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes
Size of void: 1 bytes
Size of bool: 1 bytes
Size of wchar_t: 2 bytes

RESULT:
This Program is successfully executed.

17 | C + + R E C O R D B O O K
#include <iostream>
using namespace std;
int main()
{// start program

cout << "Size of char: " << sizeof(char) << " byte" << endl; //displays size
of Char
cout << "Size of unsigned char: " << sizeof(unsigned char) << " bytes" <<
endl;//displays of unsigned char
cout << "Size of signed char: " << sizeof(signed char) << " bytes" << endl;
//displays of signed char
cout << "Size of int: " << sizeof(int) << " bytes" << endl; //displays size
of int
cout << "Size of signed int: " << sizeof(signed int) << " bytes" << endl;
//displays of signed int
cout << "Size of unsigned int: " << sizeof(unsigned int) << " bytes" << endl;
//displays of unsigned int
cout << "Size of short int: " << sizeof(short int) << " bytes" << endl;
//displays of short int
cout << "Size of unsigned short int: " << sizeof(unsigned short int) << "
bytes" << endl; //displays of unsigned short int
cout << "Size of signed short int: " << sizeof(signed short int) << " bytes"
<< endl; //displays of signed short int
cout << "Size of long int: " << sizeof(long int) << " bytes" << endl;
//displays of long int
cout << "Size of signed long int: " << sizeof(signed long int) << " bytes" <<
endl; //displays of signed long int
cout << "Size of unsigned long int: " << sizeof(unsigned long int) << "
bytes" << endl; //displays of unsigned long int
cout << "Size of float: " << sizeof(float) << " bytes" << endl; //displays
size of float
cout << "Size of double: " << sizeof(double) << " bytes" << endl; //displays
of Double
cout << "Size of long double: " << sizeof(long double) << " bytes" << endl;
//displays of long double
cout << "Size of void: " << sizeof(void) << " bytes" << endl; //displays of
void
cout << "Size of bool: " << sizeof(bool) << " bytes" << endl; //displays of
bool
cout << "Size of wchar_t: " << sizeof(wchar_t) << " bytes" << endl;
//displays of wchar_t
return 0; //end program
}

18 | C + + R E C O R D B O O K
PROGRAM 4
AIM: To be familiar with different data types, Operators and Expressions
in C++.
TITLE: To write a program to illustrate the use of unary prefix and postfix
increment and decrement operators.
ALGORITHM:
1. Start program
2. Declaration of variables a, c
3. Assign value to variable to a
4. Adds increment sign to a
5. Display the Values of a after assignment of increment and decrement
signs
6. End program
CODE:

#include <iostream>
using namespace std;
int main()
{ // start program
int a = 21;
int c ;
// Value of a will not be increased before assignment.
c = a++;
cout << "Line 1 - Value of a++ is :" << c << endl ;

// After expression value of a is increased


cout << "Line 2 - Value of a is :" << a << endl ;

// Value of a will be increased before assignment.


c = --a;
cout << "Line 3 - Value of ++a is :" << c << endl ;
return 0; //end program
}

OUTPUT:
Line 1 - Value of a++ is :21
Line 2 - Value of a is :22
Line 3 - Value of ++a is :23

RESULT: This Program is successfully executed

19 | C + + R E C O R D B O O K
PROGRAM 5
AIM: To be familiar with different data types, Operators and Expressions
in C++.
TITLE: To write a program to input two numbers and display the maximum
number.
ALGORITHM:
1. Start program
2. Declaration of variables num1 and num2.
3. User will have to make input of two values
4. If the first number is bigger to the second, first number will appear
5. If the second number is bigger to the first, second number will appear
6. Displays the Bigger number of the Two
7. End program
CODE:

#include <iostream>
using namespace std;
int main()
{// start program
int num1, num2;
cout<<"Enter first number:"; //tells user to input value
cin>>num1; //user makes an input
cout<<"Enter second number:"; //tells user to input value
cin>>num2; //user makes an input
if(num1>num2) //if-else condition
{
cout<<"First number "<<num1<<" is the largest"; //displays the big
number
}
else // If-else condition
{
cout<<"Second number "<<num2<<" is the largest"; //displays the
big number
}
return 0;//end program
}

OUTPUT:
Enter first number:4
Enter second number:19
Second number 19 is the largest

RESULT:
This Program is successfully executed.
20 | C + + R E C O R D B O O K
PROGRAM 6
AIM: To be familiar with different data types, Operators and Expressions
in C++.
TITLE: To write a program to find the largest of three numbers using
ternary operators.
ALGORITHM:
1. Start program
2. Declaration of variables
3. User makes input
4. Finds the largest number
5. Displays the largest number
6. Ends program
CODE:

#include <iostream>

using namespace std;

int main()

{// start program

int a,b,c;

cout<<"Enter three integers: "; //user told to inputs three value

cin>>a>>b>>c; // user makes input

int largest=(a>b)?(a>c?a:c):(b>c?b:c);

cout<<largest<<" is largest."<<endl; //displays the largest number

return 0; //end program

OUTPUT:
Enter three integers:
2
3
6
6 is largest.

RESULT:
This program is executed successfully.

21 | C + + R E C O R D B O O K
PROGRAM 7
AIM: To be familiar with different data types, Operators and Expressions
in C++.
−𝑏±√𝑏2 −4𝑎𝑐
TITLE: To write a program to find the roots of quadratic equation. 2𝑎
ALGORITHM:
1. Start program
2. Declaration of variables a, b and c
3. User makes input of the variables from the equation
4. Computation for the roots of the variables of the equation
5. Displays the values of the given equation either real or imaginary.
6. Ends program

CODE:
P .T .O

OUTPUTS:
enter the variables
1
-3
9
Roots are complex and different.
Root 1 = 1.5 + 2.59808i
Root 2 = 1.5 - 2.59808i

RESULT:
This program is successfully executed.

22 | C + + R E C O R D B O O K
#include <iostream>
using namespace std;
int main()
{// start program
int a, b, c; //declaration of variables
float discriminant, realPart, imaginaryPart, x1, x2; //declaration of
variables
cout<<"enter the variables\n"; // user told to make inputs
cin>>a>>b>>c; //user makes inputs of coefficients of the equations
if (a == 0) {
cout << "This is not a quadratic equation";
}
else {
discriminant = b*b - 4*a*c; // computes for the discriminants
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a); //computes for x1
x2 = (-b - sqrt(discriminant)) / (2*a); //computes for x2
cout << "Roots are real and different." << endl; //displays the types
of roots of the equation
cout << "Root 1 = " << x1 << endl;
cout << "Root 2 = " << x2 << endl;
} else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "Root 1 = Root 2 =" << x1 << endl;
}
else {
realPart = (float) -b/(2*a); //computes for the real parts of the
quadratic equation
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl; //tells the type
of root displayed
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i"
<<endl; //displays roots
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i"
<<endl; //displays roots
}
}
return 0; //end program
}

23 | C + + R E C O R D B O O K
PROGRAM 8

AIM: To be familiar with different data types, Operators and Expressions


in C++.
TITLE:
To write a program to input name, marks of 5 subjects of a student and
display the name of the student, the total marks scored, percentage scored
and the class of result.
ALGORITHM:
1. Start program
2. Declaration of variables (marks, index_no, sum, name)
3. User makes input of the variables and the Average is computed and
printed
4. Student’s details printed
5. Ends program
CODE:
P.T.O
OUTPUT:
input Index number of student (10)
123456789
input the name of student
ABENA
Enter Marks of Student
------------------------------------
Basic Mech : 98
Com Skills : 89
Linear Algebra : 87
Applied Electricity: 90

C++ : 88
------------------------------------
Student's Profile:
Name: ABENA
Index Number: 123456789
Total Marks of Student = 452
Average = 90.4
Grade = A

RESULT:
This Program is successfully executed.

24 | C + + R E C O R D B O O K
#include<iostream>
using namespace std;
int main()
{//start program
int marks[5], i,index_no ; //declaration of variation
float sum=0,avg;
char name[20];
cout<<"input Index number of student(10)\n";
cin>>index_no;//input index number
cout<<"input the name of student\n";
cin>>name;
cout<<"\n Enter Marks of Student \n";
cout<<"------------------------------------";
cout<<"\n Basic Mech : ";//user inputs values
cin>>marks[0];
cout<<"\n Com Skills : ";
cin>>marks[1];
cout<<"\n Linear Algebra : ";
cin>>marks[2];
cout<<"\n Applied Electricity : ";
cin>>marks[3];
cout<<"\n C++ : ";
cin>>marks[4];
for(i=0;i<5;i++) //If else condition
{
sum=sum+marks[i];
}
cout<<"------------------------------------\n";
cout<<"Student's Profile: \n";
cout<<"Name: "<<name<<"\nIndex Number: "<<index_no<<endl;
cout<<"\n Total Marks of Student = "<<sum;
avg=sum/5; //computes the average
cout<<"\n Average = "<<avg; //prints average
cout<<"\n Grade = ";//print grades
if(avg>80)
{
cout<<"A";
}
else if(avg>60 && avg<=80)
{
cout<<"B";
}
else if(avg>40 && avg<=60)
{
cout<<"C";
}
else
{
cout<<"D";
}
return 0;//end program
EXERCISE 3
}

25 | C + + R E C O R D B O O K
EXERCISE 3
PROGRAM 1:
AIM: To write a program to check whether a number is prime or not
ALGORITHM:
1. Start
2. Declare variables
3. Assign zero values to variables m and flag
4. Allow input into variables
5. Use if statement to determine whether input value is prime or not
6. Display whether input value is prime or not
7. Stop
CODE:

// program to check whether a number is a prime or not.


#include <iostream> //start program
using namespace std;
int main()
{
int n, i, m=0, flag=0; //declaration of variables
cout << "Enter the Number to check Prime:\n ";
cin >> n; //user inputs a number
m=n/2; //mathematical formula
for(i = 2; i <= m; i++) //for statement to test values input
{
if(n % i == 0) //if statement to check if number is not a prime
{
cout<<"Number is not Prime."<<endl;//prints number not
prime per condition
flag=1;
break; //terminates loop
}
}
if (flag==0) //introduce if condition to check for prime
number
cout << "Number is Prime."<<endl; //prints the prime number
return 0; //end program
}

OUTPUT:
Enter the Number to check Prime:
5
Number is Prime.
RESULT: Executed without errors

26 | C + + R E C O R D B O O K
PROGRAM 2:
AIM: To write a program to find the largest and smallest among three entered
numbers and also display whether the identified number is even or odd
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Calculate for largest and smallest using mathematical formula
5. Stop
CODE:

#include <iostream>
using namespace std;
//a program to tell the largest among three input numbers
int main() {//start program
float x, y, z ; //declaration of variables
int l ,s ,largest,smallest; //declaration of variables
cout << "Enter three numbers: \n";
cout << "First, Second and Third respectively\n";
cin >> x >> y >> z; //user makes an input
//conditions for x,y,z being a largest number
if(x >= y && x >= z)
cout << "Largest number: \n" << x; //displays x as large number
cout<<endl;
if(y >= x && y >= z)
cout << "Largest number: \n" << y; //displays y as large number
cout<<endl; //next line
if(z >= x && z >= y)
cout << "Largest number: \n" << z; //display as large number
cout<<endl;
//conditions for x,y,z being a smallest number
if(x <z && x < y)
cout << "Smallest number: \n" << x; //display x as small number
cout<<endl;
if(y <x && y < z)
cout << "Smallest number: \n" << y; //display y as small number

27 | C + + R E C O R D B O O K
cout<<endl;
if(z <x && z < y)
cout << "Smallest number: \n" << z; //display z as small number
cout<<endl;
/*checking whether even or odd*/
l= largest %2;
s=smallest %2;
largest =(x>y)?(x>z?x:z):(y>z?y:z); //checks for largest input
smallest =(x<z)?(x<z?x:z):(x<z?x:z); //checks for smallest input
/*checking if largest is even*/
if(l==0)
{
cout<< largest << "is even" << endl;
}
else{cout << largest << "is odd" << endl;}
/*checking if smallest is even*/
if(s==0)
{
cout << smallest<<" is even" << endl;
}
else{cout << smallest << " is odd" <<endl;}
return 0; //end program
}

OUTPUT:
Enter three numbers:
First, Second and Third respectively
6
7
9

Largest number:
9
Smallest number:
6

9 is even
6 is even

RESULT:
Executed without errors

28 | C + + R E C O R D B O O K
PROGRAM 3:
AIM: Write a program to display the grades of students using if else adder
ALGORITHM:
1. Start
2. Declare variables for mark
3. Allow input for mark
4. Use if and else if adder to determine range and display RESULT
5. Stop

CODE: P .T.O

OUTPUT:

Enter Marks in 5 subjects ::

Enter Marks[ 1 ] :: 90
Enter Marks[ 2 ] :: 90
Your Grade is :: [ A+ ]

RESULT:

Executed without errors

29 | C + + R E C O R D B O O K
/* C++ Program to Find Grade of a Student using if else */
#include<iostream>
using namespace std;
int main()
{
int mark[2], i;
float sum=0,avg;
cout<<"\nEnter Marks in 5 subjects :: \n";
for(i=0; i<2; i++)
{
cout<<"\nEnter Marks[ "<<i+1<<" ] :: ";
cin>>mark[i];
sum=sum+mark[i];
}

avg=sum/2;
cout<<"\nYour Grade is :: ";
if(avg>=90 &&avg<=100)
{
cout<<"[ A+ ]\n";
}
else if(avg>=80 && avg<90)
{
cout<<"[ A ]\n";
}
else if(avg>=70 && avg<80)
{
cout<<"[ B+ ]\n";
}
else if(avg>=60 && avg<80)
{
cout<<"[ B ]\n";
}
else if(avg>=50 && avg<60)
{
cout<<"[ C ]\n";
}
else if(avg<50)
{
cout<<"[ F ]\n";
}
return 0;//end program
}
}

30 | C + + R E C O R D B O O K
PROGRAM 4 :
Write a program to check whether the entered year is a year leap year or
not

ALGORITHM:
1. Start
2. Declare variables
3. Allow input for year
4. Test to see if year is a leap or not
5. Display RESULT using if else condition
6. Stop
CODE:

#include <iostream>
using namespace std;
// a c++ program to check whether the entered year is leap or not.
int main() {
int year;

cout << "Enter a year: ";


cin >> year;

if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";

return 0;
}

OUTPUT:
Enter a year: 2021
2021 is not a leap year.
RESULT:
Executed without errors

31 | C + + R E C O R D B O O K
PROGRAM 5:
AIM: Write a program to write the factorial of a number
ALGORITM:
1. Start
2. Declare variables
3. Allow input into variables
4. Perform function to find factorial
5. Display factorial
6. Stop

CODE:

#include <iostream>
using namespace std;
int main()
{
int n;
unsigned long long factorial = 1;

cout << "Enter a positive integer: \n";


cin >> n;

if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}

return 0; //end progrm

OUTPUT:
Enter a positive integer: RESULT: Executed without error
7
Factorial of 7 = 5040

32 | C + + R E C O R D B O O K
PROGRAM 6:
AIM: Write a program if a number is Armstrong or not
ALGORITHM:
1. Start
2. Declare variables
3. Allow input for num
4. Perform function to test if input is Armstrong or not
5. Display whether input is Armstrong

CODE:

#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cout<<"Enter the Number= \n ";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
cout<<"Armstrong Number."<<endl;
else
cout<<"Not Armstrong Number."<<endl;
return 0;//end program
}

OUTPUT:
Enter the Number=
4
Not Armstrong Number.

RESULT: Executed without errors

33 | C + + R E C O R D B O O K
PROGRAM 7:
AIM: Write a program to print day name using switch case
ALGORITHM:
1. Start
2. Declare variables
3. Allow input for variables
4. Create cases for the various days using switch command
5. Display result if valid or invalid
6. Stop
CODE:
#include <iostream>
using namespace std;
int main()
{
int weeknumber;

//Reading week no from user


cout<<"Enter week number(1-7): \n";
cin>>weeknumber;

switch(weeknumber)
{
case 1: cout<<"Monday";
break;
case 2: cout<<"Tuesday";
break;
case 3: cout<<"Wednesday";
break;
case 4: cout<<"Thursday";
break;
case 5: cout<<"Friday";
break;
case 6: cout<<"Saturday";
break;
case 7: cout<<"Sunday";
break;
default: cout<<"Invalid input! Please enter week no. between 1-7.";
}

return 0;//end program


}

OUTPUT:
Enter week number(1-7):
5
Friday

RESULT: Executed without error

34 | C + + R E C O R D B O O K
PROGRAM 8 :
AIM: Write a program to determine whether an input is capital or small letter
digits or special symbol

ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variable
4. Test whether input is an capital or small letter, digits or special
symbol
5. Display result
6. Stop
CODE:

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character:\n ";
cin>>ch;

if(ch>=65&&ch<=90)
cout<<endl<<"You entered an uppercase character";
else if(ch>=48&&ch<=57)
cout<<endl<<"You entered a digit";
else if(ch>=97&&ch<=122)
cout<<endl<<"You entered a lowercase character";
else
cout<<endl<<"You entered a special character";
return 0; //end program
}

OUTPUT: Enter any character:


&
You entered a special character

RESULT: Executed without error

35 | C + + R E C O R D B O O K
PROGRAM 9
AIM: Write a program to check whether a date is valid or not
ALGORITHM:
1. Start
2. Declare variables
3. Allows users input for variables
4. Test to see if date is invalid or not
5. Display result
6. Stop
CODE:

#include<iostream>
using namespace std;
int main () {
int day,month,year;
cout<<"Enter the date (Day/Month/Year) : \n";
cin>>day>>month>>year;
if(1000 <= year <= 3000)
{
if((month==1 || month==3 || month==5|| month==7|| month==8
||mmonth==10||month==12) && day>0 && day<=31)
cout<<"It is valid";
else
if(month==4 || month==6 || month==9|| month==11 && day>0 && day<=30)
cout<<"It is Valid";
else
if(month==2)
{
if((year%400==0 || (year%100!=0 && year%4==0)) && day>0 && day<=29)
cout<<"It is Valide";
else if(day>0 && day<=28)
cout<<"It is Valid";
else
cout<<"It is Invalid";
}
else
cout<<"It is Invalid";
}
else
cout<<"It is Invalid";

return 0; //end program


}

36 | C + + R E C O R D B O O K
OUTPUT:
Enter the date (Day/Month/Year) :
3
5
2021
It is valid
RESULT:
Executed without errors

37 | C + + R E C O R D B O O K
PROGRAM 10:
AIM: Write a program to check whether a number is positive ,negative or zero
ALGORITHM:
1. Start
2. Declare variables
3. Test variable to see if number is positive, negative or zero
4. Display result
5. Stop

CODE:

#include<iostream>
using namespace std;
int main()
{
// declare variable
double number;
// take input
cout << "Enter a Number : \n";
cin >> number;
// check
switch(number > 0)
{
// positive number
case 1 :
cout << "Positive" << endl;
break;
// number is zero or negative
case 0 :
switch(number < 0)
{
// number is negative
case 1 :
cout << "Negative" << endl;
break;
// number is zero
case 0 :
cout << "Zero" << endl;
break;
}
break;
}
return 0; //end program
}

OUTPUT:
Enter a Number :
6
Positive
RESULT:
Executed without error
38 | C + + R E C O R D B O O K
EXERCISE 4
PROGRAM 1:
Write a program to count number of digits in a given integer.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use while loop to count the number of digits
5. Display number of digits
6. Stop
CODE:

#include <iostream>
using namespace std;
int main()
{
cout << "\n\nWelcome to This Program \n\n\n";
cout << " == Program to count the number of digits in a given number==
n"
//variable declaration
int n, n1, num = 0;
//taking input from the command line (user)
cout << " Enter a positive integer : ";
cin >> n;
n1 = n; //storing the original number
//Logic to count the number of digits in a given number
while (n != 0)
{
/= 10; //to get the number except the last digit.
num++; //when divided by 10, updated the count of the digits
}
cout<<"\n\nThe number of digits in the entered number: " << n1 << " is " <<
num;
cout << "\n\n\n";
return 0;//end program
}

OUTPUT: Welcome to This Program


===== Program to count the number of digits in a given number =====

Enter a positive integer : 5678

The number of digits in the entered number: 5678 is 4

RESULTS: Executed without errors

39 | C + + R E C O R D B O O K
PROGRAM 2:
AIM: Write a program to reverse a given integer
ALGORITHM:
1. Start
2. Declare variables
3. Allows input into variables
4. Display reverse
5. Stop
CODE:

#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
int main()
{
system("Title Problem 4.3"); //gives title to the Command
Prompt
system("Color 1C");
int number,reversed_num=0;
cout << "Enter a number for find reverse" << endl; //asks
user to enter an Integer
cin>>number; //Taking a number as an input and stores
number variable
cout << "you entered: "<<number;
for(; number!=0;)
{
reversed_num=reversed_num*10;
reversed_num=reversed_num+number%10;
reversed_num=reversed_num-2;
number=number/10; //updating statements
}
cout << "\nReversed number is: "<<reversed_num;
getch();
return 0;//end program
}

OUTPUT: Enter a number for find reverse


5
you entered: 5
Reversed number is: 3

RESULTS: Executed without errors

40 | C + + R E C O R D B O O K
PROGRAM 3:
AIM: Write a program to print number in reverse order with a difference of 2.
ALGORITHM:
1. Start
2. Declare variables
3. Allows input into variables
4. Use while to reverse the input number with difference 2
5. Display reverse of number
6. Stop
CODE:

#include <iostream>//start program


using namespace std;
int main ( )
{
int n, reverse=0, rem; //declaration of variables
cout<<"Enter a number: \n"; //prompt user to make an input
cin>>n; //user makes an input
while(n !=0)
{
rem=n%10;
reverse=(reverse*10+rem)-2;
n/=10;
}
cout<<"Reversed Number: "<<reverse<<endl;
return 0; //end program
}

OUTPUT: Enter a number:


7
Reversed Number: 5

RESULTS: Executed without errors

41 | C + + R E C O R D B O O K
PROGRAM 4:
AIM: Write a program to print the sum of digits of a number using for loop
ALGORITHM:
1. Start
2. Declare variables
3. Allows input into variables
4. Use loop to calclulate for sum of digits
5. Display sum
6. Stop
CODE:

#include <iostream>
using namespace std;
int main()
{
int num1, num2, r, sum=0; //declaration of variables
cout << "\n\n Find the sum of digits of a given number:\n";
cout << "----------------------------------------------\n";
cout << " Input a number: ";
cin >> num1; //user inputs number1
num2 = num1;
while (num1 > 0)
{
r = num1 % 10;
num1 = num1 / 10;
sum = sum + r; //calculates sum
}
cout << " The sum of digits of " << num2 << " is: " << sum <<
endl; //displays sum of digits
return 0;//end program
}

OUTPUT:
Find the sum of digits of a given number:
----------------------------------------------
Input a number: 66
The sum of digits of 66 is: 12

RESULTS: Executed without errors

42 | C + + R E C O R D B O O K
PROGRAM 5:
AIM: Write a program to check whether a number is Palindrome or not
ALGORITHM:
1. Start
2. Declare variables
3. Allows input into variables
4. Use while loop to check whether number is palindrome or not
5. Diplay result
6. Stop

CODE:
#include <iostream>
using namespace std;
int main()
{
int n,r,sum=0,temp;
cout<<"Enter the Number \n";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
cout<<"Number is Palindrome.\n";
else
cout<<"Number is not Palindrome.";
return 0;//end program
}

OUTPUT: Enter a number:


7
Reversed Number: 5

RESULTS: Executed without errors

43 | C + + R E C O R D B O O K
PROGRAM 6:
AIM: Write a program to generate Fibonacci series.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use for loop and if statement to find for Fibonacci series
5. Display Fibonacci series
6. Stop
CODE:

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
system("Title LAB EXERCISE 4, Problem 4.6");//gives the
window title
system("color F2");
cout<<endl;
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms:\n ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0; //end program
}

44 | C + + R E C O R D B O O K
OUTPUT:
Enter the number of terms:
89
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,
1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418,
317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465,
14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
267914296, 433494437, 701408733, 1134903170, 1836311903, -1323752223,
512559680, -811192543, -298632863, -1109825406, -1408458269,
1776683621, 368225352, 2144908973, -1781832971, 363076002, -
1418756969, -1055680967, 1820529360, 764848393, -1709589543, -
944741150, 1640636603, 695895453, -1958435240, -1262539787,
1073992269, -188547518, 885444751, 696897233, 1582341984, -2015728079,
-433386095, 1845853122, 1412467027, -1036647147, 375819880, -
660827267, -285007387, -945834654, -1230842041, 2118290601, 887448560,
-1289228135, -401779575, -1691007710, -2092787285,

RESULTS: Executed without errors

45 | C + + R E C O R D B O O K
PROGRAM 7 :
If a four-digit number is input through the keyboard, write a program to
obtain the
sum of the first and last digit of this number.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use for loop to find sum of first and last digits
5. Display first and last digits
6. Stop
CODE:

#include <iostream>
using namespace std;

int main()
{
int n,first,last,sum;
cout << "\n\n Find the sum of first and last digit of a
number:\n";
cout << "------------------------------------------------------
\n";
cout << " Input any number: ";
cin >> n;
first = n;
last=n % 10;
for(first=n;first>=10;first=first/10);
cout<<" The first digit of "<<n<<" is: "<<first<<endl;
cout<<" The last digit of "<<n<<" is: "<<last<<endl;
cout<<" The sum of first and last digit of "<<n<<" is:
"<<first+last<<endl;
return 0;//end program
}

OUTPUT:
Find the sum of first and last digit of a number:
------------------------------------------------------
Input any number: 32
The first digit of 32 is: 3
The last digit of 32 is: 2
The sum of first and last digit of 32 is: 5

RESULTS: Executed without errors

46 | C + + R E C O R D B O O K
PROGRAM 8:
AIM: Write a program to find GCD (greatest common divisor or HCF) and LCM
(least
common multiple) of two numbers.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use while loop to find HCF and LCM
5. Display HCF and LCM
6. Stop
CODE:
#include<iostream>
using namespace std;
int main()
{
int a, b, x, y, temp, hcf, lcm;
cout<<"\n Enter Two Numbers : \n";
cin>>x>>y;
a=x;
b=y;
while(b!=0)
{
temp=b;
b=a%b;
a=temp;
}
hcf=a;
lcm=(x*y)/hcf;
cout<<"\n HCF : "<<lcm<<"\n";
cout<<"\n LCM : "<<hcf<<"\n";
return 0;//ends program
}

OUTPUT:
Enter Two Numbers :
5
8
HCF : 40
LCM : 1

RESULTS: Executed without error

47 | C + + R E C O R D B O O K
Program 9(i-v)
AIM: To understand the programming using Loop & nested loop Statements (for, while, do-while)
TITLE: To write programs to display each of the patterns.
ALGORITHM:
1. Start program
2. Declares variable, add logic to print and display patterns using do, while loop and switch case
3. Ends program
CODE:

#include<iostream>//Start program
using namespace std;
void halfPyramid();
void invertedHalfPyramid();
void fullPyramid();
void invertedFullPyramid();
int main()
{ int ch;//declaration of variable
do //using the do-while loop
{ cout<<"1. Print Half Pyramid of Stars\n";
cout<<"2. Print Inverted Half Pyramid of Stars\n";
cout<<"3. Print Full Pyramid of Stars\n";
cout<<"4. Print Inverted Full Pyramid of Stars\n";
cout<<"5. Exit\n";
cout<<"Please Choose your Choice: ";
cin>>ch; //user makes an input
switch(ch) //using the switch case
{ case 1:
halfPyramid();
break;
case 2:
invertedHalfPyramid();
break;
case 3:
fullPyramid();
break;
case 4:
invertedFullPyramid();
break;
case 5:
return 0;
default:
cout<<"\nWrong Choice!";
break;
}

PTO

48 | C + + R E C O R D B O O K
} while(ch>=1 && ch<=4);
cout<<endl;
return 0; //ends program
}
void halfPyramid()
{
int i, j; //declaration of variables
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
cout<<" * "; //displays pattern for *
cout<<endl;
}
cout<<endl;
}
void invertedHalfPyramid()
{ int i, j; //declaration of variables
for(i=0; i<6; i++)
{ for(j=i; j<6; j++)
cout<<" $ ";//displays pattern for $
cout<<endl;
}
cout<<endl;
}
void fullPyramid()
{ int i, space, j; //declaration of variables
for(i=1; i<=6; i++)
{ for(space=1; space<=(6-i); space++)
cout<<" ";
for(j=1; j<=i; j++)
cout<<" & ";//displays pattern for &
cout<<endl;
}
cout<<endl;
}
void invertedFullPyramid()
{ int i, space, j; //declaration of variables
for(i=1; i<=6; i++)
{ for(space=1; space<i; space++)
cout<<" ";
for(j=i; j<=6; j++)
cout<<" @ ";//displays pattern for @
cout<<endl;
}
cout<<endl;
return o; // end program
}

49 | C + + R E C O R D B O O K
OUTPUT:
1. Print Half Pyramid of Stars
2. Print Inverted Half Pyramid of Stars
3. Print Full Pyramid of Stars
4. Print Inverted Full Pyramid of Stars
5. Exit
Please Choose your Choice: 1
*
* *
* * *
* * * *
* * * * *
* * * * * *
1. Print Half Pyramid of Stars
2. Print Inverted Half Pyramid of Stars
3. Print Full Pyramid of Stars
4. Print Inverted Full Pyramid of Stars
5. Exit
Please Choose your Choice: 2
$ $ $ $ $ $
$ $ $ $ $
$ $ $ $
$ $ $
$ $
$
1. Print Half Pyramid of Stars
2. Print Inverted Half Pyramid of Stars
3. Print Full Pyramid of Stars
4. Print Inverted Full Pyramid of Stars
5. Exit

50 | C + + R E C O R D B O O K
Please Choose your Choice: 3
&
& &
& & &
& & & &
& & & & &
& & & & & &

1. Print Half Pyramid of Stars


2. Print Inverted Half Pyramid of Stars
3. Print Full Pyramid of Stars
4. Print Inverted Full Pyramid of Stars
5. Exit
Please Choose your Choice: 4
@ @ @ @ @ @
@ @ @ @ @
@ @ @ @
@ @ @
@ @
@

1. Print Half Pyramid of Stars


2. Print Inverted Half Pyramid of Stars
3. Print Full Pyramid of Stars
4. Print Inverted Full Pyramid of Stars
5. Exit
Please Choose your Choice: 5

RESULT:
This program is executed successfully without any errors

51 | C + + R E C O R D B O O K
EXERCISE 5
PROGRAM 1:
AIM: Write a Program to Search an element in array.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Find the position of the input variable
5. Displays the number and its position
6. Stop
CODE:

#include<iostream>
#include<windows.h>
using namespace std;

int main()
{
system("Title LAB EXERCISE 5.1");
int arr[10], i, num, index;
cout<<"Enter 10 single Numbers:\n";
for(i=1; i<=10; i++)
cin>>arr[i];
cout<<"\nEnter a Number to Search: \n";
cin>>num;
for(i=1; i<=10; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"\nFound at Index No."<<index;
cout<<endl;
return 0; //end program
}

52 | C + + R E C O R D B O O K
OUTPUT:
Enter 10 single Numbers:
59
0
8
7
6
9
0
7
6
0
Enter a Number to Search:
8
Found at Index No.3

RESULTS: Executed without errors

53 | C + + R E C O R D B O O K
PROGRAM 2:
AIM:Write a Program to perform addition of all elements in Array.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use for loop
5. Display size of array
6. Stop
CODE:

#include<iostream> //start program


using namespace std;
int main ()
{
int arr[10], n, i, sum = 0; //declaration of variables
cout << "Enter the size of the array : ";
cin >> n;
cout << "\nEnter the elements of the array : \n";
for (i = 0; i < n; i++)
cin >> arr[i];
for (i = 0; i < n; i++)
{
sum += arr[i];
}
cout << "\nSum of array elements : " << sum; //sum of arrays
displayed
return 0; //end program
}

OUTPUT: Enter the size of the array : 5


Enter the elements of the array :
14
7
8
5
9
Sum of array elements : 43

RESULTS: Executed without error

54 | C + + R E C O R D B O O K
PROGRAM 3:
AIM: Write a Program to find the largest and smallest element in Array
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Use for loop and if statement in finding largest and smallest element
in array
5. Display largest and smallest element in array
6. Stop
CODE:
#include<iostream> //start Program
using namespace std;
int main ()
{
int arr[10], n, i, max, min; //declaration of variables
cout << "Enter the size of the array : \n";
cin >> n; //user inputs variables
cout << "Enter the elements of the array : \n";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max<<endl; //displays
largest number
cout << "Smallest element : " << min; //displays smallest
number
return 0; //end program
}

OUTPUT: Enter the size of the array :


2
Enter the elements of the array :
7
9
Largest element : 9
Smallest element : 7

RESULTS: Executed without errors


55 | C + + R E C O R D B O O K
PROGRAM 4:
AIM: Write a Program to reverse the array elements in C++ Programming.
ALGORITHM:
1. Start
2. Declare variables
3. Allow input into variables
4. Display original array
5. Use for loop in finding reverse array
6. Display reverse array
7. Stop
CODE: #include<iostream>
using namespace std;
int main()
{//start program
int arr[10], i; //declaration of variables
cout<<"Enter 10 Array Elements: \n";
for(i=0; i<10; i++)
cin>>arr[i]; //user inputs the arrays
cout<<"\nThe Original Array is:\n";
for(i=0; i<10; i++)
cout<<arr[i]<<" "; //original arrays displayed
cout<<"\n\nThe Reverse of Given Array is:\n";
for(i=(10-1); i>=0; i--)
cout<<arr[i]<<" "; //reversed arrays displayed
cout<<endl; //nextline
return 0; //end program
}

OUTPUT: Enter 10 Array Elements:


5
6
3
4
3
2
3
4
5
6
The Original Array is:
5634323456
The Reverse of Given Array is:
6543234365

RESULTS: Executed without error

56 | C + + R E C O R D B O O K
PROGRAM 5
AIM: To understand programming using different dimensions of Array.
TITLE: To write a Program for deletion of an element from the specified
location from Array

ALGORITHM:
1. Start program
2. Declaration of variables (arr, tot, elem, found)
3. User inputs ten arrays, for deletion.
4. Operation carried out and Displayed.
5. End program
CODE:

#include<iostream> //start program


using namespace std;
int main ( )
{ int arr [10], tot=10, i, elem, j, found=0; //declartion of
variables
cout<<"Enter 10 Array Elements: \n";
for (i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nEnter Element to Delete: \n";
cin>>elem; //user inputs an element
for (i=0; i<tot; i++)
{
if(arr[i]==elem)
{
for (j=i; j<(tot-1); j++)
arr[j] = arr[j+1];
found++;
i--;
tot--;
}
}
if(found==0)
cout<<"\nElement doesn't found in the Array!";
else
cout<<"\nElement Deleted Successfully!";
cout<<endl;
return 0; //end program
}

57 | C + + R E C O R D B O O K
OUTPUT:
Enter 10 Array Elements:
1
2
3
4
5
6
7
8
9
0
Enter Element to Delete:
7
Element Deleted Successfully!

RESULT:
This program is executed successfully without any errors.

58 | C + + R E C O R D B O O K
PROGRAM 6
AIM: To understand programming using different dimensions of Array.
TITLE: To Write a Program to access an element in 2-D Array.

ALGORITHM:
1. Start program
2. Declaration Of arrays to be displayed, and (i, j) variable declaration
3. Arrays displayed in 2-D.
4. End program
CODE:

#include<iostream>
using namespace std;
int main()
{// start program
int arr[4][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
int i, j; // declaration of variables
cout<<"The Two-dimensional Array is:\n"; // displays the two
dimentional array
for(i=0; i<4; i++)
{
for(j=0; j<2; j++)
cout<<arr[i][j]<<" ";
cout<<endl;
}
cout<<endl;
return 0; //end program
}

OUTPUT:
The Two-dimensional Array is:
1 2
3 4

5 6
7 8

RESULT:
This program is executed successfully without error.

59 | C + + R E C O R D B O O K
PROGRAM 7
AIM: To understand programming using different dimensions of Array.
TITLE: To write a program for addition of two matrices of any order in C ++.

ALGORITHM:
1. Start program
2. loop variable i to iterate rows and j to iterate columns.
3. Declaring the 3 matrices (2D arrays) m1-first matrix, m2- second
matrix and sum- stores the addition of the two matrices
4. User input variables for both matrices for computation
5. Displays the values of the addition outcome
6. End program
CODE:
PLEASE TURN OVER (PTO)

OUTPUT:
Welcome to a Program to demonstrate Addition of two Matrices
Enter the number of Rows and Columns of matrix :
2
2
Enter the 4 elements of first matrix :
1
2
3
4
Enter the 4 elements of second matrix :
5
6
4
3
The first matrix is :
1 2
3 4
The second matrix is :
5 6
4 3
The Sum matrix is :
6 8
7 7

RESULT:
This program was run successfully without any errors.

60 | C + + R E C O R D B O O K
#include <iostream> //start program
using namespace std;
int main() {
cout << " Welcome to a Program to demonstrate Addition of two Matrices
\n\n";
//loop variable i to iterate rows and j to iterate columns.
int row, col, i, j;
//Declaring the 3 matrices (2D arrays) m1-first matrix, m2- second matrix
and sum- stores the addition of the two matrices
int m1[10][10], m2[10][10], sum[10][10];
cout << "\n\nEnter the number of Rows and Columns of matrix : \n";
cin >> row >> col;
cout << "\nEnter the " << row * col << " elements of first matrix : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cin >> m1[i][j];
}
} cout << "\nEnter the " << row * col << " elements of second matrix : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cin >> m2[i][j];
}
}
for (i = 0; i < row; i++) //calculating the sum matrix {
for (j = 0; j < col; j++) {
sum[i][j] = m1[i][j] + m2[i][j];
}
}
cout << "\n\nThe first matrix is : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << m1[i][j] << " ";
}
cout << endl;
}
cout << "\n\n The second matrix is : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << m2[i][j] << " ";
}
cout << endl;
}
cout << "\n\nThe Sum matrix is : \n";
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
}
cout << "\n\n";

return 0;//end program


} 61 | C + + R E C O R D B O O K
PROGRAM 8
AIM: To understand programming using different dimensions of Array.
TITLE: To write a Program to multiply two 3 X 3 Matrices

ALGORITHM:
1. Start program
2. Declaration of variables
3. Assigning of values to variables
4. Display the First and Second 3x3 Matrices
5. Multiplication of the Matrices
6. Display the 3x3 Product matrix
7. End Program
CODE:
PLEASE TURN OVER ( PTO )

OUTPUT:
The first matrix is:
241
239
318

The second matrix is:


123
361
247

Product of the two matrices is:


16 32 17
29 58 72
22 44 66

RESULT:

This Program is terminated successfully without any error.

62 | C + + R E C O R D B O O K
#include<iostream> //start program
using namespace std;
int main() {
int product[10][10], r1=3, c1=3, r2=3, c2=3, i, j, k; //
declaration assigning of values
int a[3][3] = { {2, 4, 1} , {2, 3, 9} , {3, 1, 8} };
int b[3][3] = { {1, 2, 3} , {3, 6, 1} , {2, 4, 7} };
if (c1 != r2) {
cout<<"Column of first matrix should be equal to row of second
matrix";
} else {
cout<<"The first matrix is:"<<endl; //displays the first matrix
for(i=0; i<r1; ++i) {
for(j=0; j<c1; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"The second matrix is:"<<endl; //displays the second
variable
for(i=0; i<r2; ++i) {
for(j=0; j<c2; ++j)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j) {
product[i][j] = 0;
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k) {
product[i][j]+=a[i][k]*b[k][j];
}
cout<<"Product of the two matrices is:"<<endl; //displays the
product
for(i=0; i<r1; ++i) {
for(j=0; j<c2; ++j)
cout<<product[i][j]<<" ";
cout<<endl;
}
}
return 0; //end program
}

63 | C + + R E C O R D B O O K
PROGRAM 9

AIM: To understand programming using different dimensions of Array.


TITLE: To write a program to read a string and check for palindrome
without using string related function (a string is palindrome if its half is
mirror by itself eg: abcdcba)
ALGORITHM:
1. Start program
2. Declaration of variables y and x,
3. User inputs a Word
4. System tells Whether it’s a Palindrome or Not
5. End program
CODE:
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{//start program
char y[100], x[100]; //declaration of variable
cout << "Enter word" << endl;
cin >> y; //user inputs a word
strcpy(x, y);
strrev(x);
if (strcmp(y, x) == 0)
cout << "Palindrome"; //prints Palindrome
else
cout << "Not Palindrome \n";
system("pause");
cout<<endl;
return 0; //end program
}

OUTPUT 1:
Enter word
wow
Word is a Palindrome
Press any key to continue . . .

OUTPUT 2:
Enter word
programming
Word is Not a Palindrome
Press any key to continue . . .

RESULT: This Program is run successfully without any errors.

64 | C + + R E C O R D B O O K
PROGRAM 10
AIM: To understand programming using different dimensions of Array.
TITLE: To Write a program to accept a string and count the number of
vowels present in this string.
ALGORITHM:
1. Start program
2. Declares str, v, c, n, s
3. Conditions and computations to find the number of vowel, consonant,
number and special symbols
4. User inputs Words, Symbols or Numbers
5. System detects and displays the Number of vowels, consonants,
numbers or special characters
6. End program
CODE:

#include<iostream>
#include<string.h>
using namespace std;
int main ()//start program
{ char str[50]; //declaration of variables
int v = 0, c = 0, n = 0, s = 0;
cout << "Enter a string : ";
gets(str);
for (int i = 0; str[i]!='\0'; ++i)
{
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o'
||
str[i] == 'u' || str[i] == 'A' || str[i] == 'E' || str[i] == 'I'
|| str[i] == 'O' || str[i] == 'U')
++v;
else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i]
<= 'Z'))
++c;
else if (str[i] >= '0' && str[i] <= '9')
++n;
else
++s;
}
cout << "Number of vowels : " << v; // prints number of vowels
cout << "\nNumber of consonants : " << c; //prints number of consonants
cout << "\nNumber of numbers :" << n; //prints number of numbers
cout << "\nNumber of special characters : " << s; // prints number of
special character
return 0; //end program
}

65 | C + + R E C O R D B O O K
OUTPUT:
Enter a string : pneumonoultramicroscopicsilicovolcanoconiosis**2342
Number of vowels : 20
Number of consonants : 25
Number of numbers :4
Number of special characters : 2

RESULT:
This Program is executed without any errors.

66 | C + + R E C O R D B O O K

You might also like