PC File
PC File
B.Tech Programme:
VISION OF INSTITUTE
MISSION OF INSTITUTE
INDEX
EXERCISE - 1
Theory
Step-by-step explanation of how a typical C program
calculates the factorial of a given number:
Algorithm
Step-1 : Start.
Step-2 : Read n.
Step-3 : Initialize counter variable i to 1 and fact to 1.
Step-4 : if i <= n go to step 5 otherwise go to step 7.
Step-5 : calculate fact = fact * i.
Step-6 : increment counter variable i and go to step 4.
Step-7 : Write fact.
Step-8 : Stop.
Flowchart
Source Code
Output
Learning Outcomes
Exercise - 2
Theory
Algorithm
1. Start
2. Declare the variables.
3. Initialize the first term, the total number of terms, and
the common ratio.
4. Use a for loop that will calculate the sum.
5. Declare two variables for sum and element.
6. Update both the elements in each iteration
7. At the end display the calculated sum.
8. Stop
Flowchart
Source Code
Output
Learning Outcomes
1. Understanding how to calculate the sum of GP series
using formula and for loop in C.
2. Understanding using different libraries such as math.h
and performing arithmetic operations.
Problem – 2 : Sum of GP Series using
For Loop
Theory
In this program, we will find the sum of a geometric series
using a for loop.
Firstly, the first term, the total number of terms, and the
common ratio are declared. Then, we calculate the total sum
of the geometric series using the formula and print it using
the for loop.
ALGORITHM:
1. Start
2. Declare the variables.
3. Initialize the first term, the total number
of terms, and the common ratio. 4. Use a for
loop that will calculate the sum.
5. Declare the formula for sum and last term before the
loop.
6. Calculate the sum till the last element in the for
loop.
7. Display the sum.
8. Stop.
Flowchart:
Source Code:
Output
Learning Outcomes
1. Understanding how to calculate sum of GP series using
just a for loop and through basic arithmetic
operations.
Algorithm
1. Start
2. Declare the variables.
3. Initialize the first term, the total number of terms, and
the common ratio.
4. Call the function that will calculate the sum.
5. Declare the formula for sum and last term in the function.
6. Calculate the sum till the last element.
7. Display the sum.
8. Stop.
Flowchart
Source Code
Output:
Learning Outcomes
1. Understanding how to calculate sum of GP series using
a function instead of performing it in main.
2. Understanding functions, how to call a function and
function parameters.
Algorithm
1. Step 1 − Move n-1 disks from source to aux
2. Step 2 − Move nth disk from source to dest
3. Step 3 − Move n-1 disks from aux to dest
4. A recursive algorithm for Tower of Hanoi can be
driven as follows −
5. START
6. Procedure Hanoi(disk, source, dest, aux)
7. IF disk == 1, THEN
8. move disk from source to dest
9. ELSE
10. Hanoi(disk - 1, source, aux, dest)
11. move disk from source to dest
Flowchart
Source Code
Output
Learning Outcomes
1. Understanding Recursion, and how to solve the tower
of Hanoi problem using Recursion.
2. Understanding Base Cases in Recursion.
3. Learning how to perform Recursion through the tower
of Hanoi problem.
Exercise – 3 : a recursive program to
print the first m Fibonacci number.
Theory
The Fibonacci sequence is a sequence where the next term is
the sum of the previous two terms. The first two terms of
the Fibonacci sequence are 0 followed by 1. The Fibonacci
sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21
We can use recursion to solve this problem because any
Fibonacci number n depends on previous two Fibonacci
numbers. Therefore, this approach repeatedly breaks down
the problem until it reaches the base cases.
In mathematical terms, the number at the mth position can
be represented by:
Fn = Fn-1 + Fn-2
where, F0 = 0 and F1 = 1.
For example, Fibonacci series 10 terms are: 0, 1, 1, 2, 3,
5, 8, 13, 21, 34.
Algorithm
1. START.
2. Declare variable n
3. Initialize variables:
4. Read n.
5. The recursive function is as follows:
6. If n < = 1:
7. Return n
8. Else:
9. Return integer nthFibonacci(n-1) + nthFibonacci(n-2)
10. Call the function nthFibonacci(n) in main and output
the result
11. Step 6: STOP.
Flowchart
Source Code
Output
Learning Outcomes
1. Understanding how to calculate a Fibonacci number
mathematically and then calculate it using C.
2. Understanding how to calculate a Fibonacci number
using Recursion.
• Ask the user for the number of rows (a) and columns
(b).
• Take input for elements and store them in
matrix1[a][b].
3. Input Matrix 2:
• Ask the user for the number of rows (c) and columns
(d).
• Take input for elements and store them in
matrix2[c][d].
4. Display Menu:
• Show options for matrix operations:
• Addition
• Subtraction
• Upper & Lower Triangular Matrix
• Transpose
• Multiplication
• Exit
5. Perform Addition (if chosen):
• Check if matrices have the same dimensions (a == c
and b == d).
• Subtract corresponding elements and display the
result.
6. Print Upper & Lower Triangular Matrix (if chosen):
• Display the upper triangular matrix (set elements
below the diagonal to 0).
• Display the lower triangular matrix (set elements
above the diagonal to 0).
7. Transpose Matrix (if chosen):
• Swap rows and columns (matrix1[j][i]).
• Display the transposed matrix
8. Multiply Matrices (if chosen):
• Check if multiplication is possible (b == c).
Flowchart
Source Code
Output
Learning Outcomes
1. Understanding Matrix Operations learn how to perform
fundamental matrix operations like addition,
subtraction, multiplication, transpose, and
identifying triangular matrices.
2. Dynamic Input Handling understand how to take user
input for variable-sized matrices and store them in a
2D array.
3. Conditional Statements & Error Handling learn to
validate operations by checking matrix dimensions
before performing addition, subtraction, or
multiplication.
4. Problem-Solving Skills breaking down complex matrix
operations into step-by-step algorithms.
5. Memory and Performance Considerations understanding
static array allocation in C.
• Initialize i to 0.
• Loop until the end of the string s1 (when s1[i]
== '\0'):
o Copy each character of s1[i] to s2[i].
o Increment i.
• After the loop, set s2[i] = '\0' to null-
terminate the copied string.
• Print the copied string s2.
• Initialize l to 0.
• Loop through s1 and increment l until the null
character \0 is encountered (end of the string).
• Print the calculated length l.
7. End.
Flowchart
Source Code
Output
Learning Outcomes
1. Understanding how to find the length of a
string without using strlen().
2. Understanding how to concatenate two
strings in C without using strcat()
function.
3. Understanding how to Reverse a string in C
without using strrev() function.
4. Understanding how to copy one string to
another string in C without using strcpy()
function.