0% found this document useful (0 votes)
46 views28 pages

CPG Practical File

diuhigherhhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views28 pages

CPG Practical File

diuhigherhhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

CPG

PRAC
TICAL
FILE
SHREYA SINGH
2023/BAE/143
SECTION-B
Practical 1- ALGORITHMS AND PSEUDOCODES

Algorithms are step-by-step procedures or sets of instructions for


solving a specific problem or performing a specific task. They are
fundamental to the field of computer science and are used in various
aspects of computing, mathematics, and other disciplines.
Algorithms provide a systematic way to solve problems, and they can
be expressed in various forms, including natural language,
pseudocode, flowcharts, and computer code

Algorithm has the following characteristics


 Input: An algorithm takes input.
 Output: Each algorithm is expected to produce at least one
result Algorithm & Flowchart.
 Definiteness: Each instruction must be clear and unambiguous.
 Finiteness: If the instructions of an algorithm are executed, the
algorithm should terminate after finite number of steps.
 Feasibility: Algorithms should be practical and capable of being
executed by humans or machines within a reasonable amount
of time and resources.
 Problem-Specific: Algorithms are often designed for specific
types of problems. Efficiency: One important aspect of
algorithms is their efficiency in terms of time and space
complexity. Efficient algorithms are designed to minimize the
time and resources required to solve a problem.

Pseudocode is a high-level description of an algorithm or a program


written in a simplified, human-readable format. It is not a formal
programming language but rather a way to outline the logic of an
algorithm or program using a mix of natural language and simple,
programming-like constructs. Pseudocode is often used during the
early stages of software development to plan and design algorithms
before they are implemented in a specific programming language.

The key characteristics of pseudocode include:


 Readability: Pseudocode is designed to be easily understood by
both technical and non-technical individuals, making it a useful
communication tool among team members.
 Abstraction: It focuses on the logic and the high-level structure
of the algorithm, avoiding specific syntax or programming
language conventions.
 Flexibility: Pseudocode is not bound by the constraints of a
particular programming language, allowing developers to
express their ideas more freely.
 Clarity: Pseudocode should be clear and unambiguous, making
it suitable for documenting and sharing algorithms with others.

Questions:
Q1. Write an algorithm to determine the maximum of two
numbers.
Ans.
1. Start
2. Input the first number (A)
3. Input the second number (B)
4. If A is greater than B
4.1 Set max number as A
Else
4.2 Set max number as B
5. Display max number as the maximum of the two numbers
6. Print
Q2. Write an algorithm to determine the average of 3 numbers.
Ans.
1. Start
2. Input the first number (X)
3. Input the second number (Y)
4. Input the third number (Z)
5. Add X , Y and Z to get the SUM
6. Divide the sum by 3 to calculate the average
6.1 Set average as SUM divided by 3
7. Display the average as the result
8. End

Practical 2 - Sequence structure

A sequence structure is a fundamental programming construct that


defines the order in which a series of instructions or statements are
executed within a program. It represents a linear or sequential flow
of control where one operation or statement follows another in a
predetermined order. Each statement in the sequence structure is
executed one after the other, from the beginning of the sequence to
the end.
In this structure, the execution of each statement is contingent on
the successful completion of the preceding statement. This means
that the program proceeds from one statement to the next without
branching or making decisions based on conditions. The sequence
structure is often used for implementing straightforward, step-by-
step processes where the order of execution is crucial.
FLOWCHARTS
Flowchart is a graphical representation of an algorithm.
Programmers often use it as a program-planning tool to solve a
problem. It makes use of symbols which are connected among them
to indicate the flow of information and processing. The process of
drawing a flowchart for an algorithm is known as “flowcharting”.

QUESTIONS
Q3. Write an algorithm and draw a flowchart to convert length
in feet to length in centimetres.
Ans.
1. Start
2. Input the length in feet
3. Multiply the length in feet by 30.48 ( 1 foot = 30.48 cm)
3.1 Set length in cm as length in feet * 30.48
4. Display the length in cm
5. End
Q4. Write an algorithm and draw a flowchart that will read the
two sides of a rectangle and calculate its area.
Ans.
1. Start
2. Input the length of one side of the rectangle (A)
3. Input the length of the other side of the rectangle (B)
4. Calculate the area of the rectangle
4.1 Set area as side1 multiplied by side2
5. Display the calculated area as the result
6. End
START

INPUTSIDES
(A and B)

AREA=A*B

PRINT
AREA

STOP

Practical 3 – Decision structure

A decision structure, often referred to as a control structure or


conditional structure, is a fundamental concept in computer
programming and algorithm design. It allows a program to make
decisions and choose different paths or actions based on certain
conditions or criteria. Decision structures are used to control the
flow of a program, enabling it to react to different situations and
make choices accordingly.
Decision structures are essential for creating dynamic, responsive
programs that can adapt to different situations. They are used to
implement branching logic, allowing your program to perform
different actions based on user input, data conditions, or other
factors.

QUESTIONS
Q5. Write an algorithm and draw a flowchart that will calculate
the roots of a quadratic equation.
Ans.
1. Start
2. Input the coefficients a, b, and c of the quadratic equation ax^2 +
bx + c = 0
3. Calculate the discriminant (D) using the formula: D = b^2 - 4ac
4. If D < 0, display "No real roots exist."
5. If D = 0, calculate the single root using the formula: root = -b / (2a)
6. If D > 0, calculate two distinct roots using the formulas:
root1 = (-b + √D) / (2a)
root2 = (-b - √D) / (2a)
7. Display the roots or root obtained in steps 5 and 6.
8. End
Q6. Write an algorithm and draw a flowchart to calculate the
marks of a student in 4 subjects and assign Pass if the average
marks are greater than 50, else fail.
Ans.
ALGORITHM
1. Start
2. Initialize variables SUB1, SUB2, SUB3, SUB4, total marks,
average marks.
3. Read input for SUB1, SUB2, SUB3, SUB4.
4. Calculate total marks = SUB1 + SUB2 + SUB3 + SUB4.
5. Calculate average marks = total marks / 4.
6. If average marks > 50, then
7. Display "Pass"
Else
Display "Fail"
8. End
Q7. Write an algorithm and draw a flowchart that reads two
values, determines the largest value and prints the largest value
with an identifying message.
Ans.
1. Start
2. Input value1
3. Input value2
4. If value1 > value2
5. Print "The largest value is value1 "
6. Else
7. Print "The largest value is value2"
8. Stop
Q8. Write an algorithm and draw a flowchart to find the greatest
of the three numbers.
Ans.
1. Start
2. Input three numbers: num1, num2, num3
3. If num1 is greater than num2 and num1 is greater than num3,
then,
 Display num1 as the greatest number
4. Else, if num2 is greater than num1 and num2 is greater than
num3, then
 Display num2 as the greatest number
5. Else,
 Display num3 as the greatest number
6. End
Q9. Write an algorithm and draw a flowchart to find out
whether the number entered by user is even or odd.
Ans.
1. Start
2. Input a number: num
3. If num modulo 2 equals 0, then
 Display "The number is even"
4. Else,
 Display "The number is odd"
5. End

START

ENTER A
NUMBER

IS NUM
%2==0

PRINT ODD
PRINT EVEN

STOP
Q10. Write the program to take two numbers as input from the
user and provide a menu for calculations on those two
numbers
as per the operation selected by the user.
Ans.
1.Prompt the user to enter two numbers.
2.Read and store the two numbers in separate variables, let's
say num1 and num2.
3.Display a menu with options for different operations, such as
addition, subtraction, multiplication, and division.
4.Ask the user to choose an operation by entering a
corresponding number or character.
5.Based on the user's choice, perform the selected operation
and display the result.
6.Repeat the process until the user chooses to exit the
program.

Q11. Write an algorithm and draw a flowchart to print n natural


numbers where n is taken as input from the user.
Ans.
1. Take a natural number n as input.
2. Set another variable i=1
3. Iterate I from 1 to n
4. Print value if i in each iteration.
Q12. Write an algorithm and draw a flowchart to print n natural
numbers where n is taken as input from the user in reverse
order.
Ans.
1. Start
2. Input a number 'n' as the limit for natural numbers
3. Set a variable 'count' to 'n'
4. Repeat the following steps until 'count' is greater than zero:
 Display the value of 'count'
 Decrement 'count' by 1
5. End
START

INPUT n
Q5. Write an algorithm and draw a flowchart to compare the
sum of first n natural numbers where n is taken as input from the
NO
user. YES
IS

Ans. n==0

1. Start END
2. Input a number 'n' as the limit for natural numbers
Initiative
3. Calculate the sum of the first 'n' natural numbers (sum1) using
I=n
the formula: sum1 = (n * (n + 1)) / 2
4. Calculate the sum of the next 'n' natural numbers (sum2) using
the formula: sum2 = ((n * (n + 1)) / 2) - (n * (n - 1)) / 2
5. IfIS sum1 is greater than sum2, then
 Display
i>/1? "The sum of first 'n' natural numbers is greater"
6. Else if sum1 is less than sum2, then
 Display "The sum of next 'n' natural numbers is greater"
PRINT
7. Else,
 Display "Both sums are equal"
8. End
I=i-1 START

INPUT N

SUM=0

I=i=1

I<=n
?

SUM=SUM+i

I=i+1

Print sum

STOP
Q14. Write an algorithm and draw a flowchart to print sum of
even numbers between 1 to n where n is taken as input from
the user.
Ans.
Step 1: Start
Step 2: Read the limit of numbers, n
Step 3: Assign i=1
Step 4: Assign sum=0
Step 5: Repeat steps 6,7&8 until i=n reaches
Step 6: If i%2==0 go to step 7
Step 7: Compute sum= sum+i
Step 8: Compute i=i+1
Step 9: Print sum of even numbers, sum
Step 10: Stop.
Q15. Write an algorithm and draw a flowchart to print the
series 1,2, 4, 8, 16, 32, 64.
Ans.
1. Start
2. Set a variable 'num' to 1
3. Repeat the following steps six times:
 Display the value of 'num'

 Multiply 'num' by 2

4. End
Practical 4 – Case structure

In computer programming, a "case structure" typically refers to a


control structure used to implement conditional branching based on
a specific value or expression. It allows you to execute different
blocks of code or actions depending on the value of the given
expression. The specific implementation of a case structure can vary
depending on the programming language, but it's often called a
"switch statement" or "case statement."
Case structures are particularly useful when you need to perform
different actions based on the value of a single variable or
expression, making your code more readable and efficient compared
to using multiple if and else if statements.

Practical 5 – Repetition structure


A repetition structure, also known as a loop or iteration structure, is
a fundamental concept in computer programming that allows you to
execute a block of code multiple times. It is used when you need to
perform a specific task repeatedly, whether it's iterating through a
collection of data, performing calculations, or executing a sequence
of steps until a certain condition is met.
Repetition structures are essential for automating tasks that involve
repeated or iterative actions. They help reduce code duplication and
make programs more efficient and adaptable. Repetition structures
are commonly used in scenarios like iterating through arrays,
processing large datasets, implementing game loops, and carrying
out iterative calculations. They play a critical role in controlling the
flow of a program and achieving specific tasks efficiently.
EXCEL FUNCTIONS
1) IF FUNCTION
The "IF" function in Excel is a logical function
that allows you to perform different actions or
return different values based on whether a
specified condition is true or false. It is often
used for decision-making and conditional
calculations in Excel formulas.

2)MEDIAN FUNCTION
The MEDIAN function in Excel is used to find the middle
value in a set of numbers. It's a statistical function that
can be helpful for determining the central or typical
value in a data set. =MEDIAN(number1, [number2], ...)
3)MAX Function:
The "MAX" function returns the largest value from a
range of numbers.

MIN Function:
The "MIN" function, on the other hand, returns the
smallest value from a range of numbers.
4)STEP DEVIATION METHOD
In statistics, the concept of "step deviation" or "step
data" typically refers to data that has been adjusted by
subtracting a constant value from each data point. This
adjustment is often used when you want to perform
calculations or create a new dataset with a more
manageable range of values, which can be especially
useful when working with data that has a wide range or
large numerical values.

5)SUM IFS
The SUMIFS function in Excel allows you to sum values
based on multiple criteria or conditions. It's useful when
you want to calculate the sum of values that meet
specific conditions simultaneously

You might also like