**Click this link for Algorithm Theory**

**Example : Write an algorithm to convert the length in feet to centimeter.**

```
Step 1: Start
Step 2: Input the value of feet, say F
Step 3: Calculate as following
Cm=F x 30
Step 4: Print Cm
Step 5: End
```

**Example : Write an algorithm to convert Temperature from Fahrenheit (℉) to Celsius (℃).**

```
Step 1: Start
Step 2: Read temperature in Fahrenheit,say F
Step 3: Calculate temperature (say C) as following
C=5/9*(F-32)
Step 4: Print C
Step 5: End
```

**Example : Write an algorithm to convert Temperature from Celsius (℃) to Fahrenheit (℉) .**

```
Problem definition : To Convert temperature in Celsius to Fahrenheit.
Problem Analysis :
input – c value
Output – f value
Algorithm
Step 1:Start
Step 2:Input c
Step 3: Calculate f=9/5*c+32
Step 4: Print f
Step 5:Stop
```

**Example : Write an algorithm that will read the two sides of a rectangle and calculate its area. **

```
Step 1: Start
Step 2: Input two sides of rectangle say L and W
Step 3: Calculate as following
Area = L x W
Step 4: Print Area
Step 5: End
```

**Example : Write an algorithm to add two numbers entered by user.**

```
Step 1: Start
Step 2: Initialize sum=0
Step 3: Read/Input two values into num1 and num2 respectively.
Step 4: (Add num1 and num2 values and assign the result to sum.)do as following
sum=num1+num2
Step 5: Print sum
Step 6: Stop
```

**Example : Write an algorithm to find the average of three numbers entered by the user.**

```
Step 1 Start
Step 2 Read the numbers a, b, c
Step 3 Compute the sum of a, b and c
Step 4 Divide the sum by 3
Step 5 Store the result in variable d
Step 6 Print the value of d
Step 7 End of the program
```

**Example : Write an algorithm to calculate simple interest of entered required values.**

```
Step 1: Start
Step 2: Read Principle,rate,and time
Step 3: Calculate and store the interest as "Simple Interest=(Principle*rate*time)/100
Step 4: Print Simple Interest,
Step 5: End
--------------- OR ---------------
Problem definition : To find simple interest of given values.
Problem Analysis :
inputs required – p, r, t.
Output – simple interest.
Algorithm is -
Step 1:Start
Step 2:input p,r,t
Step 3: calculate si=p*r*t/100
Step 4: Print si
Step 5:Stop
```

**Example : Write an algorithm to check the given number is odd or even.**

```
Step 1: Start
Step 2: Read a number say num1,
Step 3: Calculate remainder value as, remainder=num1 modulo 2,
Step 4: If remainder is equal to 0 then Print Even, otherwise Print Odd,
Step 4: End.
```

**Example : Write an algorithm to determine a student is pass or fail on the basis of average of four marks.**

```
Step 1: Start
Step 2: Input four subjects marks say m1,m2,m3 and m4.
Step 3: Calculate average marks as Avg=(m1+m2+m3+m4)/4.
Step 4: if (Avg < 40) then
Print “FAIL”
otherwise
Print “PASS”
Step 5: End
```

**Example : Write an algorithm that will ****calculate the roots of a quadratic equation ax**^{2} +bx c =0 . **[ Hint: d = sqrt ( ), and the roots are: x1 =****(–b + d)/2a and x2 = (–b – d)/2a. ]**

^{2}+bx c =0 .

```
Step 1: Start
Step 2: Input three coefficient of quadratic equation say a, b, c
Step 3: Calculate as following
d = sqrt(b.b-4ac)
Step 4: Again, Calculate as following
x = (–b + d) / (2 x a)
Step 5: Again, Calculate as following
y = (–b – d) / (2 x a)
Step 6: Print x & y.
Step 7: End
```

**Example : Write an algorithm that reads two values, determines the largest value and prints the largest value with an identifying message.**

```
Step 1: Start
Step 2: Input two values, say Val1, Val2
Step 3: if (Val1 > Val2) then
MAX = Val1
else
MAX = Val2
Step 4: Print “The largest value is”, MAX
Step 5: End
```

**E****xample : Write an algorithm to find the smallest number between two numbers**

```
Step 1: Start
Step 2: Input two numbers, say A and B
Step 3: If A<B then store/assign as small = A otherwise store/assign small=B
Step 4: Print Small
Step 5: End
```

**Example : Write an algorithm that reads three numbers and prints ****the largest number.**

```
Start 1: Start
Step 2: Input three values, say N1, N2, N3
Step 3: Compare as followings
if (N1>N2) then
if (N1>N3) then
MAX = N1
else
MAX = N3
else
if (N2>N3) then
MAX = N2
else
MAX = N3
Step 4: Print “The largest number is”, MAX
Step 5: End
```

**Example : Write an algorithm to prints all the numbers from 1 to 20****.**

```
Step 1: Start
Step 2: Initialize X as 1, say X=1
Step 3: If X is less than or equal to 20 then go to step 4 otherwise go to step 6. [OR]
Do as following
if(X<=20)
Go to Step 4
else
Go to Step 6
Step 4: Print X
Step 5: Increment X by 1 (as X=X+1) and go to step 3
Step 6: End
```

**Examples :** **Write an algorithm to find sum of first N numbers.**

```
Step 1: Start
Step 2: Read the value of N
Step 3: Put/Initialize i=1, sum=0
Step 4: if (i<N) go to step 5 otherwise go to step 8
Step 5: Update sum = sum + i
Step 6: Update i = i+1
Step 7: go to step 4
Step 8: Print sum
Step 9: Stop
```

**Examples :** **Write algorithm to find the factorial of a given number (N).**

```
Step 1 : Start
Step 2: PROD = 1
Step 3: I = 0
Step 4: Read N
Step 5: While I < N do
I = I + 1
PROD = PROD * I
Step 6: Write “Factorial of the given no.”, N, “is”, PROD
Step 7: End.
```

**Examples : **Write an algorithm to find sum of given data values until negative value is entered.

```
Step 1: Start
Step 2: SUM = 0
Step 3: I = 0
Step 4: Read NEW_VALUE
Step 5: While NEW VALUE < = 0 do
SUM = SUM + NEW_VALUE
I = I + 1
Read NEW_VALUE
Step 6: Write “Sum of”, I, “data value is", SUM
Step 7: END
```

1,729 total views, 1 views today

## 0 Comments