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

lab_manual_Problem_solving_programming_in_cPP2024[1]

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)
26 views

lab_manual_Problem_solving_programming_in_cPP2024[1]

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/ 59

SAGE UNIVERSITY, INDORE

Institute of Advance Computing

Laboratory Manual
Course Type- Practical
Academic Session: June-Dec 2024

Program : B.Tech. CST


Semester : I
Course Code : ACTDCPSP001P
Course Name : Problem Solving Programming Using C++

Submitted To Submitted By
Dr. Atul Nandwal Name :

Associate Professor Enrollment No :


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Table of Contents
Exp. Page Experiment Date Submission Signature
No. Title of Experiment No. Date
WAP that accepts the marks of 4 12-Sep-24 13-Sep-24
5 subjects and finds the sum
and percentage
marks Obtained by the student.
1
WAP that calculates the 9 19-Sep-24 30-Sep-24
Simple Interest and Compound
Interest. The Principal,
2 Amount, Rate of Interest and
Time are entered through the
keyboard
WAP to calculate the area and 13 26-Sep-24 27-Sep-24
3 circumference of a circle.
WAP that accepts the 16 3-Oct-24 4-Oct-24
4 temperature in Centigrade and
converts into Fahrenheit using
the formula C/5=(F-32)/9
WAP that swaps values of two 18 10-Oct-24 11-Oct-24
5 variables using a third variable.
WAP that checks whether the 21 24-Oct-24 25-Oct-24
6 two numbers entered by the
user are equal or not
WAP to find the greatest of 24 7-Nov-24 8-Nov-24
7 three numbers.
WAP that finds whether a 27 14-Nov-24 22-Nov-24
8 given number is even or odd.
WAP that tells whether a given 29 21-Nov-24 22-Nov-24
year is a leap year or not.
9
WAP that accepts marks of five 32 28-Nov-24 29-Nov-24
subjects and finds percentage
10 and prints grades.
Write a program to calculate 36 12-Dec-24 13-Dec-24
the sum of the elements of an
11 array.
Create a program to 41 19-Dec-24 20-Dec-24
12 manage a student database
that stores their name, age,
and
grade.

Vishal Chakradhari /24ADV3CST1183 Page | 2


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

WAP to check given string is a 48 26-Dec-24 27-Dec-24


13 palindrome or not without
using predefine function of
string header file
14 Write a program that 52 26-Dec-24 27-Dec-24
calculates the average of a list
of numbers using functions
Write a program that uses
functions to perform
15 mathematical operations and 56 2-Jan-24 3-Jan-24
demonstrate the concepts of
call by value, call by
reference, and recursive
functions.

Vishal Chakradhari /24ADV3CST1183 Page | 3


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[1]
Experiments with Results and Viva-Voice Questions
Experiment
1
Number
WAP that accepts the marks of 5 subjects and finds the sum and
Experiment Title percentage marks Obtained by the student.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Use the following algorithm to calculate total marks and


Description percentage of five subjects; as follows:
• Accept the marks for 5 subjects.
• Compute the total sum of marks.
• Calculate the percentage based on the total marks out of 500.
• Display the results, including both the sum of marks and the
percentage.

Experiment #include <iostream.h>


Code/ Procedure #include <conio.h>
void main( )
{
float sub1, sub2, sub3, sub4, sub5, sum, percentage;
clrscr();
cout << "Enter the marks of 5 subjects: \n";
cout << "Subject 1: ";
cin >> sub1;

cout << "Subject 2: ";


Vishal Chakradhari /24ADV3CST1183 Page | 4
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

cin >> sub2;


cout << "Subject 3: ";
cin >> sub3;

cout << "Subject 4: ";


cin >> sub4;

cout << "Subject 5: ";


cin >> sub5;

sum = sub1 + sub2 + sub3 + sub4 + sub5;

percentage = (sum / 500) * 100;


cout << "\nSum of marks: " << sum;
cout << "\nPercentage: " << percentage << "%";
getch();
}

Input Enter the marks of 5 subjects:


Subject 1: 45
Subject 2: 80
Subject 3: 60
Subject 4: 67
Subject 5: 81

Vishal Chakradhari /24ADV3CST1183 Page | 5


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Output/Result

Sample viva Q1. What formulae are used in this program to calculate the total marks and
question percentage?
Ans. The total marks are calculated by adding the marks obtained in each subject.
The percentage is calculated by dividing the total marks by 5.

Q2. What data types are used in this program?


Ans. Float data type is used to store the marks obtained in each subject and
the calculated values of total marks and percentages.

Q3. Can this program be used to calculate the percentage of marks obtained
in more than five subjects?
Ans. No, this program is specifically designed to calculate the percentage of
marks obtained in five subjects only. If you need to calculate the percentage of
marks obtained in more subjects, you will need to modify the program accordingly.

Q4. How to find the percentage of marks?


Ans. To find the percentage of marks, you need to know the total marks
obtained in a particular subject and Once you have these values, you can
use the following formula to calculate the percentage:
Percentage = (Total Marks / Maximum Marks) x 100

Q5. What is the purpose of clrscr() in this program?

Ans: The clrscr() function is used to clear the console screen before displaying the
output. It makes the output appear clean and more readable.

Q: What is C++?

A: C++ is a general-purpose programming language that supports procedural,


object-oriented, and generic programming.

Vishal Chakradhari /24ADV3CST1183 Page | 6


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Q: What is the file extension of a C++ program?

A: .cpp

Q: What is the purpose of #include <iostream>?

A: It is used to include the input/output stream library.

Q: What are data types in C++?

A: Data types in C++ specify the type of data that a variable can hold, such as
integers, floating-point numbers, characters, etc.

Q: What is the difference between int and float in C++?

A: int is used for whole numbers (e.g., 10, -3), while float is used for numbers with
decimal points (e.g., 10.5, -3.14).

Q: What is the size of a char in C++?

A: The size of a char is 1 byte.

Q: How many types of data types are there in C++?

A: C++ has four types of data types:

• Basic types (e.g., int, char, float, bool)


• Derived types (e.g., arrays, pointers, functions)
• Enumeration types (e.g., enum)
• User-defined types (e.g., class, struct, union)

Q: What is the difference between signed and unsigned data types?

A: Signed data types can store both positive and negative values, whereas
unsigned data types can only store positive values.

Q: What is the purpose of the bool data type in C++?

A: The bool data type is used to store Boolean values, which are either true or
false.

Q: What is the range of the int data type in C++?

A: The range of int depends on the system but is typically from -32768 to +32767

Q: Can a double data type store more precision than a float?

Vishal Chakradhari /24ADV3CST1183 Page | 7


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

A: Yes, double has more precision and a larger range than float.

Q: What is a void data type in C++?

A: The void data type is used to indicate that a function does not return any value.

Q: Can we use auto as a data type in C++?

A: Yes, auto allows the compiler to automatically deduce the type of a variable
based on its initializer.

Vishal Chakradhari /24ADV3CST1183 Page | 8


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[2]
Experiments with Results and Viva-Voice Questions

Experiment
2
Number
WAP that calculates the Simple Interest and Compound
Experiment Title
Interest. The Principal, Amount, Rate of Interest and Time are
entered through the keyboard.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Use the following algorithm to calculate Simple interest and


Description Compound interest ; as follows:
1. Declare the variable Principal, Rate, Time, Amount,
SI, CI.
2. Store the value of all variables.
3. Calculate Simple Interest using SI = (P*R*T) / 100.
4. Then calculate the Amount using A= P * (pow((1 +
R/ 100), T)).
5. Calculate Compound Interest Interest using CI = A-P
6. Finally, print resultant values SI, and CI.

Vishal Chakradhari /24ADV3CST1183 Page | 9


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>
#include<math.h>

void main() {
clrscr();

float principal, rate, time, simpleInterest, compoundInterest,


amount;

cout << "Enter the Principal amount: ";


cin >> principal;
cout << "Enter the Rate of Interest: ";
cin >> rate;
cout << "Enter the Time (in years): ";
cin >> time;

simpleInterest = (principal * rate * time) / 100;

amount = principal * pow((1 + rate / 100), time);


compoundInterest = amount - principal;

cout << "\nSimple Interest = " << simpleInterest;


cout << "\nCompound Interest = " << compoundInterest;

getch();
}
Input Enter the Principal amount: 1000
Enter the Rate of Interest: 2
Enter the Time (in years): 2

Output/Result

Vishal Chakradhari /24ADV3CST1183 Page | 10


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Sample viva 1. What is Simple Interest (SI)?


question • Answer: Simple Interest is the interest calculated on the original principal
amount for the entire duration of the loan or investment. The formula is:

Simple Interest=P×R×T/100

where P is the Principal, R is the Rate of Interest, and T is the Time in


years.

2. What is Compound Interest (CI)?

• Answer: Compound Interest is the interest calculated on the initial


principal as well as the interest that has already been added to the
principal. The formula is:

Compound Interest=P(1+100/R)T−P

where P is the Principal, R is the Rate of Interest, and T is the Time in


years.

3. What is the difference between Simple Interest and Compound Interest?

• Answer: The key difference is that Simple Interest is calculated only on


the original principal, while Compound Interest is calculated on both the
principal and the accumulated interest.

6. What is the role of the pow() function in this program?

• Answer: The pow() function is used to raise the base(1+R to the power of
T (the time period), which is part of the Compound Interest formula.

7. What will be the output if the rate of interest is 0%?

• Answer: If the rate of interest is 0%, both the Simple Interest and
Compound Interest will be 0 because no interest will be applied.

8. What happens if the time period is 0 years?

• Answer: If the time period is 0, the interest will be 0, regardless of the


principal or rate of interest, because the interest is calculated over a period
of time.

9. Can the program handle negative values for Principal, Rate, or Time?

• Answer: The program does not handle negative input correctly. If the user
enters negative values, it will lead to incorrect results. The program should

Vishal Chakradhari /24ADV3CST1183 Page | 11


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

ideally include input validation to handle such cases.

10. How can the program be modified to calculate Compound Interest for
monthly compounding?

• Answer: To calculate Compound Interest with monthly compounding, the


formula for Compound Interest should account for 12 compounding
periods per year. The rate and time should be adjusted to reflect monthly
compounding: CI=P×(1+100R/12)12×T−P

11. What is the expected output if the Principal is 1000, Rate is 5%, and Time
is 2 years?

• Answer: The program will display the Simple Interest and Compound
Interest based on the entered values. For example:
o Simple Interest = 100
o Compound Interest = 102.5

12. How does the program calculate the Compound Interest if the principal is
invested for multiple years?

• Answer: The program uses the formula for Compound Interest, which
calculates the total amount after the given time period by compounding the
interest annually, and then subtracts the principal to get the Compound
Interest.

13. What will happen if the user enters a decimal number for the principal,
rate, or time?

• Answer: The program can handle decimal numbers, and the calculation
will proceed correctly. For example, if the principal is 1000.50, the interest
will be calculated on the decimal value.

14. Why is the getch() function used in this program?

• Answer: The getch() function is used to wait for the user to press a key
before closing the program. It ensures that the output is visible to the user
until they acknowledge it by pressing any key.

Vishal Chakradhari /24ADV3CST1183 Page | 12


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[3]
Experiments with Results and Viva-Voce Questions
Experiment
3
Number
WAP to calculate the area and circumference of a circle.
Experiment Title

Demonstrate an understanding of computer systems and their


Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input: The program asks for the radius of the circle.
Description Processing:
• The area of the circle is calculated using the formula:

Area=πr2

• The circumference of the circle is calculated using the


formula:

Circumference=2πr

• Instead of defining Pi explicitly, we use the constant


M_PI from the math.h library, which provides the value
of Pi (approximately 3.14159).

Vishal Chakradhari /24ADV3CST1183 Page | 13


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>
#include<math.h>
void main() {
clrscr();

float radius, area, circumference;

cout << "Enter the radius of the circle: ";


cin >> radius;

area = M_PI * radius * radius;


circumference = 2 * M_PI * radius;

cout << "\nArea of the circle: " << area << endl;
cout << "Circumference of the circle: " << circumference <<
endl;

getch();
}

Input
Enter the radius of the circle: 5

Output/Result

Sample viva Q1. What is the formula used to calculate the area of the circle?
question Ans: The formula to calculate the area of the circle is Area=πr2

• where r is the radius of the circle and π is the constant approximately


equal to 3.14159.

Vishal Chakradhari /24ADV3CST1183 Page | 14


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Q2. What is the formula used to calculate the circumference of the circle?

• Ans: The formula to calculate the circumference of the circle is


Circumference=2πr, where r is the radius of the circle and π\piπ is
approximately 3.14159.

Q3 Why did you use the constant M_PI instead of defining Pi manually?

• Ans: The constant M_PI is defined in the math.h library, and it provides the
value of Pi. Using this built-in constant ensures better accuracy and
prevents hardcoding the value of Pi, which could lead to errors or
inconsistencies.

Q4 What would happen if the radius entered is negative?

• Ans: The program does not handle negative input for the radius. If a
negative radius is entered, the program will calculate an invalid result.
Input validation could be added to check for valid positive radius values.

Q5 What does the getch() function do in this program?

• Ans: The getch() function waits for the user to press any key before the
program terminates, allowing the user to view the output before the
window closes.

Q6 Can this program be used for other shapes, like squares or rectangles?

• Ans: No, this program is specifically designed for calculating the area and
circumference of a circle. Different formulas are required for other shapes,
such as squares or rectangles.

Vishal Chakradhari /24ADV3CST1183 Page | 15


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[4]
Experiments with Results and Viva-Voice Questions

Experiment 4
Number
Experiment Title WAP that accepts the temperature in Centigrade and converts
into Fahrenheit using the formula C/5=(F-32)/9
Course Design and develop algorithms using flowcharts, pseudo
Outcome(CO) code, and a selected programming language to solve
specificprogramming problems.
Hardwar Computer System with 2GB RAM and
e HardDisk storage
Tools/
Apparatus
Required Software Turbo C/C++ OR AN OTHER COMPILER

Experiment • Input: Take the temperature in Centigrade from the user.


Description • Process:

• Use the formula F=9/5*C+32 to calculate the temperature in


Fahrenheit.

• Output: Display the temperature in Fahrenheit.

Experiment Code/ #include <iostream.h>


Procedure #include <conio.h>

void main() {
float centigrade, fahrenheit;

clrscr();
cout << "Enter temperature in Centigrade: ";
cin >> centigrade;

fahrenheit = (9.0 / 5.0) * centigrade + 32;

Vishal Chakradhari /24ADV3CST1183 Page | 16


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

cout << "\nTemperature in Fahrenheit: " << fahrenheit;

getch();
}

Input Enter temperature in Centigrade: 5

Output/Result

Sample Viva 1. What is the formula used to convert Centigrade to Fahrenheit?


Questions with • The formula is: F=9/5*C+32
Answers
2. What is the purpose of the clrscr() function?

• clrscr() is used to clear the screen in Turbo C++ to provide a clean


interface for the user.

3. Why are float variables used for the temperatures?

• float is used because the temperature values can be decimal numbers,


and float stores such values with precision.

4. What does the getch() function do?

• getch() is used to capture a key press from the user, which stops the
program from immediately closing. It waits for the user to press a key.

5. Can you explain the significance of #include<iostream.h> and


#include<conio.h>?

• #include<iostream.h> is used to include the Input-Output library, which


provides functions like cin and cout.
• #include<conio.h> is used for functions like clrscr() and getch() which
are part of the C++ console input/output library.

Vishal Chakradhari /24ADV3CST1183 Page | 17


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[5]
Experiments with Results and Viva-Voice Questions
Experiment
5
Number
WAP that swaps values of two variables using a third
Experiment Title
variable.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Initialization:
Description
• The two variables a and b are initialized with values 5 and
10, respectively.

Displaying Initial Values:

• The program prints the values of a and b before the swap.

Swapping Process:

• A third variable temp is introduced to temporarily store


the value of a.
• The value of b is assigned to a, and then the original value
of a (stored in temp) is assigned to b.

Displaying Swapped Values:

• Finally, the program prints the values of a and b after the


swap to confirm that the values have been swapped.

Vishal Chakradhari /24ADV3CST1183 Page | 18


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();

int a, b, temp;

cout << "Enter the value of a: ";


cin >> a;

cout << "Enter the value of b: ";


cin >> b;

cout << "\nBefore swapping: " << endl;


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

temp = a;

a = b;
b = temp;

cout << "\nAfter swapping: " << endl;


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

getch();
}

Input Enter the value of a: 5


Enter the value of b: 10

Vishal Chakradhari /24ADV3CST1183 Page | 19


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Output/Result

Sample viva 1. What is the purpose of this C++ program?


question • The purpose of this program is to swap the values of two variables using a
third temporary variable and display the result before and after the swap.

2. Why do we use a third variable (temp) in the swapping process?

• The third variable temp is used to temporarily store one of the values so
that it is not overwritten during the swapping process. Without temp, we
would lose the value of one variable when assigning the value of the other.

4. What would happen if we didn't use the third variable temp in this
program?

• If we didn’t use the third variable temp, we would overwrite the value of a
when we assign b to a, and the original value of a would be lost. The swap
would not be correct.

5. Can this program be modified to swap values without using a third


variable?

• Yes, this can be done using arithmetic operations (e.g., addition and
subtraction) or XOR bitwise operation to swap values without a temporary
variable.

6. Explain the logic behind the swapping of values in the program.

• First, the value of a is stored in the temporary variable temp. Then, the
value of b is assigned to a, and finally, the value stored in temp (which is
the original value of a) is assigned to b,

Vishal Chakradhari /24ADV3CST1183 Page | 20


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[6]
Experiments with Results and Viva-Voice Questions
Experiment
6
Number
WAP that checks whether the two numbers entered by the
Experiment Title
user are equal or not.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input Numbers:


Description
• The program will prompt the user to input two numbers
(integers). These values will be stored in two variables,
say num1 and num2.

Comparison:

• The program will use an if statement to check if the


values of num1 and num2 are equal (num1 == num2).

Output:

• If the numbers are equal, the program will print: "The


numbers are equal."
• If the numbers are not equal, the program will print: "The
numbers are not equal."

Experiment #include <iostream.h>


Code/ Procedure #include <conio.h>

Vishal Chakradhari /24ADV3CST1183 Page | 21


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

void main() {
clrscr(); .
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
if (num1 == num2) {
cout << "The numbers are equal." << endl;
} else {
cout << "The numbers are not equal." << endl;
}
getch();
}

Input Enter first number: 5


Enter second number: 5.
Output/Result

Sample viva 1 How does the program check if the two numbers are equal?
• The program uses an if statement to compare the two numbers. If num1
question == num2, it means the numbers are equal; otherwise, they are not equal.
2 What will happen if the user enters two identical numbers?

• If the user enters two identical numbers, the program will output that "The
numbers are equal."
3 What happens if the user enters two different numbers?
• If the user enters two different numbers, the program will output that "The
numbers are not equal."

Q: What is a conditional statement in C++?

A: A conditional statement is a feature in C++ that performs different actions based


on whether a specified condition evaluates to true or false.

Q: What are the types of conditional statements in C++?

Vishal Chakradhari /24ADV3CST1183 Page | 22


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

A: The main types of conditional statements are:if statement

• if-else statement
• else if ladder
• switch statement

Q: What is the purpose of the break statement in a switch block?

A: The break statement prevents the execution from falling through to the next
case in the switch block.

Q: Can we use relational operators in an if condition?

A: Yes, relational operators like <, >, ==, !=, <=, >= can be used in an if condition.

Q: Is a default case in a switch statement mandatory?

A: No, the default case is optional, but it is good practice to include it to handle
unexpected values.

Vishal Chakradhari /24ADV3CST1183 Page | 23


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[7]
Experiments with Results and Viva-Voice Questions
Experiment
7
Number
WAP to find the greatest of three numbers.
Experiment Title

Demonstrate an understanding of computer systems and their


Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input:
Description
• The program prompts the user to enter three integers
(num1, num2, and num3) using cin >> num1 >> num2 >>
num3;

Comparison Logic:

• The program uses a series of if-else if-else statements to


compare the numbers:
• First, it checks if num1 is greater than or equal to
both num2 and num3.
• If not, it checks if num2 is greater than or equal to
the other two.
• If both conditions are false, it concludes that num3
is the greatest.

Vishal Chakradhari /24ADV3CST1183 Page | 24


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();

int num1, num2, num3;

cout << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

if (num1 >= num2 && num1 >= num3) {


cout << num1 << " is the greatest number.";
} else if (num2 >= num1 && num2 >= num3) {
cout << num2 << " is the greatest number.";
} else {
cout << num3 << " is the greatest number.";
}

getch();
}

Input Enter three numbers: 25 50 10

Output/Result

Sample viva 1. How does the program compare the three numbers?
question • The program uses a series of if-else statements to compare the three
numbers. It checks if the first number is greater than or equal to the other
two, then checks the second number, and if neither is true, it assumes the
third number is the greatest.

2. What is the expected output if all three numbers are equal?

• The program will display the number as the greatest, since all the numbers

Vishal Chakradhari /24ADV3CST1183 Page | 25


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

are equal.

3. What data types are used in this program?

• The program uses the int data type to store the three numbers (num1,
num2, num3).

4. What happens if the user enters negative numbers?

• The program will still work correctly and determine the greatest number,
whether positive or negative.

5. How does the program determine which number is the greatest?

• It compares the numbers using relational operators (>=). It checks if one


number is greater than or equal to the other two and determines which
number is the greatest.

6. Can this program handle decimal numbers (floating-point numbers)?

• No, the program is designed to handle integers only. If a user enters a


decimal number, the program will produce an error or unexpected
behavior.

7. What is the output if the numbers entered are 5, 10, and 15?
• The output will be 15 is the greatest number.

8. What is the role of clrscr() in this program?

• clrscr() clears the screen before the program starts, giving a clean display.

9. What will happen if the user enters the same value for all three numbers?

• The program will display that the entered number is the greatest, as all
values are equal.

Vishal Chakradhari /24ADV3CST1183 Page | 26


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[8]
Experiments with Results and Viva-Voice Questions

Experiment
8
Number
WAP that finds whether a given number is even or odd.
Experiment Title

Demonstrate an understanding of computer systems and their


Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Taking user input for a number.


Description
Using the modulus operator to check the remainder when the
number is divided by 2.

Using conditional statements to output whether the number is


even or odd.

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();

int num;

cout << "Enter a number: ";


cin >> num;
Vishal Chakradhari /24ADV3CST1183 Page | 27
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

if (num % 2 == 0) {
cout << num << " is an Even Number.";
} else {
cout << num << " is an Odd Number.";
}

getch();
}

Input Enter a number: 10

Output/Result

1. What is an even number?


• An even number is a number that is divisible by 2 with no
Sample viva remainder.
question 2. What is an odd number?
• An odd number is a number that, when divided by 2, leaves a
remainder of 1.
3. What does the modulus operator % do?
• The modulus operator returns the remainder of a division. For
example, num % 2 gives the remainder when num is divided by 2.
4. How does the program check if a number is even?
• The program checks if the remainder when the number is divided
by 2 is 0. If true, the number is even.
5. What will happen if the user enters a negative number?
• The program will still work correctly. The modulus operation will
determine if the negative number is even or odd.
6. What will happen if the user enters 0?
• The program will display "0 is an Even Number," as 0 is divisible
by 2 with no remainder.
7. Can this program handle non-integer inputs?
• No, the program expects an integer input. If a non-integer value is
entered, it may cause an error.
8. What would happen if the user enters a decimal number?
• The program is designed to accept integers, so entering a decimal
number will likely cause an error or unexpected behavior.
9. What is the output if a user enters the number 7?
• The output will be "7 is an Odd Number," since 7 divided by 2

Vishal Chakradhari /24ADV3CST1183 Page | 28


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

leaves a remainder of 1.

[9]
Experiments with Results and Viva-Voice Questions
Experiment
9
Number
WAP that tells whether a given year is a leap year or not.
Experiment Title

Demonstrate an understanding of computer systems and their


Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input:
Description
• The program asks the user to enter a year using cin >>
year;.

Leap Year Check:

• The if condition checks if the year is divisible by 400 or


divisible by 4 but not divisible by 100.
• If any of these conditions are true, it prints that the year
is a leap year. Otherwise, it prints that the year is not a
leap year

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();
Vishal Chakradhari /24ADV3CST1183 Page | 29
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

int year;

cout << "Enter a year: ";


cin >> year;

if ((year % 400 == 0) || (year % 4 == 0 && year % 100 !=


0)) {
cout << year << " is a Leap Year.";
} else {
cout << year << " is not a Leap Year.";
}

getch();
}

Input Enter a year: 2024

Output/Result

Sample viva 1. What is the purpose of this program?


• The program checks whether a given year is a leap year or not
question based on the rules of leap years.
2. What is a leap year?
• A leap year is a year that has 366 days instead of 365. It occurs
every 4 years, except for years divisible by 100 unless they are
also divisible by 400.
3. What condition is used to determine if a year is a leap year?
• A year is a leap year if it is divisible by 400, or if it is divisible by 4
but not divisible by 100.
4. What is the purpose of the clrscr() function?
• clrscr() clears the console screen before the program starts,
ensuring the output is clean and visible.
5. What will happen if a year divisible by 100 but not 400 is entered?
• The program will output that the year is not a leap year.
6. What is the role of the modulus operator % in this program?
• The modulus operator is used to find the remainder when a year is
Vishal Chakradhari /24ADV3CST1183 Page | 30
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

divided by 4, 100, or 400. This helps determine if the year satisfies


the leap year conditions.
7. Can this program handle all types of years correctly?
• Yes, the program handles all cases correctly, including years
divisible by 100 and 400.
8. What happens if a non-numeric value is entered as the year?
• The program may not handle non-numeric input properly since it's
designed to accept only integers. A more advanced version could
include input validation.
9. What happens if a user enters a negative year?
• The program will still check the leap year conditions for the
negative year, but logically, the concept of leap years doesn't
apply to negative years. This would require additional validation to
handle such cases.
10. What is the expected output if a user enters the year 2000?
• The output will be "2000 is a Leap Year," because it is divisible by
400.

Vishal Chakradhari /24ADV3CST1183 Page | 31


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[10]
Experiments with Results and Viva-Voice Questions
Experiment
10
Number
WAP that accepts marks of five subjects and finds percentage
Experiment Title
and prints grades.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input Marks:


Description
• The program asks the user to input marks for 5 subjects. It
stores these marks in the marks array.
• The totalMarks variable accumulates the total of all the
marks entered.

Calculate Percentage:

• The percentage is calculated by dividing the total marks


by the maximum possible marks (500 in this case) and
multiplying by 100 to get the percentage.

Grade Assignment:

• Based on the percentage, the program assigns a grade:


o 'A' for 90% and above
o 'B' for 80% to 89.99%
o 'C' for 70% to 79.99%
o 'D' for 60% to 69.99%
o 'F' for below 60%

Vishal Chakradhari /24ADV3CST1183 Page | 32


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();

float marks[5], total = 0, percentage;


char grade;

cout << "Enter marks for 5 subjects:\n";


for(int i = 0; i < 5; i++) {
cout << "Enter marks for subject " << i+1 << ": ";
cin >> marks[i];
total += marks[i];
}

percentage = (total / 500) * 100;

if(percentage >= 90)


{
grade = 'A';
}
else
if(percentage >= 75)
{
grade = 'B';
}
else
if(percentage >= 60)
{
grade = 'C';
}
else if(percentage >= 50)
{
grade = 'D';
}

Vishal Chakradhari /24ADV3CST1183 Page | 33


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

else {
grade = 'F'; // Fail
}

cout << "\nTotal Marks: " << total << "/500";


cout << "\nPercentage: " << percentage << "%";
cout << "\nGrade: " << grade;

getch();
}

Input Enter marks for 5 subjects:


Enter marks for subject 1: 90
Enter marks for subject 2: 85
Enter marks for subject 3: 60
Enter marks for subject 4: 45
Enter marks for subject 5: 90
Output/Result

Sample viva 1. What does this program do?


• The program calculates the total marks, percentage, and assigns
question a grade based on the marks entered for five subjects.
2. How does the program ensure that the entered marks are valid?
• The program uses a do-while loop to repeatedly ask the user for
marks until valid input (marks between 0 and 100) is entered.
3. What is the significance of the clrscr() function in this program?
• The clrscr() function clears the console screen to give a cleaner
display when the program runs. It's specific to Turbo C++.
4. What is the role of the getch() function in the program?
• The getch() function pauses the program until the user presses a
Vishal Chakradhari /24ADV3CST1183 Page | 34
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

key. This prevents the program window from closing immediately


after output is displayed.
5. What will happen if the user enters marks greater than 100?
• The program will keep asking for valid input (marks between 0 and
100) until the user provides valid marks.
6. What happens if the user enters marks as a negative number?
• The program will ask the user to re-enter the marks, as negative
marks are not valid.
7. How is the grade determined in this program?
• The grade is determined based on the percentage calculated from
the total marks. For example, a percentage above 90 results in
grade 'A'.
8. What data type is used for storing the marks, total, and percentage?
• The float data type is used for marks, total, and percentage, as
they can have decimal values.
9. How does the program calculate the percentage?
• The percentage is calculated by dividing the total marks by the
maximum marks (500) and then multiplying by 100.
10. Can this program handle more or fewer than five subjects?
• No, this program is specifically designed for five subjects. To
handle more or fewer subjects, the program would need to be
modified to take input dynamically.

Vishal Chakradhari /24ADV3CST1183 Page | 35


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[11]
Experiments with Results and Viva-Voice Questions
Experiment
11
Number
Write a program to calculate the sum of the elements of an
Experiment Title
array.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Input:
Description
• The program first asks the user to input the number of
elements for the array (n).
• Then, it takes the n elements from the user and stores
them in the array arr[].

Sum Calculation:

• The program calculates the sum of the array elements


using a for loop. Each element is added to the sum
variable.

Experiment #include<iostream.h>
Code/ Procedure #include<conio.h>

void main() {
clrscr();

Vishal Chakradhari /24ADV3CST1183 Page | 36


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

int n, sum = 0;
cout << "Enter the number of elements in the array: ";
cin >> n;

int arr[100];

cout << "Enter " << n << " elements:\n";


for(int i = 0; i < n; i++) {
cout << "Element " << i+1 << ": ";
cin >> arr[i];
}

for( i = 0; i < n; i++) {


sum += arr[i];
}
cout << "\nThe sum of the array elements is: " << sum <<
endl;

getch();
}

Input Enter the number of elements in


the array: 5
Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50

Vishal Chakradhari /24ADV3CST1183 Page | 37


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Output/Result

Sample viva 1. How does the program calculate the sum of the array elements?
question • The program uses a for loop to iterate through each element of the array
and adds each element to a variable (sum).

2. What is the role of the clrscr() function in the program?

• The clrscr() function is used to clear the screen before displaying any
output, making the program output look cleaner.

3. Why is the size of the array declared as arr[n]?

• The size of the array is dynamically defined based on the user input (n).
This ensures that the array size corresponds to the number of elements
the user wants to input.

4. What will happen if the user inputs a negative number for the size of the
array?

• If the user inputs a negative number for the array size, it will likely lead to
unpredictable behavior or errors, as array sizes cannot be negative.

5. What is the purpose of the cin statement?

• The cin statement is used to take input from the user. In this program, it is
used to input the number of elements and each element of the array.

6. What will the program output if the array elements are all zeros?

• If all the array elements are zeros, the sum will be zero.

7. Can this program work with negative numbers in the array?

• Yes, the program can handle negative numbers in the array. It will correctly

Vishal Chakradhari /24ADV3CST1183 Page | 38


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

add the negative values to the sum.

8. What happens if the user enters a value for n greater than the available
memory for the array?

• If the user enters a large value for n, it could lead to a memory overflow or
crash the program, especially in environments with limited memory.

9. What does the getch() function do in this program?

• The getch() function pauses the program until the user presses a key. It
allows the user to see the result before the program closes.

Q: What are the three types of loops in C++?

A: The three types of loops in C++ are:

for loop

while loop

do Q: How does a while loop work in C++?

A: A while loop continues to execute a block of code as long as the specified


condition evaluates to true.

Q: What is the difference between while and do-while loops?

A: A while loop checks the condition before executing the block, whereas a do-
while loop executes the block at least once before checking the condition.

Q: What is an infinite loop, and how can it occur?

A: An infinite loop is a loop that never terminates because the condition always
evaluates to true

Q: Can a for loop be written without an initialization or increment?

A: Yes, it is possible

for (; i < 5;)

Q: How can a break statement be used in a loop?

A: The break statement is used to exit a loop prematurely.

Vishal Chakradhari /24ADV3CST1183 Page | 39


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Q: What is the role of the continue statement in a loop?

A: The continue statement skips the rest of the current iteration and moves to the
next iteration of the loop

Q: How can you use nested loops in C++?

A: Nested loops are loops inside other loops.

Vishal Chakradhari /24ADV3CST1183 Page | 40


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[12]
Experiments with Results and Viva-Voice Questions
Experiment
12
Number
Create a program to manage a student database that stores
Experiment Title
their name, age, and grade.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Structure Definition:


Description
• We use a struct named Student to store student details,
including their name, age, and grade.

Input Number of Students:

• The program first asks the user how many students they
want to enter. The numStudents variable holds this value.

Student Array:

• A fixed array students[100] is used to store the student


data. You can change 100 to any other reasonable size,
but it limits the number of students.
• If the user enters more students than the array can hold,
you can add error handling, but in this basic example, we
assume that users won’t exceed the 100-student limit.

Menu System:

• The user can select from three options: Add a student,


Display all students, or Exit.

Vishal Chakradhari /24ADV3CST1183 Page | 41


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Adding Students:

• The program uses a for loop to input details for each


student and stores them in the students array.
• For each student, the name, age, and grade are input.

Display Students:

• If the user selects option 2, the program loops through the


array and displays all the students' details.

Experiment
Code/ Procedure #include <iostream.h>
#include <conio.h>
#include <string.h>

struct Student
{
char name[50];
int age;
char grade;
};

void main()
{
clrscr();
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;

if (numStudents <= 0)
cout << "Invalid number of students!\n";

Student students[100];

int choice;
do {
clrscr();
cout << "\nStudent Database Menu:\n";
Vishal Chakradhari /24ADV3CST1183 Page | 42
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

cout << "1. Add a student\n";


cout << "2. Display all students\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice)
{
case 1:
for (int i = 0; i < numStudents; i++)
{
cout << "\nEnter details for student " << (i + 1)
<< ":\n";
cout << "Enter student's name: ";
cin>>students[i].name;
cout << "Enter student's age: ";
cin >> students[i].age;
cout << "Enter student's grade: ";
cin >> students[i].grade;

}
break;

case 2:
if (numStudents > 0)
{
for (int i = 0; i < numStudents; i++)
{
cout << "\nStudent " << (i + 1) << ":\n";
cout << "\n Name: " << students[i].name ;
cout << "\n Age: " << students[i].age ;
cout << "\n Grade: " << students[i].grade ;
}
} else {
cout << "No students in the database.\n";
}
getch();
break;

case 3:
cout << "Thank you for using Programme \n
Vishal Chakradhari /24ADV3CST1183 Page | 43
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Exiting program...\n";
break;

default:
cout << "Invalid choice! Try again.\n";
}
} while (choice != 3);

getch();
}

Input Enter the number of students: 2


Output/Result Student Database Menu:
1. Add a student
2. Display all students
3. Exit
Enter your choice: 1

Enter details for student 1:


Enter student's name: John Doe
Enter student's age: 20
Enter student's grade: B

Enter details for student 2:


Enter student's name: Jane Smith
Enter student's age: 19
Enter student's grade: A

Student Database Menu:


1. Add a student
2. Display all students
3. Exit
Enter your choice: 2

Student 1:
Name: John Doe
Age: 20
Grade: B

Student 2:
Vishal Chakradhari /24ADV3CST1183 Page | 44
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Name: Jane Smith


Age: 19
Grade: A

Student Database Menu:


1. Add a student
2. Display all students
3. Exit
Enter your choice: 3

Exiting program...
Sample viva 1. What is the purpose of the clrscr() function in the program?
question • Answer: The clrscr() function is used to clear the screen in Turbo C++
IDE. It removes any previous output on the console before the program
displays new information.

2. What is the use of the cin.ignore() function in this program?

• Answer: The cin.ignore() function is used to clear the input buffer. It is


necessary after reading an integer or a character, as it discards any
remaining characters (like newline) in the input buffer to prevent issues
when reading strings using cin.getline().

3. What would happen if we didn't include the cin.ignore() after reading


numStudents or students[i].age?

• Answer: If cin.ignore() is not used, any leftover newline character (\n) from
the previous input would remain in the input buffer, which could cause
issues when using cin.getline() for reading strings, leading to skipping input
or incorrect behavior.

4. Why do we use the Student students[100]; array instead of dynamically


allocating memory?

• Answer: In this program, we use a statically declared array Student


students[100]; to store up to 100 students because we know the maximum
limit in advance. The program is designed for simplicity, and the array size
is predefined. If dynamic memory allocation was needed, new could be
used to allocate memory based on user input.

5. Explain the purpose of the switch statement in this program.

• Answer: The switch statement is used to present a menu and handle the
user’s choice. It allows the user to select from three options: adding
students, displaying student details, or exiting the program. Based on the
Vishal Chakradhari /24ADV3CST1183 Page | 45
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

user's input (choice), the corresponding case block will be executed.

6. What is the significance of the do-while loop in this program?

• Answer: The do-while loop ensures that the menu is displayed at least
once, and it keeps running until the user chooses option 3 (Exit). The loop
continues to prompt for actions, providing the user with multiple
opportunities to perform actions like adding students or displaying details.

7. How would the program behave if the user enters an invalid number of
students (e.g., a negative number)?

• Answer: If the user enters a negative or zero value for numStudents, the
program will print "Invalid number of students!" and terminate immediately
due to the return statement, as the program cannot continue with invalid
input.

8. What is the role of the Student structure in this program?

• Answer: The Student structure is used to store the data related to each
student, such as their name, age, and grade. It acts as a blueprint to
define the attributes of each student, allowing the program to manage and
organize the student data efficiently.

9. How is memory managed in this program?

• Answer: The program does not use dynamic memory allocation (i.e., it
does not use new or delete). Instead, it uses a statically defined array of
Student objects, which means the memory for storing student data is pre-
allocated when the program runs. The memory size is fixed at 100
students in this case.

10. What would happen if the number of students entered exceeds 100?

• Answer: If the number of students entered exceeds 100, the program will
attempt to access memory outside of the array's bounds, which may cause
undefined behavior, memory corruption, or a crash. The program should
ideally handle this scenario, but in this version, it assumes that the user
won't exceed the fixed array size of 100.

Q What is a structure in C++?


A structure is a user-defined data type in C++ that groups related variables of
different types into a single entity.

struct Person {
string name;
int age;
float height;
Vishal Chakradhari /24ADV3CST1183 Page | 46
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

};

Q How do you declare a structure in C++?


Use the struct keyword followed by the structure name and a set of curly braces
containing member variables.

Q How do you access structure members?


Use the dot operator (.) for direct access and the arrow operator (->) for pointer
access.
Person p1;
p1.name = "Bob";

Person *pPtr = &p1;


pPtr->age = 30;

Vishal Chakradhari /24ADV3CST1183 Page | 47


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[13]
Experiments with Results and Viva-Voice Questions
Experiment
13
Number
WAP to check given string is a palindrome or not without
Experiment Title
using predefine function of string header file
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment A palindrome is a word, phrase, number, or other


Description sequence of characters that reads the same forward and
backward, ignoring spaces, punctuation, and
capitalization.

Examples of Palindromes:

Words: madam, level, racecar

Phrases (ignoring spaces and punctuation): A man, a plan,


a canal, Panama!, Was it a car or a cat I saw?

Numbers: 121, 12321

The key characteristic of a palindrome is that the


sequence of characters is identical when reversed.

• Include Necessary Header Files:

#include <string.h>:

Includes the string library that provides functions such as

Vishal Chakradhari /24ADV3CST1183 Page | 48


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

strcpy(),

strrev(), and

strcmp() to manipulate and compare strings.

• Declare Variables two string variable :


• Input the A String:
• Copy same input to other using strcpy function
• Reverse copied string using strrev function
• Compare reverse string with given string if both are same
given string is palindrome

Experiment #include <iostream.h>


Code/ Procedure #include <conio.h>
#include <string.h> // For string function

int main()
{
char str[100], str1[100];
int k;
clrscr();
cout << "Enter a string: ";
cin >> str;

strcpy(str1,str);

strrev(str1);

if (strcmp(str, str1) == 0)
{
cout << "The string is a palindrome." << endl;
}
else
{
Vishal Chakradhari /24ADV3CST1183 Page | 49
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

cout << "The string is not a palindrome." << endl;


}

getch();
}

Input
Enter a string: madam

Output/Result

Q What is the purpose of the #include <iostream.h> header file?

Answer: It includes the input/output stream functions like cin, cout, endl, and other
standard I/O operations.

Q What is the role of #include <conio.h> in this code?

Answer: It includes the console input/output functions like clrscr() (to clear the
screen) and getch() (to wait for user input before closing the program).

Q What is the purpose of the clrscr() function?

Answer: The clrscr() function clears the console screen (only available in older
compilers like Turbo C++).

Q What does cin >> str; do in this program?

Answer: It takes input from the user and stores the string in the character array str.

Q Explain the function of strcpy(str1, str);.

Answer: The strcpy() function copies the content of the string str into str1.

Q What does strrev(str1); do in the program?

Answer: The strrev() function reverses the string stored in str1.

Q What is the purpose of strcmp(str, str1) in this code?

Vishal Chakradhari /24ADV3CST1183 Page | 50


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Answer: The strcmp() function compares two strings (str and str1). It returns 0 if the
strings are identical, indicating that the string is a palindrome.

Q What is the significance of if (strcmp(str, str1) == 0)?

Answer: This condition checks if the original string str is the same as the reversed
string str1. If true, it confirms that the string is a palindrome.

Q What would be the output if the input string is "hello"?

Answer: The output will be "The string is not a palindrome." because "hello" is not
the same when reversed.

Q Why does the program use getch() at the end?

Answer: The getch() function waits for the user to press any key before closing the
program window. It is used to pause the execution in console-based applications.

Vishal Chakradhari /24ADV3CST1183 Page | 51


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[14]
Experiments with Results and Viva-Voice Questions
Experiment
14
Number
Write a program that calculates the average of a list of
Experiment Title
numbers using functions.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment Function calculateAverage:


Description
• This function takes an array arr[] and its size size as
parameters.
• It calculates the sum of the elements of the array by
iterating through each element and adding it to a sum
variable.
• The average is then calculated by dividing the sum by the
number of elements and returned as a float.

In the main function:

• The user is prompted to enter the number of elements.


• The program then takes the list of numbers as input and
stores them in the array numbers[].
• The calculateAverage function is called with the array
and its size as arguments.
• The result (average) is displayed.

Vishal Chakradhari /24ADV3CST1183 Page | 52


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include <iostream.h>


Code/ Procedure #include <conio.h>

float calculateAverage(int arr[], int size) {


int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
return float(sum) / size;
}

void main() {
clrscr();

int n;
cout << "Enter the number of elements: ";
cin >> n;

int numbers[100];

cout << "Enter " << n << " numbers: ";


for (int i = 0; i < n; i++) {
cin >> numbers[i];
}

float avg = calculateAverage(numbers, n);

cout << "The average of the entered numbers is: " << avg <<
endl;

getch();
}

Input Enter the number of elements: 5


Enter 5 numbers: 10 20 30 40 50

Vishal Chakradhari /24ADV3CST1183 Page | 53


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Output/Result

Sample viva 1. What is a function in C++?


question • A function is a block of code that performs a specific task. It allows code to
be reused and organized.

2. What is the difference between call by value and call by reference?

• Call by value: The actual value of the argument is passed to the function,
so changes made inside the function do not affect the original variable.
• Call by reference: A reference (memory address) to the original variable
is passed to the function, so changes made inside the function affect the
original variable.

3. What does the function calculateAverage do in the program?

• The calculateAverage function calculates the average of a list of numbers.


It takes an array of numbers and its size as input, calculates the sum of the
numbers, and then divides it by the size of the array to find the average.

4. What is the purpose of the float return type in the calculateAverage


function?

• The return type is float because the average could be a decimal number,
and using float allows the program to return decimal values.

3. What is the significance of the int arr[] and int size parameters in the
calculateAverage function?

• int arr[] is an array that holds the numbers entered by the user, and int size
is the number of elements in the array. These two parameters allow the
function to process the correct number of elements to calculate the
average.

Q What is a function in C++?

A function is a block of code that performs a specific task and can be reused
multiple times.

Q What is the syntax of a function in C++?

returnType functionName(parameters) {

Vishal Chakradhari /24ADV3CST1183 Page | 54


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

// body of the function

} What are the types of functions in C++?

Q Built-in functions: Provided by the C++ Standard Library (e.g., sqrt, abs).

User-defined functions: Created by the programmer to perform specific tasks.

Q What is a return type in a function?

The return type specifies the type of value the function returns (e.g., int, float, void).

Q What is the difference between void and non-void functions?

A void function does not return a value.

A non-void function returns a value of the specified type.

Q How do you call a function in C++?

By writing the function name followed by parentheses with arguments (if required).
Example:

int result = add(5, 10);

Q What is the difference between a function declaration and definition?

Declaration: Provides the function’s name, return type, and parameters without the
body.

int add(int, int);

Definition: Provides the complete function body.

int add(int a, int b) {

return a + b;

Vishal Chakradhari /24ADV3CST1183 Page | 55


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

[15]
Experiments with Results and Viva-Voice Questions
Experiment
15
Number
Write a program that uses functions to perform mathematical
Experiment Title
operations and demonstrate the concepts of call by value, call
by reference, and recursive functions.
Demonstrate an understanding of computer systems and their
Course Outcome
components, including memory, processor, I/O devices,
(CO)
storage, and operating systems
Computer System with 2GB RAM and Hard Disk
Hardware
storage
Tools/Apparatus
Required
Software Turbo C/C++ OR AN OTHER COMPILER

Experiment 1. Call by Value:


Description • In this method, when a function is called, the
values of the arguments are copied into the
function's parameters. Any changes made to the
parameters inside the function do not affect the
original arguments outside the function.
• Example: Adding two numbers.
2. Call by Reference:
• In this method, instead of passing copies of the
arguments, the memory addresses (references) of
the arguments are passed. Changes made to the
parameters inside the function directly affect the
original arguments.
• Example: Swapping two numbers.
3. Recursion:
• A function is said to be recursive if it calls itself to
solve a smaller instance of the problem until a base
case is met.
• Example: Calculating the factorial of a number.

Vishal Chakradhari /24ADV3CST1183 Page | 56


IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

Experiment #include <iostream.h>


Code/ Procedure #include <conio.h>

int addByValue(int a, int b) {


return a + b;
}

void subtractByReference(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1);
}
}

void main() {
clrscr();

int num1, num2;

cout << "Enter two numbers for addition (Call by value): ";
cin >> num1 >> num2;

cout << "Sum of " << num1 << " and " << num2 << " is: " <<
addByValue(num1, num2) << endl;

cout << "Enter two numbers for swapping (Call by reference): ";
cin >> num1 >> num2;

cout << "Before swapping: num1 = " << num1 << ", num2 =
Vishal Chakradhari /24ADV3CST1183 Page | 57
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

" << num2 << endl;

subtractByReference(num1, num2);

cout << "After swapping: num1 = " << num1 << ", num2 = "
<< num2 << endl;

int num;
cout << "Enter a number to calculate its factorial (Recursive
function): ";

cin >> num;

cout << "Factorial of " << num << " is: " << factorial(num)
<< endl;

getch();
}

Input Enter two numbers for addition (Call by value): 5 3


Enter two numbers for swapping (Call by reference): 10 20
Enter a number to calculate its factorial (Recursive function): 5

Output/Result

Sample viva 1. What is call by value?


• Call by value is a parameter passing mechanism where the
question function receives a copy of the actual argument. Any modification
to the parameter inside the function does not affect the original
variable.
2. What is call by reference?
• Call by reference is a parameter passing mechanism where the
function receives the address (reference) of the actual argument.
Any changes made to the parameter inside the function directly
affect the original argument.
3. What is recursion in programming?
• Recursion is a technique in programming where a function calls
itself to solve a smaller instance of the same problem. The
Vishal Chakradhari /24ADV3CST1183 Page | 58
IAC-Core, SAGE University Indore B.Tech . - CST I-Sem

function continues to call itself until it reaches a base case.


4. Why are & symbols used in the swap() function?
• The & symbol is used to pass arguments by reference. This
means that the function can modify the original variables passed
to it, rather than working with copies of those variables.
5. What will happen if you enter a negative number for factorial
calculation?
• The given factorial function does not handle negative numbers. It
is designed to work for positive integers, and it will produce
incorrect results if a negative number is entered. A check can be
added to handle this case.

Vishal Chakradhari /24ADV3CST1183 Page | 59

You might also like