0% found this document useful (0 votes)
2 views16 pages

Algorithm

The document outlines seven algorithms, including exchanging values of two variables, counting elements, summing a set of numbers, computing factorials, generating Fibonacci sequences, reversing digits of an integer, and converting characters to numbers. Each algorithm is presented with steps, input examples, execution details, and explanations of the output. The algorithms demonstrate fundamental programming concepts and operations in a concise manner.

Uploaded by

jimmypslick
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Algorithm

The document outlines seven algorithms, including exchanging values of two variables, counting elements, summing a set of numbers, computing factorials, generating Fibonacci sequences, reversing digits of an integer, and converting characters to numbers. Each algorithm is presented with steps, input examples, execution details, and explanations of the output. The algorithms demonstrate fundamental programming concepts and operations in a concise manner.

Uploaded by

jimmypslick
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Algorithm: Exchanging the Values of Two Variables


Step 1: Start
Step 2: Read the values of A and B.
Step 3: Set Temp = A.
Step 4: Set A = B.
Step 5: Set B = Temp.
Step 6: Display A and B.
Step 7: Stop.
2. Algorithm: Counting
Step 1: Start
Step 2: Read the value of N (number of elements).
Step 3: Set Count = 0.
Step 4: while Count < N, do the following steps
• a) Read a number.
• b) Set Count = Count + 1.
Step 5: Display Count.
Step 6: Stop.
3. Algorithm: Summation of a Set of Numbers
Step 1: Start
Step 2: Read the value of N (number of elements).
Step 3: Set Sum = 0.
Step 4: Set Count = 1.
Step 5: while Count ≤ N, do following steps
• a) Read a number (Num).
• b) Set Sum = Sum + Num.
•c) Set Count = Count + 1.
Step 6: Display Sum.
Step 7: Stop.
4. Algorithm: Factorial Computation
Step 1: Start
Step 2: Read the value of N.
Step 3: Set Factorial = 1.
Step 4: Set Count = 1.
Step 5: while Count ≤ N, do the following steps
• a) Set Factorial = Factorial * Count.
• b) Set Count = Count + 1.
Step 6: Display Factorial.
Step 7: Stop.
5. Algorithm: Generating Fibonacci Sequence
Step 1: Start
Step 2: Read the value of N (number of terms).
Step 3: Set a = 0, b = 1.
Step 4: Display a and b.
Step 5: Set Count = 3.
Step 6: while Count ≤ N, do the following steps
• a) Set c = a + b.
• b) Display c.
• c) Set a = b.
• d) Set b = c.
• e) Set Count = Count + 1.
Step 7: Stop.
6. Algorithm: Reversing the Digits of an Integer
Step 1: Start
Step 2: Read the integer N.
Step 3: Set Reverse = 0.
Step 4: while N > 0, do the following steps
• a) Set Digit = N % 10.
• b) Set Reverse = (Reverse * 10) + Digit.
• c) Set N = N / 10 (Integer Division).
Step 5: Display Reverse.
Step 6: Stop.
7. Algorithm: Character to Number Conversion
Step 1: Start
Step 2: Read the character Ch.
Step 3: Set Number = Ch - '0'.
Step 4: Display Number.
Step 5: Stop.
1. Exchanging the Values of Two Variables
Input:
A = 10, B = 20
Execution:
• A = 10, B = 20
• Temp = A = 10
• A = B = 20
• B = Temp = 10
Output:
A = 20
B = 10
Explanation:
The temporary variable Temp stores the value of A. Then the value of B is
assigned to A, and finally the value stored in Temp is assigned to B. Thus, the
values are exchanged.
2. Counting
Input:
N=5
Numbers: 12, 8, 25, 17, 30
Execution:
• Count = 0
• Read 12 → Count = 1
• Read 8 → Count = 2
• Read 25 → Count = 3
• Read 17 → Count = 4
• Read 30 → Count = 5
Output:
Count = 5
Explanation:
The algorithm reads one number at a time and increases the counter by 1. After
reading all 5 numbers, the final count is 5.
3. Summation of a Set of Numbers
Input:
N=5
Numbers: 10, 20, 30, 40, 50
Execution:
• Sum = 0
• Read 10 → Sum = 10
• Read 20 → Sum = 30
• Read 30 → Sum = 60
• Read 40 → Sum = 100
• Read 50 → Sum = 150
Output:
Sum = 150
Explanation:
The algorithm starts with Sum = 0. Each input number is added to Sum. After all
numbers are processed, the final sum is displayed.
4. Factorial Computation
Input:
N=5
Execution:
• Factorial = 1
• 1×1=1
• 1×2=2
• 2×3=6
• 6 × 4 = 24
• 24 × 5 = 120
Output:
Factorial = 120
Explanation:
Factorial is the product of all positive integers from 1 to N. For 5, the calculation
is:
5! = 1 × 2 × 3 × 4 × 5 = 120
5. Generating Fibonacci Sequence
Input:
N=7
Execution:
• a = 0, b = 1
• Output: 0 1
• c = 0 + 1 = 1 → Output: 0 1 1
• a = 1, b = 1
• c = 1 + 1 = 2 → Output: 0 1 1 2
• a = 1, b = 2
• c = 1 + 2 = 3 → Output: 0 1 1 2 3
• a = 2, b = 3
• c = 2 + 3 = 5 → Output: 0 1 1 2 3 5
• a = 3, b = 5
• c = 3 + 5 = 8 → Output: 0 1 1 2 3 5 8
Output:
0112358
Explanation:
The Fibonacci sequence starts with 0 and 1. Every new number is obtained by
adding the previous two numbers.
6. Reversing the Digits of an Integer
Input:
N = 125
Execution:

N Digit Reverse

125 5 5

12 2 52

1 1 521

Output:
521
Explanation:
The last digit is extracted using % 10 and added to Reverse.
• 125 % 10 = 5 → Digit = 5
• 12 % 10 = 2 → Digit = 2
• 1 % 10 = 1 → Digit = 1
The last digit is then removed using integer division (/ 10).
• 125 / 10 = 12
• 12 / 10 = 1
• 1 / 10 = 0
The process continues until N becomes 0.
Thus, the digits of 125 are reversed to obtain 521.
7. Character to Number Conversion
Input:
Ch = '7'
Execution:
• Ch = '7'
• Number = '7' - '0'
• Number = 55 - 48
• Number = 7
Output:
7
Explanation:
Characters are stored using numeric codes (ASCII). The character '0' has the value
48 and '7' has the value 55. Subtracting '0' from '7' gives:
55 − 48 = 7
Thus, the character '7' is converted into the integer 7.

You might also like