SUBJECT:- PROGRAMMING FOR PROBLEM SOLVING (BE01000121)
Unit 1- Flowchart and Algorithm
What is Flowchart and Algorithm? Differentiate between Algorithm and Flowchart.
Explain Various Symbol used in Flowchart.
Solution:
Flowchart:
A flowchart is graphical representation of sequence of any problem to be solved by computer
programming language. Following are the flowchart symbols:
Name Symbol Use in Flowchart
Oval Start/stop (Denotes the beginning or end of a program)
Denotes either an input operation or an output
Parallelogram
operation
Rectangle Denotes a process or calculation or expression
Denotes a decision or condition. The program should
Diamond
continue along one of two routes.
Arrow Denotes direction of logic flow in a program
Circle Flowchart Connector
Algorithm:
Algorithm is a finite sequence of well-defined steps for solving a problem in systemic
manner.
Differentiate between Algorithm and Flowchart.
Algorithm Flowchart
1 It is a finite sequence of well-defined 1 A flowchart is graphical
steps for solving a problem in systemic representation of sequence of any
manner. problem to be solved by computer
programming language.
2 It is a bit difficult to understand. 2 It is easy to understand.
3 It doesn't use any specific symbols. 3 It uses various kinds of symbols
which are interlinked with arrows.
Page 1
1. Write flowchart or algorithm to find area of a triangle.
Solution:
Algorithm:-
Step1: Start
Step 2: Input b,h
Step 3: calculate area=0.5*b*h
Step 4: print area
Step 5: Stop
Flowchart:
Page 2
2. Sketch flowchart and write an algorithm to convert seconds into hour, minute and second.
(Hint : 3700 seconds => 1 hr , 1 minute , 40 second)
Solution:
Algorithm:
Step1: Start
Step 2: Input second
Step 3: calculate h=second/3600
m= (second%3600)/60
s= (second%3600)%60
Step 4: print h ,m,s.
Step 5: Stop
Flowchart:
Page 3
3. Write an algorithm and flowchart to determine whether the given year is a leap year
or not.
Solution:
Algorithm:
Step1: Start
Step 2: Input year
Step 3: if ((year%400==0) || (year%4==0 && year%100!=0) )then goto next step otherwise goto
step 6.
Step 4: print “year is leap year.”
Step 5: stop
Step 6: print “year is not leap year.”
Step 7: stop
Flowchart:
Page 4
4. Write an algorithm for finding odd and even number from given two numbers.
Solution:
Algorithm:
Step1: Start
Step 2: Input a,b
Step 3: if a%2==0 then goto next step otherwise goto step 5.
Step 4: print “a is even.” then goto step 6.
Step 5: print “a is odd”
Step 6: if b%2==0 then goto next step otherwise goto step 8.
Step 7: print “b is even.” then goto step 9.
Step 8: print “b is odd”
Step 9: Stop
Flowchart:-
Page 5
5. Write an algorithm for finding largest Number out of three given Numbers. Draw the Flowchart
for finding largest Number out of three given Numbers.
Solution:
Algorithm:
Step-1: Input a, b, c
Step-2: if a > b then goto next step otherwise goto step 8
Step-3: if a > c then goto next step otherwise goto step 6
Step-4: print, “Max num is”, a
Step-5: stop
Step-6: print, “Max num is”, c
Step-7: stop
Step-8: if b > c then goto next step otherwise goto step 11
Step-9: print, “Max num is”, b
Step-10: stop
Step-11: print, “Max num is”, c
Step-12: stop
Flowchart:
Start
Input a, b, c
False True
Is (a>b)
True
Is (a>c) ?
Is (b>c)
False True False
Print ,”Max
Print ,”Max Print ,”Max num is”, a
Print ,”Max
num is”, b num is”, c
num is”, c
Stop
*********************************************************************************
Page 6
6. Write an algorithm and draw the flowchart to find root of equation: ax2 + bx + c.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read a,b,c
Step 3: d = b * b – 4 * a * c
Step 4: if d >= 0 then goto next step otherwise goto step 9
Step 5: x1 = ( - b + √d ) / 2 * a
Step 6: x2 = ( - b - √d ) / 2 * a
Step 7: print, x1, x2
Step 8: stop
Step 9: print, “Negative Roots possible.”
Step 10: stop
*********************************************************************************
Page 7
7. Write an algorithm and draw the flowchart to find factorial of given number.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: count=1, fact=1
Step 4: Repeat steps from 4 to step 6 until count<= num
Step 5: fact = fact * count
Step 6: count = count + 1
Step 7: print, “Ans =”, fact
Step 8: stop
Page 8
8. Write an algorithm and draw the flowchart to find sum of first N element.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: count=1, sum=0
Step 4: Repeat steps from 4 to step 6 until count<= num
Step 5: sum = sum + count
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
*********************************************************************************
Page 9
9. Write an algorithm and draw the flowchart to find sum of 10 elements read from the user.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: count = 1, sum = 0
Step 3: Repeat steps from 3 to step 6 until count<= 10
Step 4: Read num
Step 5: sum = sum + num
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
*********************************************************************************
PREPARED BY-JINAL ZALA Page 10
10. Write an algorithm and draw the flowchart to add first N odd numbers.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read n
Step 3: sum = 0, i = 1
Step 4: Repeat steps from 4 to step 6 until i <= 2*n
Step 5: sum = sum + i
Step 6: i = i + 2
Step 7: print, “Ans =”, sum
Step 8: stop
*********************************************************************************
PREPARED BY-JINAL ZALA Page 11
11. Write an algorithm and draw the flowchart to accept N numbers & count how many of them
where odd and also compute sum of all these odd numbers.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read N
Step 3: count = 1, sum = 0, odd = 0
Step 4: Repeat steps from 4 to step 9 until count<= N
Step 5: Read num
Step 6: if num % 2 = 1 then goto next step otherwise goto step 9
Step 7: odd = odd + 1
Step 8: sum = sum + num
Step 9: count = count + 1
Step 10: print, “Odd Count =”, odd, “Sum=”, sum
Step 11: Stop.
*********************************************************************************
PREPARED BY-JINAL ZALA Page 12
12. Write an algorithm and draw the flowchart to print sum of numbers between 1 to 100 which
are divisible by 3 and 5.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: count = 1, sum = 0
Step 3: Repeat steps from 3 to step 6 until count<= 100
Step 4: if count % 3 = 0 && count % 5 = 0 then goto next step otherwise goto step 6
Step 5: sum = sum + count
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
*********************************************************************************
PREPARED BY-JINAL ZALA Page 13
13. Write an algorithm and draw the flowchart to solve following series 1! + 2! + 3! + …..+ n!
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read N
Step 3: count = 1, sum = 0, fact = 1
Step 4: Repeat steps from 4 to step 7 until count<= N
Step 5: fact = fact * count
Step 6: sum = sum + fact
Step 7: count = count + 1
Step 8: print, “Ans =”, sum
Step 9: stop
*********************************************************************************
Page 14
14. Write an algorithm and draw the flowchart to print first N Fibonacci numbers.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read N
Step 3: a = 0, b = 1
Step 4: if N < 0 then goto next step otherwise goto step 7
Step 5: print, “Invalid Number.”
Step 6: Stop
Step 7: print, b
Step 8: count = 2
Step 9: Repeat steps 9 to steps 14 until count < = N
Step 10: c = a + b
Step 11: print, c
Step 12: a = b
Step 13: b = c
Step 14: count = count + 1
Step 15: stop
*********************************************************************************
Page 15
15. Write an algorithm and draw the flowchart to print reverse a given number.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum * 10 + r
Step 7: num = num / 10
Step 8: print, “Ans =”, sum
Step 9: Stop
*********************************************************************************
Page 16
16. Write an algorithm and draw the flowchart to print sum of individual digits of a given number.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum + r
Step 7: num = num / 10
Step 8: print, “Ans =”, sum
Step 9: Stop
*********************************************************************************
Page 17
17. Write an algorithm and draw the flowchart to check whether given number is Palindrome or
not.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: temp = num, sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum*10 + r
Step 7: num = num / 10
Step 8: if sum = temp then goto next step otherwise goto step 11
Step 9: print, “Palindrome Number.”
Step 10: Stop
Step 11: print, “Not a Palindrome Number.”
Step 12: Stop
*********************************************************************************
Page 18
18. Write an algorithm and draw the flowchart to check whether given number is Armstrong
number or not.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: temp = num, sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum + r*r*r
Step 7: num = num / 10
Step 8: if sum = temp then goto next step otherwise goto step 11
Step 9: print, “Armstrong Number.”
Step 10: Stop
Step 11: print, “Not an Armstrong Number.”
Step 12: Stop
*********************************************************************************
Page 19
19. Write an algorithm and draw the flowchart to check whether given number is Prime number
or not.
Solution:
Flowchart:
Algorithm:
Step 1: Start
Step 2: Read num
Step 3: count = 2
Step 4: Repeat steps from 4 to steps 8 until count < num
Step 5: if num % count = 0 then goto next step otherwise goto step 8
Step 6: print, “Not a Prime Number.”
Step 7: Stop.
Step 8: count = count + 1
Step 9: print, “Prime Number.”
Step 10: Stop
*********************************************************************************
Page 20