2. Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
• Let’s consider two glasses denoted A and B with 30 ml
mango juice and 50 ml orange juice. Find out the way to
exchange the juice in A and B.
Dr. Kuppusamy P
3. • Declare variables
• Initialize the values
Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
Dr. Kuppusamy P
4. Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
Move Glass A value 30 to Glass
Temp
Move Glass B value 50 to
Glass A
Dr. Kuppusamy P
5. Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
Move Glass Temp value 30 to
Glass B
Dr. Kuppusamy P
6. Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
Algorithm
Step 1. Start
Step 2. Declare two variables A, B and one more variable temp
Step 3. Read the positive numbers to A and B
Step 3. temp ← A
Step 4. A ← B
Step 5. B ← temp
Step 6. Display interchanged numbers in A , B
Step 7. Stop
Dr. Kuppusamy P
7. Problem Statement 1: Design an algorithm and
draw the flow chart to exchange two values.
Algorithm
Step 1. Start
Step 2. Declare two variables A, B and one more variable temp
Step 3. Read the positive numbers to A and B
Step 3. temp ← A
Step 4. A ← B
Step 5. B ← temp
Step 6. Display interchanged numbers in A , B
Step 7. Stop
Read the positive
numbers to A and B
temp ← A
A ← B
B ← temp
Display
interchanged
numbers in A , B
Stop
Start
Declare variables A, B,
temp
Dr. Kuppusamy P
8. Problem Statement 2: Design an algorithm
and draw the flow chart to exchange two
values without using temporary variable.
Algorithm
Step 1. Start
Step 2. Declare two variables A, B
Step 3. Read the positive numbers as A and B
Step 3. A ← A + B
Step 4. B ← A - B
Step 5. A ← A - B
Step 6. Display interchanged numbers in A , B
Step 7. Stop
E.g. A = 7, B= 5
A = 7 + 5 =12
B = 12-5 =7
A= 12 - 7 =5
Dr. Kuppusamy P
9. Problem Statement 2: Design an algorithm
and draw the flow chart to exchange two
values without using temporary variable.
Algorithm
Step 1. Start
Step 2. Declare two variables A, B
Step 3. Read the positive numbers as A and B
Step 3. A ← A + B
Step 4. B ← A - B
Step 5. A ← A - B
Step 6. Display interchanged numbers in A , B
Step 7. Stop
E.g. A = 7, B= 5
A = 7 + 5 =12
B = 12-5 =7
A= 12 - 7 =5
Read the positive
numbers to A and B
A ← A + B
B ← A - B
A ← A - B
Display
interchanged
numbers in A , B
Stop
Start
Declare variables A, B
Dr. Kuppusamy P
10. Problem Statement 3: Design an algorithm and draw
the flow chart to find the sum of individual digits for
the given non negative integer.
Step 1. Start
Step 2. Declare variable n
Step 3. Read non negative integer (n)
Step 4. Initialize result ← 0
Step 5. Repeat until n > 0
Step 5.1 result ← result + (n % 10)
Step 5.2 n ← n / 10
Step 6. Display result
Step 7. Stop
E.g. Iteration: 1
n = 76
result = 0 + (76 % 10)
n = 76/10
Iteration: 2
n = 7
result = 6 + (7 % 10)
n = 7/10
Dr. Kuppusamy P
11. Repeat
Yes No
Problem Statement 3: Design an algorithm and draw
the flow chart to find the sum of individual digits for
the given non negative integer.
Step 1. Start
Step 2. Declare variable n
Step 3. Read non negative integer (n)
Step 4. Initialize result ← 0
Step 5. Repeat until n > 0
Step 5.1 result ← result + (n % 10)
Step 5.2 n ← n / 10
Step 6. Display result
Step 7. Stop
Read the positive
number n
result ← result + (n % 10)
n ← n / 10
Display the result
Stop
Start
Declare variable n
Repeat until n >0
Dr. Kuppusamy P
12. Problem Statement 4: Design an algorithm and draw
the flow chart to find the sum of given set of n positive
integer.
Step 1. Start
Step 2. Declare variables n, sum, a, i
Step 3. Read total numbers value (n) and ‘n’ positive integers in array ‘a’
Step 4. Initialize sum ← 0 , i ← 0
Step 5. Repeat until i < n
Step 5.1 sum ← sum + a[i]
Step 5.2 i ← i + 1
Step 6. Display sum
Step 7. Stop
n = 3
a[] = {2, 3, 4}
sum = 0 , i=0
Iteration: 1
sum = 0 + 2
i = i +1
Iteration: 2
sum = 2 + 3
i = i +1
Iteration: 3
sum = 5 + 4
i = i +1
Dr. Kuppusamy P
13. Problem Statement 4: Design an algorithm and draw
the flow chart to find the sum of given set of n positive
integer.
Step 1. Start
Step 2. Declare variables n, sum, a
Step 3. Read total numbers value (n)
and ‘n’ positive integers in
array ‘a’
Step 4. Initialize sum ← 0, i ← 0,
Step 5. Repeat until i < n
Step 5.1 sum ← sum + a[i]
Step 5.2 i ← i + 1
Step 6. Display sum
Step 7. Stop
Repeat
Yes No
Read total numbers value (n)
and ‘n’ integers in array ‘a’
sum ← sum + a[i]
i ← i + 1
Display the sum
Stop
Start
Declare variable n, sum, a
Repeat until i <n
Dr. Kuppusamy P
14. Problem Statement 5: Design an algorithm and draw
the flow chart to find the number of digits in given
number
Sample Input 1: 4582
Sample Output 1: The number of digits in 4582 is 4
Algorithm
Step 1. Start
Step 2. Declare variables
Step 3. Read the input
Step 4. Initialize count ← 0
Step 4. Repeat until n !=0
Step 5.1 count ← count + 1
Step 5.2 n ← n / 10
Step 6. Display count
Step 7. End
Iteration: 1
count = 0 + 1
n= 4582 /10
Iteration: 2
count = 1 + 1
n= 458 /10
Iteration: 3
count = 2 + 1
n= 45 /10
Iteration: 4
count = 3 + 1
n= 4 /10
Iteration: 5
n != 0 → False
Dr. Kuppusamy P
15. Problem Statement 6: Design an algorithm and draw
the flow chart to find Factorial computation
of given number
Sample Input 1: 4!
Sample Output 1: 24
Step 1. Begin
Step 2. Declare variables
Step 3. Read the decimal number n
Step 4. Initialize fact ← 1, i ← 1
Step 4. if (n != 0)
Step 4.1. Repeat until 1 to n
Step 4.1.1 fact ← fact * i
Step 4.1.2 increment i
Step 5. Display fact
Step 7. End
Iteration: 1
fact = 1 * 1
i = i +1
Iteration: 2
fact = 1 * 2
i = i +1
Iteration: 3
fact = 2 *3
i = i +1
Iteration: 4
fact = 6 * 4
i = i +1
Iteration: 5
i < =n → False
Dr. Kuppusamy P
16. Problem Statement 7: Design an algorithm and draw
the flow chart to find Sine function computation
Step 1. Begin.
Step 2. Read the input x and n
Step 3. Convert x value to radius using rad ← 3.14/180*x.
Step 4. Initialize sum ← rad, sign ← -1.
Step 5. Repeat for i ← 3 to n in increment by 2.
Step 5.1 power ← pow(rad, i).
Step 5.2 factorial ← factorial*(i-1)*i.
Step 5.3 result ← power / factorial.
Step 5.4 sum ← sum + (sign * result).
Step 5.5 k ← k*(-1).
Step 6. Display the sum as sine series result.
Step 7. End.
Dr. Kuppusamy P
Repeat
Yes No
Read the input x and n
power ← pow(rad, i).
factorial ← factorial*(i-1)*i.
result ← power / factorial.
sum ← sum + (sign * result).
k ← k*(-1)
Display the sum
Stop
Start
Declare variable
Repeat until i <n
Initialize sum, sign
17. Problem Statement 8: Design an algorithm and draw
the flow chart to generate Fibonacci sequence
Step 1. Begin
Step 2. Get Non-negative decimal limit n
Step 3. Initialize num1 ← 0, num2 ← 1
Step 4. Display num1 and num2
Step 5. Repeat until 1 to n- 2
Step 5.1 num3 ← num1 + num2
Step 5.2 Display num3
Step 5.3 num1 ← num2
Step 5.4 num2 ← num3
Step 6. End
Dr. Kuppusamy P
Input : 5
Output: 0, 1, 1, 2, 3, 5
18. Problem Statement 9: Design an algorithm and draw
the flow chart to Reverse the digits of an integer
Step 1. Begin
Step 2. Get Non-negative integer num
Step 3. Initialize reversed ← 0
Step 4. Repeat until num > 0
Step 4.1 digit ← n % 10
Step 4.2 reversed ← (reversed * 10) + digit;
Step 4.3 num ← num / 10
Step 5. Display reversed
Step 6. End
Dr. Kuppusamy P
Input : 598
Output: 895
19. Problem Statement 10: Design an algorithm and draw the flow chart to
Decimal to Binary conversion
Step 1. Begin
Step 2. Get a Positive decimal number (n)
Step 3. Initialize result as null
Step 4. if (n > 1)
Step 4.1. Repeat until n < 2
Step 4.1.1 Remainder ← n % 2
Step 4.1.2 Append remainder with result
Step 4.1.3 n ← n / 2
Step 5. Append n with result
Step 6. Display the reverse of result
Step 7. End
Dr. Kuppusamy P
Input : 3
Output: 011
Repeat
Yes No
Read integer n
Remainder ← n % 2
Append remainder with result
n ← n / 2
Append n with result
Display the reverse of
result
Stop
Start
Declare variable
Initialize result as null
Repeat until n<2
20. References
Dr. Kuppusamy P
• Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth
edition, 2017.
• https://www.sitesbay.com