C Programming • Introduction to Computer, Programming & algorithms
C Programming / Algorithm — Exercises

Algorithm — Exercises

Practical 1 Introduction to Computer, Programming & algorithms

Write an algorithm to add two numbers.

Practical / Solution

Algorithm — Exercise 1

Problem

Write an algorithm to add two numbers.

Algorithm

  1. Start
  2. Input A and B
  3. Calculate SUM = A + B
  4. Display SUM
  5. Stop

Explanation

The algorithm takes two numbers as input, adds them, and displays the sum.

Algorithm — Exercise 2

Problem

Write an algorithm to find the largest of two numbers.

Algorithm

  1. Start
  2. Input A and B
  3. Compare A and B
  4. If A is greater than B, display A
  5. Otherwise, display B
  6. Stop

Explanation

The algorithm compares the two input values and displays the greater number.

Algorithm — Exercise 3

Problem

Write an algorithm to find the area of a rectangle.

Algorithm

  1. Start
  2. Input length and width
  3. Calculate AREA = length × width
  4. Display AREA
  5. Stop

Explanation

The algorithm accepts the length and width of a rectangle, calculates its area, and displays the result.

Algorithm — Exercise 4

Problem

Write an algorithm to calculate the simple interest.

Algorithm

  1. Start
  2. Input Principal, Rate and Time
  3. Calculate SI = (Principal × Rate × Time) / 100
  4. Display SI
  5. Stop

Explanation

The algorithm takes the principal amount, rate of interest, and time as input and applies the simple interest formula.

Algorithm — Exercise 5

Problem

Write an algorithm to check whether a number is even or odd.

Algorithm

  1. Start
  2. Input N
  3. Calculate N % 2
  4. If the remainder is 0, display "Even"
  5. Otherwise, display "Odd"
  6. Stop

Explanation

The algorithm uses the modulus operator to find the remainder after dividing the number by 2. A remainder of 0 means the number is even; otherwise it is odd.

Algorithm — Exercise 6

Problem

Write an algorithm to check whether a number is positive, negative, or zero.

Algorithm

  1. Start
  2. Input N
  3. If N is greater than 0, display "Positive"
  4. Else if N is less than 0, display "Negative"
  5. Otherwise, display "Zero"
  6. Stop

Explanation

The algorithm checks the value of N against zero and identifies whether the number is positive, negative, or zero.

Algorithm — Exercise 7

Problem

Write an algorithm to find the largest of three numbers.

Algorithm

  1. Start
  2. Input A, B and C
  3. Compare A, B and C
  4. If A is greater than or equal to B and C, display A
  5. Else if B is greater than or equal to A and C, display B
  6. Otherwise, display C
  7. Stop

Explanation

The algorithm compares all three input values and identifies the greatest number.

Algorithm — Exercise 8

Problem

Write an algorithm to calculate the factorial of a number.

Algorithm

  1. Start
  2. Input N
  3. Set FACT = 1
  4. Repeat from 1 to N and multiply FACT by each number
  5. Display FACT
  6. Stop

Explanation

The algorithm initializes the factorial value to 1 and repeatedly multiplies it by each number from 1 to N.

Algorithm — Exercise 9

Problem

Write an algorithm to reverse a given number.

Algorithm

  1. Start
  2. Input N
  3. Set REV = 0
  4. Extract the last digit of N
  5. Add the digit to REV
  6. Remove the last digit from N
  7. Repeat until N becomes 0
  8. Display REV
  9. Stop

Explanation

The algorithm repeatedly extracts the last digit of the number and builds the reversed number using those digits.

Algorithm — Exercise 10

Problem

Write an algorithm to find the sum of the first N natural numbers.

Algorithm

  1. Start
  2. Input N
  3. Set SUM = 0
  4. Repeat from 1 to N and add each number to SUM
  5. Display SUM
  6. Stop

Explanation

The algorithm starts with a sum of zero and adds each natural number from 1 to N. The final value is displayed as the result.