 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.

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 + a1 where a1 is the first number
then we do S = S + a2 (The new value of S contains a1 + a2)
then S = S + a3 (The new value of S contains a1 + a2 + a3)
and so on till S = S + an

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

1. Begin.
2. Get the values of variables x and y.
3. Assign the value of x to t.
4. Assign the value of y to x. So x has the original value of y now in place of the original value of x.
5. Assign the value of t to y.
6. Show the values of x and y.
7. Stop.

Algorithm

1. Begin.
2. Read n numbers to be summed.
3. Initialize sum as 0.
4. Initialize ‘count’ as 1.
5. While ‘count’ is less than or equal to n. i.e., numbers to be added, repeatedly do:
6. 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.
7. Stop.

Algorithm

1. Begin.
2. Get the decimal number whose binary equivalent you want.
3. Divide the decimal number by 2.
4. Write the remainder.
5. Quotient o£ previous stage becomes the dividend and again divide next stage.
6. Write the remainder to the right-hand side of the previous remainder.
7. Repeat the process successively till the quotient is zero.
8. 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.
9. 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

1. Begin.
2. Get a positive integer number to be reversed.
3. 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.
4. rite the integer ( dividend which is less than 10 ) to the RHS of the reversed number.
5. 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 2nd (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

1. Begin.
2. Get the number which you want to check whether it is prime or not.
3. Set the value of the divisor to 2.
4. 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.
5. 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.
6. 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 3rd element, then 4th, 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

1. Begin.
2. 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, a, a[n].
3. Find the smallest element within the unsorted array. ,
4. 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.
5. Repeat Steps 3 and 4 until all the elements in the array are arranged in ascending order.
6. 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:
12 = 1 x 1 = 1
22 = 2 x 2 = 4
32 = 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.

12 = 1 x 1 = 1   The square root of 1 is 1.
22 = 2 x 2 = 4   The square root of 4 is 2.
32 = 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.
f1 = 1 ( 1st number in the Fibonacci series )
f2 = 2 ( 2nd in the Fibonacci series )
f3 = f2 + f1 = 2 + 1 = 3
f4 = f3 + f2 = 3 + 2 = 5
f5 = f4 + f3 = 5 + 3 = 8 and so on.
f f f f f f f7 …..
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

1. Assign sum = 0, A = 0, B = 1, i = 1
2. Get the number up to which you want to generate the Fibonacci series, i.e., n.
3. Add A and B to get the next Fibonacci number in sum.
4. Assign the value of B to A, i.e, A = B.
5. Assign the value of sum to B, i.e., B = sum.
6. Write the value of the sum to get the next Fibonacci number in the series.
7. Increment i with 1 i.e. i=i+1 and repeat Steps 3, 4, 5, 6 till the last value of i = n.
8. 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 !}$$

Understanding the problem

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 X1, X3, X5… through the use of X1 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, – X3, + X5, – X7, + ….
To do that, you note that ( X )1, (-X)3, (X)5, (-X)7 gives rise to X, -X3, X5, -X7.
Hence, you use -1 as a successive multiplier to gener¬ate successive terms of the sequence from their magnitude.

Algorithm

2. Read n where ‘n’ is the number of terms of series which you may like to add up to get desired precision of sum.
3. For 1st term,
sum = x
product = 1 (variable ‘product’ used for denominator)
num = x (variable ‘num’ used for numerator) power = 1
4. For next term,
num = num x ( x2)
power = power + 2
product = product × (power – 1) × power next_term = num/product
5. Then,
sum = sum + next term
6. Repeat Steps 4 and 5, looping ‘n – 1’ times to get the sum of the first ‘n’ terms of the series.
7. Print sum.
8. 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 ⇔ a (Swap element 5 value with element 1 value)
a ⇔ a
a ⇔ a
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

1. Begin.
2. Get the values and store them in an array with elements a, a, a, a, …..,a[n]
3. Computer the number of exchanges needed to reverse the array r = integer value of n/2.
4. Exchange the i the element with [n – i +1] using
temp = a[i]
a[i]=a[n-i+1]
a[n-i+1]=temp
5. Carry out step 4 r times, increasing i by 1.
6. Print the reversed array elements a, a, a, a, …….. a[n].
7. 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

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

$$\left.\begin{array}{l} 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 1st row.
[6 1] in the second horizontal line is called 2nd row.

$$\left[\begin{array}{l} 8 \\ 6 \end{array}\right]$$is called 1st column.

$$\left[\begin{array}{l} 5 \\ 1 \end{array}\right]$$is called 2nd 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 =Aij, i = 1,2, ……,m;
j = 1,2, …, n
Lets = B = Bjk,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 = Cik, where i changes from 1, … m and
k = 1,…p
Thus, Cik = ai1 blk + ai12 b2k +…….+ ainbnk

Cik is obtained by multiplying the values/elements in the ith row of A with the corresponding values/elements in the kth 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 1st row and 1st column of the product matrix (A × B). This is the sum of the products of the corresponding elements of 1st row of A with 1st column of B.

Algorithm

1. Get 1st and 2nd matrices.
2. Check if the number of columns in the 1st matrix is equal to the number of rows in the 2nd matrix. Then matrices are compatible for multiplication. Otherwise, the product is not possible.
3. 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

1. Begin.
2. Read the order of a square matrix, n.
3. Read the contents of the matrix in
A [i] [j] form
4. Initialize i to 0 and j to 1.
5. Repeat Steps 6, and 7, until i is less than or equal to n.
6. Repeat the following steps until j is less than or equal to n.
(i) print a [i] [j]
(ii) Increment j by 1
7. 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

1. Input the Salesman’s name and the value of the goods sold.
2. Calculate the total wage. If the value of goods sold is less than or equal to Rs. 1000, then
wage = 1000 + 25% of value
3. If the value of goods sold is greater than Rs. 1000, then
wage = 1000 + 25% of value + 50
4. 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

1. Begin
2. Read the three numbers, i.e., a, b and c.
3. If a > b
i. If a > c, then a is the greatest number.
ii. else, c is the greatest number.
4. else
i. If b > c, then b is the greatest number.
ii. else, c is the greatest number.
5. 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

1. Begin.
2. Read the value of Celsius temperature (temp).
3. Fahren = 1.8 * temp + 32
4. Print ‘Converted Temperature is Fahren
5. 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

1. Begin.
2. Read the value of Celsius temperature (temp).
3. Fahren = 1.8 * temp + 32
4. print ‘Converted Temperature is ‘, Fahren
5. 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

1. Begin.
2. 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.
3. While inputting data is left repeat steps 4 and 5.
4. Read rno, sexcode, heightb, heightg.
5. if sexcode = 1 then
sum_boys_height = sum_boys_height + heightb ++total_boys
else
sum_girls_height = sum_girls_height + heightg ++total_girls
6. Avgboysheight = sumboysheight / totalboys
7. Avggirlsheight = sumgirlsheight / totalgirls
8. Print total_boys, Avg_boys_height, total_girls, Avg_girls_height
9. Stop.

Finding Average of Random N Numbers

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

Algorithm

1. Begin.
3. Calculate the sum of these n integers.
4. Divide the sum by n.
5. Print the final value as average.
6. 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

1. Begin.
2. Read the value of the number, say num.
3. Store the value of num in n, and initialize rev to 0.
digit = num % 10 rev = rev * 10 + digit num = num / 10
4. if (n == rev)
Print “the number is a palindrome”
5. else
Print “the number is not a palindrome”
6. 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

1. Begin.
2. Read the value of a number. say num
3. Divide the number by 2.
4. if the remainder is 1 then
Print “the number is odd”
5. else
Print “the number is even”
6. 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. 1. Begin.
2. 2. Read the value of the number, say, nun.
3. 3. if num > 0, print “The value of num is positive”
4. 4. else
5. 3. If num < 0, print “The value of num is negative”
6. 5. else, print “The value of num is zero”.
7. 6. Stop.

Finding the Maximum and Minimum Number

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

Algorithm

1. Begin.
2. Read the value of the first number, say num.
3. Store the value of num in Max and Min.
4. for (count = 1; count < 100; count = count + 1)
Read the next number, num. if max < num
max = num else if min > num
min = num
5. Print ‘the value of largest number’, max
6. Print ‘the value of smallest number’, min
7. Stop

Finding Grade for Given Total Marks

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

Algorithm

1. Begin.
2. Read the value of total marks, say total.
3. 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”.
4. stop.