You can learn about Flowcharts and Algorithms in Python Programs with Outputs helped you to understand the language better.

## Python Programming – Sample Algorithms

**Sample Algorithms**

A set of instructions that describes the steps to be followed to carry out an activity is called an algo¬rithm or procedure for solving a problem. Some examples of algorithms are given in the following sub-sections:

**Exchanging the Values of Two Variables**

Exchanging the values of two variables means inter-changing their values. You can clearly understand this by example.

Suppose you have two variables x and y. Our aim is to swap or interchange the values of x and y.

The original values of x and y are:

You find that the values of x and y have now been interchanged or swapped as desired. The algorithm for exchanging the values of two variables is given in Figure 2.13.

- Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing
- Introduction to Python Programming – The Basic Model Of Computation
- Introduction to Python Programming – Algorithms

**Summation of a Set of Numbers**

Summation of a set of numbers is like adding numbers given in a series. To add a set of numbers manually, you start adding the digits in the rightmost column. For example:

When using a computer, you must design an algorithm to perform this task with a different approach. The computer can add two numbers at a time and return the sum of two numbers.

So to add n numbers, you initialize the variable location S, where you are going to store the sum, by the assignment S = 0. Now, you first do the following:

S =S + a_{1} where a_{1} is the first number

then we do S = S + a_{2} (The new value of S contains a_{1} + a_{2})

then S = S + a_{3} (The new value of S contains a_{1} + a_{2} + a_{3})

and so on till S = S + a_{n}

Here, you are repeating the same process again and again. The only difference is that the values of a and S change with each step. The algorithm for the summation of a set of numbers is given in Figure 2.14.

**Decimal Base to Binary Base Conversion**

Before doing such a conversion, you need to know the definition of the decimal number system and binary number system.

**Decimal Number System**

The decimal number system is the most common number system which you use in our daily lives. It is the set of numbers having a base of 10. Since a decimal number system uses 10 digits from 0 to 9, i.e., 0,1, 2, 3, 4, 5, 6, 7, 8, and 9, it has a base 10. The decimal number system is also called the base 10 system. For example, 6598, 75,100567, etc., are examples of decimal numbers.

- The base or radix of the decimal number system is 10.

**Algorithm**

- Begin.
- Get the values of variables x and y.
- Assign the value of x to t.
- Assign the value of y to x. So x has the original value of y now in place of the original value of x.
- Assign the value of t to y.
- Show the values of x and y.
- Stop.

**Algorithm**

- Begin.
- Read n numbers to be summed.
- Initialize sum as 0.
- Initialize ‘count’ as 1.
- While ‘count’ is less than or equal to n. i.e., numbers to be added, repeatedly do:
- Calculate the sum of n numbers. (After the number at n<sup>th</sup> count has been added, the control will shift to Step 7.)

a. Read the number at position ‘count’. i.e., when the count is 1 read the first number, when the count is 2, read the second number, and so on.

b. Update the current sum by adding to it the read number.

c. Add 1 to ‘count’. - Stop.

**Algorithm**

- Begin.
- Get the decimal number whose binary equivalent you want.
- Divide the decimal number by 2.
- Write the remainder.
- Quotient o£ previous stage becomes the dividend and again divide next stage.
- Write the remainder to the right-hand side of the previous remainder.
- Repeat the process successively till the quotient is zero.
- The set of digits formed by the remainders at different stages, when placed in the reverse order, will form the binary equivalent of the decimal number.
- Stop.

**Binary Number System**

Binary means 2. The set of numbers having a base called the binary number system. Binary numbers consist of only two digits, i.e., 0 and 1. Each one of these is also called a bit, a short form of binary digit.

- For storing the binary number, the electronic elements of a computer need to have only two stable states. The output of such electronic circuits at any time is either high or low. These states represent 1 and 0 respectively. High is represented by 1 and low is represented by 0.

The computer understands only binary number systems. But you use data in the form of decimal digits. All such numbers are converted to binary codes within the computer because computers can operate using binary numbers only.

**Conversion of Decimal Number to Binary Number**

To convert a decimal number to a binary number, the decimal number is divided by 2 successively. The quotient and remainders are noted down at each stage. The quotient of the preceding stage is divided by 2 at the next stage. The process is repeated until the quotient becomes zero. Now, to make a binary equivalent put the remainders in reverse order. For example, the binary representa¬tion of the decimal number 13 is shown here.

So binary representation of (13)_{10} will be (1101)_{2}. The algorithm for the conversion of decimal numbers to binary numbers is given in Figure 2.15.

**Reversing Digits of an Integer Number**

Reversing digits basically means changing the order of the digits backward. For example:

Input 3 5 6 8

Result 8 6 5 3 (Each digit placed in the reverse) To do so, you divide the number by 10 and get the remainder.

You can get the remainder by a mod( ) function:

r = 3568 mod ( 10 )

= 8

Now remove 8 from the original integer. So you next get 356 ( number to be reversed ).

again

r = 356 mod ( 10 )

= 6

Successively, you find out the remainders until the dividend is less than 10. Now place the digits of the remainders in the reverse order.

Algorithm for reversing digits of an integer number is given in figure 2.16.

**Algorithm**

- Begin.
- Get a positive integer number to be reversed.
- While the integer number being reversed is greater than 10 repeatedly do:

a. Extract the rightmost digit of the number to be reversed by remainder function, i.e., mod( ) function.

b. Construct the reversed integer by writing the extracted digit to the right-hand side of the current reversed number. - rite the integer ( dividend which is less than 10 ) to the RHS of the reversed number.
- Stop.

**GCD ( Greatest Common Divisor ) of Two Numbers**

The greatest number which is a common factor of two or more given numbers is called GCD (Greatest Common Divisor).

**How to find GCD?**

Suppose two numbers are given. Now, divide the greater number by the smaller one. Next, divide the divisor by the remainder. Continue on repeating the process of dividing the preceding divisor by the remainder last obtained, till the remainder zero is obtained. Then, the last divisor is the required GCD of the given numbers. Suppose, you want to find GCD of 136 and 170, then

Say, a= 136

b = 170

The greater number is b, i.e., 170; so b will be a dividend.

The smaller number is a, i.e., 136; so a will be divisor.

**Algorithm**

1. Begin.

2. Get 1st and 2^{nd} (non-zero) integers, i.e., a and b.

3 . Check which integer is larger.

4. Get the remainder by dividing the greater integer by the smaller integer.

5. Now make a smaller integer dividend and divide the dividend by the divisor for the remainder. Let the remainder be the divisor.

6. Repeat Steps 4, 5, and 6 until a ‘zero’ remainder is obtained. Now, the last divisor is the GCD.

8. Stop.

**Test whether the Given Number is a Prime Number**

The natural number 1 has only one factor. Except 1, every decimal number has two or more factors. For example, 2 has two factors 1 and 2; the number 3 has the factors 1 and 3. The number 4 has three factors 1, 2, and 4; the number 5 has two factors 1 and 5. The positive numbers which have only two factors, i.e., 1 and that number itself are called prime numbers. The example of prime numbers is 2, 3, 5, 7, 11, 13, 17, etc.

- Prime numbers are any of the positive integer numbers greater than 1, each divisible only by itself and by 1. Thus, 2, 3, 5,7, etc., are prime numbers.

The aim here is to find the prime numbers. A Greek mathematician Eratosthenes who lived in the 3rd century BC (Before Christ) gave a very simple method for finding a prime number. The method is known as the “Sieve method”. Suppose you have to find all the prime numbers in the range of 1 to 20. To find the prime number, follow the steps:

1. Strikeout 1 because it is not a prime number.

2. Encircle 2 and strike out all the multiples of 2, i. e., 4, 6, 8,10,12,…. 20.

3. Encircle 3 and strike out all the multiples of 3.

You continue the process of encircling and strik¬ing till every number in the list is either encircled or struck out.

All the circled numbers are thus prime numbers and the numbers that are struck out except 1 are composite numbers. This method is given as an algorithm below.

**Algorithm**

- Begin.
- Get the number which you want to check whether it is prime or not.
- Set the value of the divisor to 2.
- Divide the number by divisor (i.e., 2 first) If the remainder is zero, then the given number is not prime; else the process is to be continued.
- Increment divisor by 1; divide the number by the incremented divisor, and check if the remainder is zero. Keep repeating the process till either the remainder becomes zero or until the divisor is equal to the number itself. If remainder becomes zero at any stage, the number is not prime; else it is prime if carried to the last stage, till incremented divisor is the number itself.
- Stop.

**Organize Numbers in Order (Sorting)**

Sorting of numbers means arranging a given set of numbers either in ascending or in descending order. To sort the data, the selection-sort method is popularly used. On the computer systems, selection sort is implemented in the form of exchange selection sort that requires a single array to work with. In this technique, the computer keeps on finding the next smallest element and brings it to its appropriate position. To sort data in ascending order, you do the following:

1. Place the values in the form of the one-dimensional array as shown in Figure 2.19( a ).

2. Find the smallest value and place it as the first item in the array. At the same time, move the value of the first location of the array to the location where the smallest value was stored. It means interchange the value of a[ 5 ] with an [ 1] as shown in Figure 2.19(b).

3. Now an [ 1] element has the smallest value. See Figure 2.19(c). Next, compare the elements of a[ 2 ] with other elements in the unsorted array. You get the array as shown in Figure 2.19(d).

Swap the value of a[ 2 ] and a[ 6 ] with the help of a temporary variable, temp. See Figure 2.19( e ).

Repeat the above steps to get the 3^{rd} element, then 4^{th}, and so on. set Figure 2.19 ( f ).

Note that you need to process the unsorted array only (n-1) times. All the elements of the array would now be in ascending order. See Figure 2.19(g).

**Algorithm**

- Begin.
- Read numbers and store them in an array of n elements, i.e., size of array a is n with elements as a[l], a[2], a[3], a[n].
- Find the smallest element within the unsorted array. ,
- Exchange, i.e., swap the smallest value of the array element with that of the 1st element i.e., store it in the 1st element of the unsorted part of the array.
- Repeat Steps 3 and 4 until all the elements in the array are arranged in ascending order.
- Stop.

**Find the Square Root of an Integer Number**

Before understanding square root, you must know what you mean by squaring a number. To the square, a number is to multiply the number by itself. For example:

1^{2} = 1 x 1 = 1

2^{2} = 2 x 2 = 4

3^{2} = 3 x 3 = 9

Square root, on the contrary, refers to one of the two equal factors of a number. For example,

Let us read some more examples to understand the square root.

1^{2} = 1 x 1 = 1 The square root of 1 is 1.

2^{2} = 2 x 2 = 4 The square root of 4 is 2.

3^{2} = 3 x 3 = 9 The square root of 9 is 3.

Therefore, if you have to find the square root of 49 then you will see “what number multiplied by itself will give 49”. Certainly 7 × 7 = 49. But (-7 × -7) is also 49.

So, ± 7 is the square root of 49.

- A square root of a given number n is that natural number which when multiplied by itself gives n.

You can say that if a is the square root of b then b is the square of a. For example,

\(\sqrt{49}\)= ±7 (±7 is a square root of 49; 49 is square of +- 7)

\(\sqrt{64}\) = 8 (±8 is square root of 64; 64 is square of +- 8)

**Algorithm**

1. Place bar over every pair of digits starting with the right-hand side. The number of bars indicates the number of digits in the square root.

\(\overline{5} \overline{29}\)

2. Find the largest square root for the first pair that is less than or equal to the first pair. Take the square root of this number as a divisor and get a quotient. Subtract the product and bring down the next pair of digits to the right of the remain-der.

3. Double the quotient as it appears and enters it with a blank on the right for the next digit, as the next possible divisor.

4. Guess a possible digit to fill the blank and also to become the new digit in the quotient. Enter this digit in both places. Multiply the new digit in the quotient and the new divisor. Subtract and bring down the next period.

5. Repeat Steps 3 and 4 till the last pair has been used and the remainder comes to zero.

**Factorial of a Given Number**

The product of all positive integers from 1 to n is called the ‘factorial n’ or ‘n factorial’. It is denoted by n!. For example,’

5! = 1×2×3×4×5 = 120

n! = 1×2×3 (n – 2)×(n – 1)×n

Here, n is a positive integer.

- It may be noted that 0! = 1, which means factorial zero is equal to 1.

Let us taken an example. Suppose you want to find out the value of factorial 5.

Initially assign the value of i = 1 and factorial = 1. So the method will be to successively go on increasing the value of i by 1 and keep updating the value of factorial by multiplying the current value of i by the value of factorial till the incremented value of i equals to the number desired for finding the factorial.

Thus, factorial = previous value of factorial × i,

i. e., start with i = 1, giving factorial 1 = 1×1.

Now increment the value of i by 1, giving updated factorial 2 = 1×2 = 2

Again increase the value of i by 1, giving

factorial 3 = 2×3 = 6

Increment the value of i until the value of i becomes 5 and corresponding value of factorial is obtained.

To summarize, the factorial will be calculated in the following way:

factorial 1 = 1×1 =1

factorial 2 = 1×2 =2

factorial 3 = 2×3 =6

factorial 4 = 6×4 =24

factorial 5 = 24 x 5 = 120

Hence, 5! = 120

**Algorithm**

1. Begin.

2. Get the number of which the factorial is to be calculated, i.e., n.

3 . Assign the value as i = 1 and factorial = 1.

4. Calculate factorial n = factorial (n-1) x i

5. Increment the value of i by 1, i.e., i=i+l.

6. Repeat Steps 4 and 5 till Step 4 has been executed with the value of i = n.

7. Write the value of factorial.

8. Stop.

**Fibonacci Sequence**

In a Fibonacci sequence, the previous two numbers are added to generate the next Fibonacci number.

f_{1} = 1 ( 1^{st }number in the Fibonacci series )

f_{2} = 2 ( 2^{nd} in the Fibonacci series )

f_{3 }= f_{2 }+ f_{1} = 2 + 1 = 3

f_{4 }= f_{3} + f_{2} = 3 + 2 = 5

f_{5} = f_{4 }+ f_{3 }= 5 + 3 = 8 and so on.

f_{1 } f_{2 } f_{3 } f_{4 } f_{5 } f_{6 } f_{7} …..

1 2 3 5 8 13 21 …..

In other words, to get the next Fibonacci number, you have to do some of the previous two numbers in the series.

- The Fibonacci sequence is named after Fibonacci.

**Algorithm**

- Assign sum = 0, A = 0, B = 1, i = 1
- Get the number up to which you want to generate the Fibonacci series, i.e., n.
- Add A and B to get the next Fibonacci number in sum.
- Assign the value of B to A, i.e, A = B.
- Assign the value of sum to B, i.e., B = sum.
- Write the value of the sum to get the next Fibonacci number in the series.
- Increment i with 1 i.e. i=i+1 and repeat Steps 3, 4, 5, 6 till the last value of i = n.
- Stop.

**Evaluate ‘ sin ( x ) ’ as Sum of a Series**

You know that

\(\sin x=\frac{x}{1 !}-\frac{x^{3}}{3 !}+\frac{x^{5}}{5 !}-\frac{x^{j}}{7 !}+\frac{x^{9}}{9 !}\)

Here, x is in radians.

**Understanding the problem**

1. ReadX.

2. Keep accumulating a product of integers (PROD-UCT) in such a way that PRODUCT is initially 1,

then 1×2×3,

then 1×2×3×4×5, and so on.

PRODUCT serves as the denominator in terms of the formula.

3. Accumulate the terms:

\(\frac{\mathrm{X}}{\text { PRODUCT }}, \frac{\mathrm{X}^{3}}{\text { PRODUCT }}, \frac{\mathrm{X}^{5}}{\text { PRODUCT }}, \ldots\)

4. Compute X^{1}, X^{3}, X^{5}… through the use of X^{1} where i is initially set to 1 and incremented by 2’s to obtain X and odd power of X.

5. Generate the oscillating sequence of terms

X, – X^{3}, + X^{5}, – X^{7}, + ….

To do that, you note that ( X )^{1}, (-X)^{3}, (X)^{5}, (-X)^{7} gives rise to X, -X^{3}, X^{5}, -X^{7}.

Hence, you use -1 as a successive multiplier to gener¬ate successive terms of the sequence from their magnitude.

**Algorithm**

- Read x in radians.
- Read n where ‘n’ is the number of terms of series which you may like to add up to get desired precision of sum.
- For 1
^{st}term,

sum = x

product = 1 (variable ‘product’ used for denominator)

num = x (variable ‘num’ used for numerator) power = 1 - For next term,

num = num x ( x^{2})

power = power + 2

product = product × (power – 1) × power next_term = num/product - Then,

sum = sum + next term - Repeat Steps 4 and 5, looping ‘n – 1’ times to get the sum of the first ‘n’ terms of the series.
- Print sum.
- Stop.

**Reverse the Order of Elements of an Array**

Reverse order of elements means the value of 1st posi¬tion of the array is exchanged with the last element of the array, 2nd element of the array is exchanged with ‘2nd last’ and so on.

For example, see Figure 2.24(a).

Here,

a[1] ⇔ a[5] (Swap element 5 value with element 1 value)

a[2] ⇔ a[4]

a[3] ⇔ a[3]

Here, you can see the suffixes/subscripts on the left-hand side are in increasing order and the suffix-es/subscripts on the right-hand side are in decreasing order. (See Figure 2.24(b)).

For determining the reverse-array subscript val¬ues, you can use the formula [n – i + 1] where ‘n’ is total number of elements an Y is the subscript of the element of original array.

n – i + 1

5 – 1 + 1 = 5

5 – 2 + 1 = 4

5 – 3 + 1 = 3

Swapping the values of that element in the array would thus be reversed.

To exchange the values of the elements of the array, you need one temporary variable. Then you do the swapping as given below.

∴ temp = a[i]

a [i] = a [n – i + 1]

a [n – i + 1] = temp

**Algorithm**

- Begin.
- Get the values and store them in an array with elements a[1], a[2], a[3], a[4], …..,a[n]
- Computer the number of exchanges needed to reverse the array r = integer value of n/2.
- Exchange the i the element with [n – i +1] using

temp = a[i]

a[i]=a[n-i+1]

a[n-i+1]=temp - Carry out step 4 r times, increasing i by 1.
- Print the reversed array elements a[1], a[2], a[3], a[4], …….. a[n].
- Stop.

**Find Largest Number in an Array**

The largest number means the number which is greater than all other numbers in the array elements. Another number(s) in the set may be equal to this number but cannot be greater than this number. For example, in the following array:

you find that the number 19 is the largest number. From a small set of numbers stored in the array, it is easy to find the largest number, but finding the largest value from thousands of numbers stored in the array may take a long time. But using the following logic, you can get the answer in a short time.

**Logic**

Assign the value of the first element of the array to variable temp; then compare the value of temp to those of other elements in the array in order, one by one. If any element value is greater than temp then put that value in temp and continue the comparison.

For the array shown above the value of temp = 10 taken from the first element of the array.

Compare value in temp to 2nd element value of the array. Here, 11 is greater than 10, so delete the original value of temp and write down the 2nd element,

i. e., 11 in temp. Then compare the 3rd element value with the new temp value. The procedure will be con¬tinued till all the elements in the array have been examined. In the end, you will get the largest value in the temp variable.

**Algorithm**

- Begin.
- Get the value of elements in an array and store them in the array elements a[1], a[2] a[n] where n is the number of elements.
- Set temporary variable and assign the value of 1st array element a[1] to temp.
- Compare temp with the next element an [2] of the array.
- If the element is having a greater value than temp, assign that value to temp.
- Repeat Steps 4 and 5 till the last element an [n] of the array is encountered.
- The final value of temp is the largest value stored in the array.
- Stop.

**Multiplication of Two Matrices**

Arthur Kelley was the first person to introduce the concept of matrix. Later on, the study of the matrix was utilized to solve different types of linear equation problems.

**How to form a Matrix?**

8 x+5 y=2 \\

6 x+y=3

\end{array}\right\}\)

is a system of two linear equations. Arrange the coefficient of x and y in the following way:

\(\left[\begin{array}{ll}

8 & 5 \\

6 & 1

\end{array}\right]\)

[8 5] in the top horizontal line is called 1^{st} row.

[6 1] in the second horizontal line is called 2^{nd} row.

\(\left[\begin{array}{l}

8 \\

6

\end{array}\right]\)is called 1^{st} column.

\(\left[\begin{array}{l}

5 \\

1

\end{array}\right]\)is called 2^{nd} column.

A matrix is a two-dimensional array having m rows and n columns. So, you have m x n positions. Each row consists of m elements, and each column consists of n elements.

**Matrix Multiplication**

Figure 2.27 shows the elements of a 3 × 3 matrix.

Let A =A_{ij}, i = 1,2, ……,m;

j = 1,2, …, n

Lets = B = B_{jk},j = 1,2 ……. ,n;

k = 1,2, …, p

The product AB of matrices A and B will have i rows and k columns.

The two matrices A (m×n) and B (n×p) are such that the number of rows of B is the same as the number of columns of A. Then the product of these two matrices can be carried out and it is defined as:

C = AB = C_{ik}, where i changes from 1, … m and

k = 1,…p

Thus, C_{ik} = a_{i1} b_{lk} + a_{i12} b_{2k} +…….+ a_{in}b_{nk}

C_{ik} is obtained by multiplying the values/elements in the i^{th} row of A with the corresponding values/elements in the k^{th} column of B and then adding them.

- The matrix A is said to be compatible for multiplication with the matrix B if the number of columns of A is equal to the number of rows of B.

It should be noted that there can be matrices that are not compatible with multiplication, and in that case, you can say that the multiplication, of matrices, is not defined.

For example, if

Here, A is a matrix of size 2 × 3, i.e., 2 rows and 3 columns.

B is a matrix of size 4 × 2, i.e., 4 rows and 2 columns.

You know that for obtaining the product of two matrices, the number of columns of matrix A must be equal to the number of rows of matrix B.

But, A has 3 columns and B has 4 rows, so A and B are not compatible for multiplication.

Let us take an example where the multiplication of matrices is defined.

Here, A and B are compatible. The product is as follows:

Here 6 is the value of the element lying in the 1^{st} row and 1^{st} column of the product matrix (A × B). This is the sum of the products of the corresponding elements of 1^{st} row of A with 1^{st} column of B.

**Algorithm**

- Get 1st and 2nd matrices.
- Check if the number of columns in the 1
^{st}matrix is equal to the number of rows in the 2^{nd}matrix. Then matrices are compatible for multiplication. Otherwise, the product is not possible. - If matrices are compatible then find the sum of the product of the corresponding elements of each row of A with the corresponding element of each column of B.

Let us consider 4 × 4 square matrices. Now, to print the last (4-i + 1) elements in every ith row, in the first row, the last (4 – 1 + 1) = 4 elements have to be printed. In the second row, the last 3 elements have to be printed. Hence, the following values will be printed.

1 2 3 4 6 7 8 11 12 16

**Algorithm**

- Begin.
- Read the order of a square matrix, n.
- Read the contents of the matrix in

A [i] [j] form - Initialize i to 0 and j to 1.
- Repeat Steps 6, and 7, until i is less than or equal to n.
- Repeat the following steps until j is less than or equal to n.

(i) print a [i] [j]

(ii) Increment j by 1 - Stop.

**Evaluate the Salesman’s Wage**

Suppose a salesman earns a basic wage of Rs. 1000 in a week. He also earns a commission of 25% of the value of all the goods he sells. If he sells more than Rs. 1000 of goods in any week, he receives a bonus of Rs. 50. Write the algorithm.

**Algorithm**

- Input the Salesman’s name and the value of the goods sold.
- Calculate the total wage. If the value of goods sold is less than or equal to Rs. 1000, then

wage = 1000 + 25% of value - If the value of goods sold is greater than Rs. 1000, then

wage = 1000 + 25% of value + 50 - Print the total wage.

**Finding Maximum Among Three Numbers**

To determine the maximum out of three numbers, you have to assume three numbers, such as a, b and c Numbers, and follow the algorithm given below.

**Algorithm**

- Begin
- Read the three numbers, i.e., a, b and c.
- If a > b

i. If a > c, then a is the greatest number.

ii. else, c is the greatest number. - else

i. If b > c, then b is the greatest number.

ii. else, c is the greatest number. - Stop.

**Finding the Sum of First n Even Numbers**

To get the sum of first n even numbers, you assume an array A of n elements and follow the algorithm given below:

**Algorithm**

- Begin.
- Read the value of Celsius temperature (temp).
- Fahren = 1.8 * temp + 32
- Print ‘Converted Temperature is Fahren
- Stop.

**Conversion of Temperature**

Let us assume temp to be temperature given m Celsius To convert it into Fahrenheit follow the algorithm given below:

**Algorithm**

- Begin.
- Read the value of Celsius temperature (temp).
- Fahren = 1.8 * temp + 32
- print ‘Converted Temperature is ‘, Fahren
- stop.

**Finding the Average Height Of Boys and Girls in a Class**

To find the average height of boys and girls in a class follow the algorithm given below:

**Algorithm**

- Begin.
- Initialize the counters to accumulate a total number of girls and boys in the class and also their respective heights, i.e., sum_boys_height, sum_girls_height, heightb, heightg.
- While inputting data is left repeat steps 4 and 5.
- Read rno, sexcode, heightb, heightg.
- if sexcode = 1 then

sum_boys_height = sum_boys_height + heightb ++total_boys

else

sum_girls_height = sum_girls_height + heightg ++total_girls - Avgboysheight = sumboysheight / totalboys
- Avggirlsheight = sumgirlsheight / totalgirls
- Print total_boys, Avg_boys_height, total_girls, Avg_girls_height
- Stop.

**Finding Average of Random N Numbers**

To calculate the average of random n numbers, follow the algorithm given below:

**Algorithm**

- Begin.
- Read random n integers.
- Calculate the sum of these n integers.
- Divide the sum by n.
- Print the final value as average.
- Stop.

**Checking Given Number is Palindrome or Not**

A palindrome is a number that appears the same when it is read from left to right or from right to left. For example, 1234321 is a palindrome number. To check whether the given number is palindrome or not, follow the algorithm given below:

**Algorithm**

- Begin.
- Read the value of the number, say num.
- Store the value of num in n, and initialize rev to 0.

digit = num % 10 rev = rev * 10 + digit num = num / 10 - if (n == rev)

Print “the number is a palindrome” - else

Print “the number is not a palindrome” - Stop.

**Checking the given Number is Odd or Even**

To check whether the given number is odd or even, follow the algorithm given below:

**Algorithm**

- Begin.
- Read the value of a number. say num
- Divide the number by 2.
- if the remainder is 1 then

Print “the number is odd” - else

Print “the number is even” - Stop.

**Checking the given Number is Positive, Negative, or Zero**

To check whether the given number is positive, negative or zero, follow the algorithm given below:

**Algorithm**

- 1. Begin.
- 2. Read the value of the number, say, nun.
- 3. if num > 0, print “The value of num is positive”
- 4. else
- 3. If num < 0, print “The value of num is negative”
- 5. else, print “The value of num is zero”.
- 6. Stop.

**Finding the Maximum and Minimum Number**

To find the maximum and minimum number, follow the algorithm given below:

**Algorithm**

- Begin.
- Read the value of the first number, say num.
- Store the value of num in Max and Min.
- for (count = 1; count < 100; count = count + 1)

Read the next number, num. if max < num

max = num else if min > num

min = num - Print ‘the value of largest number’, max
- Print ‘the value of smallest number’, min
- Stop

Finding Grade for Given Total Marks

To find grade for given total marks, follow the algo-rithm given below:

**Algorithm**

- Begin.
- Read the value of total marks, say total.
- if (total > 100) print “Error: total is out of range ”

else if (total >=90) print “Grade A”

else if (total >=80) print “Grade B”

else if (total >=70) print “Grade C”

else if (total >=60) print “Grade D”

else if (total >=50) print “Grade E”

else if (total >=0) print “Grade F”

else print “Error: total is out of range”. - stop.