You are on page 1of 4

Algorithms

➢ Should be containing finite number of steps


➢ Every algorithm should lead to an end result
➢ They can be converted to any programming language
➢ They should be understandable by programmers

◼ Finding area of a circle by reading radius as the input from the user

Algorithm

1. Read radius r from the user


2. Compute area=3.14*r*r
3. Display area to the user

◼ Compute tax on income for a person .Tax is 10% of the income

Algorithm

1. Read income (inc) from the user


2. Compute tax=inc*(10/100)
3. Display tax to the user

◼ Write an algorithm to check if a given number is odd or even

Algorithm

1. Read the number(num) from the user


2. Divide the num by 2. If the remainder is zero display
It’s an even number
3. Otherwise display it’s an odd number

◼ Write algorithm to check if a given year is leap year

Algorithm

1. Read year (y) from the user


2. If y is divisible by 4 and not divisible by 100 or
Y is divisible by 400 then display year is leap year
3. Otherwise display year is not a leap year to user
◼ Write an algorithm to compute the tax on income for n persons

Algorithm

1. Read the number of persons(num_person) from the User


2. Initialize count to zero
3. Repeat the following steps while(count<num_person)
3.1 Read income from the user
3.2 Compute tax = income*10/100
3.3 Display tax to the user
3.4 Increment count by 1

◼ Algorithm to find the sum of the series 1+3+5+7+9..n. Read N from the user.

Algorithm

1. Read value n from the user


2. Initialize count to zero
3. Initialize i to 1
4. Initialize sum to zero
5. Repeat the following steps while (count<n)
5.1 Add i to sum
5.2 Increment i by 2
5.3 Increment count by 1
6. Display sum to the user

◼ Check if a given number is an armstrong number

Algorithm

1. Read the number(num) from the user


2. Determine length of the number (num_length)
3. Initialize sum to 0
4. Original_num=num
5. While (num>0)
5.1 Extract the remainder(last_digit) by dividing the number
5.2 num by 10
5.3 Raise the last digit to the power of num_length
5.4 And add it to sum
5.5 Divide num by 10 and assign it to num
6. If sum equals original number Display its
an Armstrong number
7. Otherwise display its not an Armstrong number
Write an algorithm to add two numbers entered by the user.

Step 1:
Start
Step 2:
Declare variables num1, num2 and sum.
Step 3:
Read values num1 and num2.
Step 4:
Add num1 and num2 and assign the result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop

Write an algorithm to find the largest among three different numbers entered by the
user.

Step 1:
Start
Step 2:
Declare variables a,b and c.
Step 3:
Read variables a,b and c.
Step 4:
If a > b
If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop

Write an algorithm to find all roots of a quadratic equation ax2+bx+c=0.

Step 1: Start
Step 2: Declare variables a, b, c, D, x1, x2, rp and ip;
Step 3: Calculate discriminant
D ← b2-4ac
Step 4: If D ≥ 0
r1 ← (-b+√D)/2a
r2 ← (-b-√D)/2a
Display r1 and r2 as roots.
Else
Calculate real part and imaginary part
rp ← -b/2a
ip ← √(-D)/2a
Display rp+j(ip) and rp-j(ip) as roots
Step 5: Stop

Write an algorithm to find the factorial of a number entered by the user.

Step 1: Start
Step 2: Declare variables n, factorial and i.
Step 3: Initialize variables
factorial ← 1
i ← 1
Step 4: Read value of n
Step 5: Repeat the steps until i = n
5.1: factorial ← factorial*i
5.2: i ← i+1
Step 6: Display factorial
Step 7: Stop
Write an algorithm to check whether a number entered by the user is prime or not.

Step 1: Start
Step 2: Declare variables n, i, flag.
Step 3: Initialize variables
flag ← 1
i ← 2
Step 4: Read n from the user.
Step 5: Repeat the steps until i=(n/2)
5.1 If remainder of n÷i equals 0
flag ← 0
Go to step 6
5.2 i ← i+1
Step 6: If flag = 0
Display n is not prime
else
Display n is prime
Step 7: Stop

Write an algorithm to find the Fibonacci series till term≤1000.

Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term ← 0 second_term ← 1
Step 4: Display first_term and second_term
Step 5: Repeat the steps until second_term ≤ 1000
5.1: temp ← second_term
5.2: second_term ← second_term + first_term
5.3: first_term ← temp
5.4: Display second_term
Step 6: Stop

Consider a bank that offers fixed deposit accounts with annual interest of 8% on the
balance available in the account. Define a structure “customer” to store the following
data: customer name (string), customer account number (int) and amount (int) to be
deposited. The program generates the balance available in the account at the end of
each year for first five years of all the customers

Write a program to get the details such as student register number, name and marks of
three subjects. Calculate the percentage of marks obtained by the students in a class
and rank them based on their average. Display the student name in the order of highest
rank (along with his percentage and rank)

You might also like