C Program Codes
C Program Codes
1. Start
2. Declare variables a, b, c, discri, root1, root2 as integers
3. Read a, b, and c
4. Calculate discri = b * b - 4 * a * c
5. If discri > 0:
Calculate root1 = (-b + sqrt(discri)) / (2 * a)
Calculate root2 = (-b - sqrt(discri)) / (2 * a)
print root1 and root2
6. Else if discri == 0:
Calculate root1 = root2 = -b / (2 * a)
print root1 and root2
7. Else, print "Roots are imaginary"
8. End if
9. Stop
Algorithm to find the sum and reverse of a number
1. Start
2. Declare variables num, sum, rev, and rem as integer
3. Read num
4. Initialize sum and reverse to 0
5. While num is not 0:
Set rem to num % 10
Add rem to sum
Multiply rev by 10 and add rem
Divide num by 10 (use integer division)
End loop
6. print sum and rev
7. Stop
Algorithm to find n’th Fibonacci number
1. Start
2. Declare variables a, b, c, i, and n as integer
3. Initialize a to 0 and b to 1
4. Read n
5. For i from 2 to n-1:
Set c to a + b
Set a to b
Set b to c
End loop
6. Output the value of c
7. Stop
Algorithm to calculate the sum of the series 1 + 𝑥 + 𝑥 2 + 𝑥 3 + ⋯ + 𝑥 𝑛
1. Start
2. Declare variables i, n, x, and sum as integer
3. Initialize sum to 0
4. Read x and n
5. For i from 1 to n, Add x^i to sum
8. Stop
Algorithm to find the number of positive , negatives, and 0s from the given number of arrays
1. Start
2. Declare variables positive, negative, zero, arr (array), i, and n as integer
3. Initialize positive, negative, and zero to 0
4. Read n
5. read n numbers into array arr
6. For each element in arr:
If the element is greater than 0, increment positive
If the element is less than 0, increment negative
If the element is 0, increment zero
End loop
7. Output the counts of positive, negative, and zero elements
8. Stop
Algorithm to print a pyramid of stars (*) based on user input
1. Start
2. Declare variables i, space, rows, and k as integer
3. Read rows
4. Initialize k to 0
5. for i from 1 to rows:
for space from 1 to rows – I, Print space
While k is not equal to 2 * i – 1, Print *, Increment k
Print a newline character
End loop
6. Stop
Algorithm to print Armstrong numbers in a range given from the user
1. Start
2. Declare variables l, u, i, t, c, rem, sum as integer
3. Initialise sum to 0
4. Read the lower and upper range and store it in l and u.
5. For I from l to u:
Set t to i
Set sum to 0
While t is not equal to 0:
Calculate rem = t%10
calculate sum = sum + (rem * rem * rem)
calculate t = t/10
End loop
If sum = I, Print i
End loop
6. Stop
Algorithm to convert the given number from the user to the pattern:
Xxxxx
Xxxx
Xxx
Xx
1. Start
2. Declare variables n, t, count, val as integer
3. Initialize count to 0
4. Read n
5. print “pattern” new line
6. Set t to n
7. While t is less than 0:
Calculate t=t/10
Increment count by 1
End loop
8. Calculate val = 10^count
9. While n not equal to 0:
Print new line
Print n%t
Calculate val = val / 10
End loop
10. Stop
Algorithm to print leap years in the given range
1. Start
2. Declare variables y, l, u as integer
3. Read l and u
4. For y from l to u:
If (y is divisible by 4) and ((y is divisible by 400) or (y is not divisible by 100)), Print y
End loop
5. Stop
Algorithm to convert the given decimal to another base
1. Start
2. Declare variables b, n, i, r, digit, p, count as integer
3. Declare variable a (array with the size 100) as character
4. Initialise count to 0
5. Read n
6. Read b (the base to be converted to)
7. Set p to n
8. Do while p is not equal to 0:
Set r to p % b
Set digit to ‘0’+r
If digit grater than 9, Set digit to digit + 7
Set a[count] to digit
Increment count by 1
Set p to p / b
End loop
9. Print base equivalent of ‘b’ is ‘n’
10. For I from count – 1 to 0, print variable a[i]
11. Stop
Algorithm to find Standard deviation of n numbers
1. Start
2. Read variables i, n as integers and std_dev, sum, sumsqr, mean, value, variance, a[100] as
floating point
3. Initialise sum = 0, sumsqr = 0, variance = 0.0
4. Read n
5. Print enter numbers
6. For I from 0 to n-1:
Read array a [i]
Calculate sum = sum+ a[i]
End loop
7. Set mean to sum / n
8. For I from 0 to n-1:
Calculate value = a[i] – mean
Calculate sumsqr = sumsqr + (value * value)
End loop
9. Calculate variance = sumsqr / n
10. Calculate std_dev = sqrt(variance)
11. Output the mean of ‘n’ numbers = ‘mean’
12. Variants of ‘n’ numbers = ‘variants’
13. Standard deviation of ‘n’ numbers is ‘std_dev’
14. Stop
Algorithm to find Location of a given number in an array
1. Start
2. Declare variables arr[50], n, i, s, found as integers
3. Read n
4. Read into the variable ‘arr’ till ‘n-1’
5. Read s (the variable to search)
6. Set found to 0
7. For I from 0 to n-1:
If arr[i] is equal to s, set found to 1 and break
End loop
8. If found is equal to 1, print ‘s’ is found at position ‘i+1’
9. Else, print ‘s’ is not found in the array
10. Stop
Algorithm to append 2 arrays
1. Start
2. Declare arrays A[20], B[20], C[40] and variables a_len, b_len, i, j, k as integers
3. Read a_len and b_len
4. Print "Enter elements for array A:"
5. For i from 0 to a_len-1, Read A[i]
6. Print "Enter elements for array B:"
7. For j from 0 to b_len-1, Read B[j]
8. Set k to 0
9. For i from 0 to a_len-1:
Set C[k] = A[i]
Increment k
End loop
10. For j from 0 to b_len-1:
Set C[k] = B[j]
Increment k
End loop
11. Print "Array A before appending:"
12. For i from 0 to a_len-1, Print A[i]
13. Print "Array B before appending:"
14. For j from 0 to b_len-1, Print B[j]
15. Print "Arrays after appending:"
16. For k from 0 to (a_len + b_len - 1), Print C[k]
17. Stop
Algorithm to divide the given number into the denominations of Indian currency
1. Start
2. Define SIZE as 7
3. Declare variables amount, and notes as integers
4. Declare denominations as an integer array with the size of “SIZE” and initialise it with the
values 500, 200, 100, 50, 20, 10, and 1
5. Read amount
6. For I from 0 to ‘SIZE’-1:
Set ‘notes’ to ‘amount’ / ‘denominations[i]’
If ‘notes’ is not equal to 0:
Calculate amount = amount % denominations[i]
Print ‘notes’ * ‘denominations[i]’ = ‘notes * denominations[i]’
End if
End loop
7. Stop
Algorithm to find the transpose of a matrix
1. Start
2. Declare 2d arrays with the size 10, 10 a, and transpose as integers
3. Declare variables r, c, i, and j as integers
4. Read r (number of rows), and c (number of columns) of the matrix
5. Read the matrix from the user using loop and store it in array ‘a’
6. Print the entered matrix ‘a’
7. For I from 0 to ‘r’-1:
For j from 0 to ‘c’-1, set ‘transpose[j][i]’ to ‘a[i][j]’
end loop
8. print “the transpose of the entered matrix: “
9. print the array ‘transpose’
10. stop
algorithm to generate identity matrix from the given size of rows and columns
1. start
2. Declare a 2D array 'a' of size 10, 10
3. Declare variables i, j, m, n as integers
4. Read ‘m’ and ‘n’
5. For i from 0 to ‘m’-1:
For j from 0 to ‘n’-1:
If ‘I’ less than j, set ‘a’[i][j] to 1
Else if i > j, set ‘a’[i][j] to -1
Else, set ‘a’[i][j] to 0
End loop
6. Print "The matrix :"
7. Print the matrix ‘a’
8. Stop
Algorithm to multiply 2 metrices
1. Start
2. Declare 2D arrays a, b, and mul of size 10, 10 as integers
3. Declare variables r1, c1, r2, c2, i, j, k as integers
4. Print "enter the number of rows and columns of matrix 1: "
5. Read r1 and c1
6. Print "enter the number of rows and columns of matrix 2: "
7. Read r2 and c2
8. If ‘c1’ is not equal to ‘r1’, Print "Multiplication is not possible...."
9. Else
Print "enter the first matrix elements"
Read the first matrix and store it in ‘a’
Print "enter the second matrix elements"
Read second matrix and store it in ‘b’
Print "Matrix 1"
Print the matrix ‘a’
Print "Matrix 2"
Print the matrix ‘b’
For I from 0 to ‘r’-1:
For j from 0 to ‘c2’-1,
Set ‘mul[i][j]’ to 0
For k from 0 to ‘c1’-1, Set ‘mul[i][j]’ to ‘mul[i][j]’ + (‘a[i][k]’ * ‘b[k][j]’)
End loop
End loop
Print "Product matrix is:"
For i from 0 to ‘r1’-1:
For j from 0 to ‘c2’-1:
Print ‘mul[i][j]’ with tab space
End loop
Print newline
End loop
End if
10. Stop
Algorithm to find factorial using recursion
1. Start
2. Define function isPrime(num):
For i from 2 to ‘num’ / 2:
If ‘num’ % i equals 0, Return 0
Else, return 1
End loop
End function
3. Define function Primes(l, u)
Print "All prime numbers between ", l, " to ", u, " are: "
While l is less than or equal to u:
If isPrime(l), Print l
Increment i
End loop
End function
4. Start
5. Declare variables l and u as integers
6. Read l and u
7. Call Primes(l, u)
8. Stop
Algorithm to output short form of a given string
1. Start
2. Declare string str of size 80
3. Declare character c and string ss of size 10
4. Declare integers i, j, and in initialized to 0
5. Read str
6. For str[i] is not null character and c = str[i]:
7. If c is not a letter, Set in to 0
8. Else if in is 0:
9. Set in to 1
10. Set ss[j+1] to c
11. End if
12. Eng loop
7. Set ss[j] to null character
8. Print "Short form is ", ss
9. Stop
Algorithm to count the number of vowels in a string
1. Start
2. Declare variables c as integer and count as integer and initialized to 0
3. Declare string s of size 1000
4. Read s
5. While s[c] is not null character:
If s[c] is 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', or 'U', Increment count
Increment c
End loop
6. Print "Number of vowels in the string: ", count
7. Stop
Algorithm to store students first name and marks in structure and print them
1. Start
2. Define a structure student with fields Name (string of size 50), roll (integer), and mark (float)
3. Declare an array s of size 10 as structure student
4. Declare variables i and n as integers
5. Read n
6. For i from 0 to n-1:
Set s[i].roll to i + 1
Print "Enter first name: "
Read s[i].Name
Print "Enter marks: "
Read s[i].mark
End loop
7. Print "Displaying Information:"\
8. For i from 0 to n-1”
Print "Roll number: ", i + 1
Print "First name: ", s[i].Name
Print "Marks: ", s[i].mark
End loop
9. Stop
Algorithm to find the length of a string using pointer
1. Start
2. Declare string s of size 50
3. Declare variable I as integer
4. Read s
5. Set pointer p to point to the beginning of s
6. For i from 0 to end of string pointed by p (i.e., until *(p + i) is null character), Increment i
7. Print "Length = ", i
8. Stop
Algorithm to store odd and even numbers to 2 different files after taking numbers from user and
storing it into a file
1. Start
2. Declare file pointers fp, fp1, fp2
3. Declare variables c, and I as integers
4. Set fp to Open file "data.txt" in write mode ("w")
5. Print "Enter the numbers to be written in data.txt:"
6. For i from 0 to 9:
Read c from user
Write c to file fp
End loop
7. Close file fp
8. Set fp to Open file "data.txt" in read mode ("r")
9. Set fp1 to Open file "even.txt" in write mode ("w")
10. set fp2 to Open file "odd.txt" in write mode ("w")
11. While (c = Read a number from file fp using getw) is not EOF:
If c % 2 equals 0, Write c to file fp1 using putw
Else, Write c to file fp2 using putw
End loop
12. Close files fp, fp1, fp2
13. Set fp1 to Open file "even.txt" in read mode ("r")
14. Print "Contents of even.txt"
15. While (c = Read a number from file fp1 using getw) is not EOF, Print c with 4 spaces padding
16. Print newline
17. Set fp2 to Open file "odd.txt" in read mode ("r")
18. Print "Contents of odd.txt"
19. While (c = Read a number from file fp2 using getw) is not EOF, Print c with 4 spaces padding
20. Close files fp1, fp2
21. Stop