0% found this document useful (0 votes)
4 views30 pages

Introduction To Problem Solving

The document outlines a systematic approach to problem-solving in programming, detailing steps from problem identification to implementation and maintenance. It emphasizes the importance of algorithms, pseudocode, and flowcharts in developing effective solutions. Additionally, it provides examples of algorithms for various tasks, illustrating the process of breaking down problems into manageable components.

Uploaded by

aditipanchal193
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)
4 views30 pages

Introduction To Problem Solving

The document outlines a systematic approach to problem-solving in programming, detailing steps from problem identification to implementation and maintenance. It emphasizes the importance of algorithms, pseudocode, and flowcharts in developing effective solutions. Additionally, it provides examples of algorithms for various tasks, illustrating the process of breaking down problems into manageable components.

Uploaded by

aditipanchal193
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
You are on page 1/ 30

Introduction to problem solving

Good Program is efficient and effective method to solve


a problem
Step to solve a problem is
1.​Identify and analyse the problem
2.​Find the solution and develop algorithm to solve
3.​Code the solution in programming language
4.​Test and debug (removing bugs or errors) the
coded solution

It is cyclic method.

1.​Identify and Analyze the problem


a.​Understanding the problem well
b.​Analyse the problem
i.​ Identify components
ii.​ Identify relationship between components
2.​Developing of algorithm
a.​Think of possible solutions
i.​ Different ideas of solution
ii.​ Check you ideas and check feasibility
iii.​ Finally, identify the correct idea, the one
which is most appropriate
b.​Break the problem into modules
i.​ Break the problem into small modules
ii.​ Develop solution for each module
iii.​ Breaking down solutions into workable
algorithm
c.​Identify operations for solution
i.​ Programming language decide
ii.​ Inputs, outputs are decides
iii.​ Desired operations
iv.​ Simple structure for storing data and other
part algorithm
3.​Code the solution
a.​Write program in the programming language
for each solutions
b.​Follow flow of control of the programming
language:- conditional statements, looping,
function etc
c.​Develop an efficient, good speed, performance
effective program
4.​Test and debug the program
a.​Test by finding errors and rectify them
b.​Documentation of the program
i.​ Details about the program
ii.​ Help about how to use
iii.​ Troubleshoot of the program
5.​Implementation and Maintenance of project
a.​Implement the code
i.​ Copy the program
ii.​ Training of the user
iii.​ Complete, partial, incremental
b.​Maintain the project
i.​ Troubleshooting
ii.​ Incremental
Program decomposition - The process of breaking
down a big program or a complex program into a set of
smaller sub-program or sub-modules in order to
understand the problem well and later to implement it.
Q Describe the task of creating mobile application
Ans
Decomposition of the task
●​What kind app is to be creating
●​What are target audience, single or multi player
●​What type interface
●​What type graphics
●​What type audio
●​Which software/ platform to develop the app
●​Do user need navigation
●​Additional requirement like database, payzee etc
●​How to test

Algorithm , pseudocode and flowchart


The definite steps to solve a particular problem in
human understandable language is known as
Algorithm. It is closely related to the programming
language in which you will write the program. It may or
maynot be based on the programming language.
You must consider the following factors:
1.​ Each operation must be defined , what to do and
how to do
2.​Each operation must be effective or required
3.​Time or operation must be finite
Characteristics of Good Algorithm
1.​ Precise
2.​Uniqueness- Each step must be unique
3.​Finite - A program must be finite
4.​Specify the input
5.​Specify the output
6.​Process should not be very long

Use Step followed by number for each step, Start is


optional

Write the algorithm to make a cup of tea


Solution
Step 1: Start
Step 2: Take Water in a pan
Step 3: Place it on stove
Step 4: wait till it boils
Step 5: Add tea leaves
Step 6: Add the ginger or tea masala(optional)
Step 7: Add Milk
Step 8: Add Sugar as per your taste
Step 9: Boil
Step 10: Sieve and serve
Step 11:End

Assignment:
Write an algorithm to withdraw money from the bank
using a cheque.
Solution
Step 1: Start
Step 2: Fill the Cheque with amount to withdraw, proper
signature, put “self” in payee
Step 3: Go to the bank
Step 4: produce the cheque to the cashier
Step 5: Collect the cheque amount
Step 6: count and cheque the currency notes
Step 7: Leave the bank
Step 8: Stop

Write an algorithm to make a Demand Draft of rs 2000


from some other bank for jee entrance fees.
Solution
Step 1: Start
Step 2: visit the branch for DD
Step 3: Fill the Form and fill the cheque, put
“yourself” in payee
Step 4: produce form and cheque to the cashier, DD
fee to collected by bank
Step 5: collect the DD from the Bank
Step 6: Stop

Q Consider the following steps in of algorithm for going


to the school or college, Arrange them in chronological
order
1)​Take lunch Box
2)​Get off the bus
3)​Get Ready
4)​Take Bus
5)​Wake up
6)​Reach School or college
7)​Have Breakfast
8)​Make your bag

Solution
5→ 8→ 3→ 7→ 1→ 4→ 6→ 2
5→ 3→ 8→ 7→ 1→ 4→ 6→ 2
Computer based algorithm
Write an algorithm to find sum of 10, 20
How to write:
1 find the input, in the example the input are given
values
2. Find what process you need to do
3. Find out what to display
Solution
Step 1: Start
Step 2: A←10
Step 3: B←20
Step 4: C←A+B
Step 5: Print C
Step 6: Stop
Write the algorithm to accept 2 numbers from the user
and display their sum.
Analyse
1.​accept 2 numbers (input) for accept use read or
input or accept
2.​Sum (task or the process)
3.​display their sum(output)
Remember:
Use variables to store values given by user or
values calculated in the program
Read means which command is required in that
programming language to accept the data, Read is
not command , you can input
Algorithm
Step 1: Start
Step 2: Read A or Input A
Step 3: Read B or Input B
Step 4: Let C=A+B or C=A+B
Step 5: print C
Step 6: Stop

Q Write an algorithm to accept Length and Breadth of a


rectangle and find its area and perimeter.
Solution
Step 1: Start
Step 2: Read L
Step 3: Read B
Step 4: Let P←2*(L+B)
Step 5:Let A← L*B
Step 6: print P,A
Step 7:Stop
Can write as
Step 4: Let P=2*(L+B)
Step 5:print P
Step 6: Let A=L*B
Step 7: print A

Q Write an Algorithm to accept the name of a student


and marks in 5 subjects, find total marks and average
marks of the student.
Solution
Step 1: Start
Step 2: Input Name
Step 3: Input Sub1,Sub2, Sub3, Sub4, Sub5
Step 4: Let Total ← Sub1+Sub2+Sub3+Sub4+Sub5
Step 5: Let Avg←Total/5
Step 6: print “Total=”,Total
Step 7: print “Average=”,Avg
Step 8:Stop

It is also known as Pseudo Codes, not a particular


programming language. It is an artificial and informal
language to develop algorithms. It like a text.

Q Write a pseudo code to accept 3 number and find


their sum and product.
Solution
Input three number
Find sum
Find product
Print sum
Print product

Can write as
Input A,B,C
S=A+B+C
P=A*B*C
Print S, P
Can Write as
Input Three number as A,B,C
Find Sum as S=A+B+C
Find Product as P=A*B*c
Display Sum as Print S
Display Product as Print P

Q Write a pseudo code to accept marks of a student


and display “pass” if more 32 and “Fail” otherwise
Solution
Accept marks
If marks more than (>) 32 then
​ Display “Pass”
Else
​ Display “Fail”
FLOWCHART
FlowChart ([Link])
1.​Terminal box - start and stop or end
2.​Input-output box - accepting data or display
3.​Process box - calculation
WAP to accept 3 numbers and find their product and
sum
#placed in input-output box
a=int(input(“enter first no:”))
b=int(input(“enter second number:”))
c=int(input(“enter third number:”))
#process in process box
d=a+b+c
e=a*b*c
#output in input-output box
print(“Sum=”,d)
print(“Product=”,e)
DRAW a Flowchart to accept 2 numbers and display
their sum,difference,product,remainder(%),power(**)
and quotient(//)
4.​Decision box -
●​ diamond shape
●​ One input
●​ 2 output - True or yes, False or no
●​ 2 statement - one will be executed if true another
will be executed if false
#either of two print statements will be executed
if x>y: #when x is larger than y
print(x)
else: # when x<y or x is equal to y
print(y)

​ if x>=y: # when x is larger than y or equal to y


​ ​ print(x)
​ else:​ ​ #when y is larger than x
​ ​ print(y)

if x==y: # when x exactly equal to y


​ ​ print(x)
​ else:​ ​ #when y is greater or less than x
​ ​ print(y)
DRAW a flowchart to accept a number and check if it
is positive or negative.

Flowchart
Input-output box
●​ Parallelogram
●​ With single input and single output
●​ Can have words like read, get, input or accept to get
the value from the user and display, print, show to
display message or output to the user
●​ It cannot have 2 outputs
●​ It cannot be the first box of your flowchart -first box is
start
●​ It cannot be the last box of your flowchart - last box is
end
●​ It has be connected to flowchart

Flowchart
Draw to flowchart to accept a number and find if it even or
odd
Divisibility rules: there is no rule. Only divide and get the
remainder number(to be checked) by the number( by
which you want to check), if the remainder is zero number
is divisible by otherwise the number is not divisible.
Is 3535355353 is factor 567? Divide the number and if the
answer is zero it is divisible by 567 otherwise it is not
divisible by 567
-​ Factors
-​ Ranges between 1 to n
-​ >=1 and <=n
-​ Fixed
Eg 6 factor (1,2,3,6)
Prime number are those number which has only
2 factors (1 and number itself)
Perfect number are number whose twice the
number is equal to the sum factors
Eg 6
(1+2+3+6)=12
6*2==(1+2+3+6)
-​ Multiple
-​ Multiply with 1 to infinity
-​ Big pool of values
​ ​ Eg 6 multiple(6,12,18,24,30………..)
To check even divide by 2 and get the remainder
(n%2) if the remainder zero - even otherwise - odd
QDraw a flowchart to accept marks of the student in 5
subjects, find total and average and display “Pass” or
“Fail”
Multiple if
If n == 0:
​ Print “zero”
Else if n%2==0
​ Print “even”
Else
​ Print “odd”
QDraw a flowchart to accept 3 number and find the
larger ( use if) will be presented by Pranav Ahuja
QDraw a flowchart to accept price of an item and
calculate the discount
Price​ ​ ​ ​ ​ Discount
>=50000​​ ​ ​ ​ 10%
<50000 and >=20000​ ​ 5%
<20000​ ​ ​ ​ ​ 2%
Also calculate the net amount (price - discount)
Will be presented by Vidooshi
Flowchart
Q electricity bill/telephone bill/income tax (slab calculation)
0-200​ ​ .35 per unit
200-400​​ .55 per unit
400-600​​ .85 per unit
>600​ ​ 1.10 per unit
Add surcharge amount as 300rs
Unit consumed: 650 units
200​--​ .35=200x.35= 70​ ​ ​ 650-200=450
200​--​ .55=200x.55=110​​ ​ 450-200=250
200​--​ .85=200x.85=170​​ ​ 250-200=50
50​ --​ 1.10=50x1.1=55​ ​ ​ 50-50=0
(200x.35)+(200x.55)+(200x.85)+(unit-600)*1.1
Total 70+110+170+55= 405
Unit consumed 470 units
200​--​ .35=200x.35= 70​ ​ ​ 470-200=270
200​--​ .55=200x.55=110​​ ​ 270-200=70
70 -- .85=70x.85=59.5
(200x.35)+(200x.55)+(unit-400)*.85
total=70+110+59.5=239.5
Unit consumed 270 units
200​--​ .35=200x.35= 70​ ​ ​ 270-200=70
(200x.35)+(unit-200)x.55
70​ --​ .55=70x.55=38.5
total=70+38.5=108.5​
Unit consumed 100 units
100x.35=35

Q character recognition
Range A- Z Conditional statement : ch>=’A’ and ch<=’Z’​
Range a-z Conditional statement: ch>=’a’ and ch<=’z’
Range 0-9 conditional statement:ch>=’0’ and ch<=’9’
Other are special characters
What is the difference between ‘0’ and 0?
Ans
‘0’ is a character literal and has an ASCII value (48), you
can only 2 arithmetic calculations (+:concatenation,
*:repetition)
0 is a numeric literal and has no ASCII value, you can do
any arithmetic calculations.
enter a character:A
A is inputted
type of ch is <class 'str'>
>>>
===== RESTART: C:/Python/[Link]
==========================
enter a character:a
a is inputted
type of ch is <class 'str'>
>>>
========================== RESTART:
C:/Python/[Link] ==========================
enter a character:0
0 is inputted
type of ch is <class 'str'>
>>>
========================== RESTART:
C:/Python/[Link] ==========================
enter a character:+
+ is inputted
type of ch is <class 'str'>
Every character has a code called UNIcode or ASCII
Value
ASCII value of ‘A’ is 65
ASCII value of ‘a’ is 97
ASCII value of ‘0’ is 48
if ‘A’ > ‘a’:
​ print(“False”)
if ‘A’ == ‘a’:
​ print(“no”)
“ABCDEFHIJ” with “ABCDEGHIJ” system will stop
comparing at F and G

Flowchart : Questions
1.​ A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Draw a flowchart to ask users to enter marks and print the corresponding
grade.
2.​ Draw a flowchart to check whether a triangle is valid or not, when the three angles of the
triangle are entered by the user. A triangle is valid if the sum of all the three angles is
equal to 180 degrees.
3.​ Any year is input by the user. Draw a flowchart to determine whether the year is a leap
year or not.
If year % 400 ==0
if year %100 ==0
print(“leap year”)
else
print(“not a leap year)
Else if year %4==0
​ Print (“leap year”)
Else​
​ print(“not a leap year”)
4.​ In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary
and DA = 90% of basic salary.
If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500
and DA = 98% of basic salary.
If the employee's salary is input by the user
Draw a flowchart to find his gross salary.
5.​ Draw a flowchart to calculate the monthly telephone bills as per the following rule:
Minimum Rs. 200 for upto 100 calls.
Plus Rs. 0.60 per call for next 50 calls.
Plus Rs. 0.50 per call for next 50 calls.
Plus Rs. 0.40 per call for any call beyond 200 calls.
6.​ Draw a flowchart to find the roots of and quadratic equation of type ax2+bx+c where a is
not equal to zero.
7.​ Draw a flowchart to check whether a entered character is lowercase ( a to
z ) or uppercase ( A to Z ).
8.​ Draw a flowchart to to find those numbers which are divisible by 7 and multiple of 5,
between 1500 and 2700
9.​ The marks obtained by a student in 5 different subjects are input by the user. The
student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Draw a Flowchart to calculate the division obtained by the student.
10.​Any character is entered by the user; write a program to determine whether the
character entered is a capital letter, a small case letter, a digit or a special symbol. The
following table shows the range of ASCII values for various characters.
Characters ​ ​ ​ ASCII Values
A–Z​​ ​ ​ 65 – 90
a–z​ ​ ​ ​ 97 – 122
0–9​ ​ ​ ​ 48 – 57
special symbols ​ 0 - 47, 58 - 64, 91 - 96, 123 – 127

Flowchart: iteration
(told you - Sequence, selection, iteration)
-means repeated iteration
-looping
-performing the same instructions again
and again
- till the condition is True
- stops condition is False
-goes to the next statement
Iteration or looping statement of 2 type:
1.​ Entry control - checks the condition
before entering the loop, so the loop will
be executed if the condition is True (for
loop, while loop)
2.​ Exit control - checks the condition after
entering the loop, such loops are
executed at least once even condition is
false (do..while loop)

Draw a flowchart to display “Hello” 10 times


Using loop
A variable declared in your program to
perform loop, called loop variable
1.​ Counter loop - using loop variable for
counting purpose
2.​ Using the variable of loop
Use finger cuts as counter to count the
number of times whenever you speak
var a
a=1
a​ ​ ​ ​ display
1​ ​ ​ ​ Hello
2​ ​ ​ ​ Hello​
3​ ​ ​ ​ Hello
Step 1: take a variable a
Step 2: give initial value as 1
Step 3:check is a<=10 yes goto step 4 no
goto step 7
Step 4: print(“hello”)
Step 5:increment a by 1
Step 6 : go back step 3
Step 7:end

print(“hello”*10)
Draw a flowchart to display natural numbers
from 1 to 20
Step 1: a=1
Step 2: check a<=20 yes goto step 3 no
goto step 6
Step 3: print a
Step 4: increment a by 1 (a+=1)
Step 5: goto step 2
Step 6: end
Draw a flowchart to display even number
from 2 to 40
Step 1: a=2
Step 2: check a<=20 yes goto step 3 no
goto step 6
Step 3: print a
Step 4: increment a by 2 (a+=2)
Step 5: goto step 2
Step 6: end
Draw a flowchart to display natural numbers
from 50 to 1
Step 1: a=50
Step 2: check a>=1 yes goto step 3 no goto
step 6
Step 3: print a
Step 4:decrement a by 1 (a-=1) [step value]
Step 5: goto step 2
Step 6: end
Loop --
1.​ Forward
Initial < final (raise the value +or *)
condition >, >=,!=
2.​ backward
Initial > final (reduce the value - or /)
condition <,<=, !=
For loop
-​ Initial value, final value, step value
-​ Syntax
For variable in <intial, final,step>:
statement(s)
-​ It will executed from initial value,
changing by step value till it reaches
final value - 1
-​ One of the technique in the for loop is
using range() function -range() give a
range of value from initial, final -1 and
you can have step
1.​ Format 1:
range(value) - it will 0 to value -1
Eg
range(10) - it will return 0,1,2,3,4,5,6,7,8,9

2.​ Format 2
range(initial,final) - from initial - final -1
Eg
range(1,10) - 1,2,3,4,5,6,7,8,9
3.​ Format 3
range(initial,final,step) - from initial to
final-1 but changes value as per step
value
Eg
range(1,10,2) - 1,3,5,7,9
range(2,20,2)- 2,4,6,8,10,12,14,16,18
range(10,0,-1)- 10,9,8,7,6,5,4,3,2,1

for a in range(10):
​ print(a, end=”,”)
Output:
0,1,2,3,4,5,6,7,8,9,
WAP in Python to display natural numbers
from 1 to 20
for a in range(1,21):
​ print(a,end=”,”)

Natural no from 1 to 20
>>> for a in range(1,21):
​ print(a,end=",")
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,1
8,19,20,

Even number from 2 to 20


>>> for a in range(2,22,2):
​ print(a,end=",")

2,4,6,8,10,12,14,16,18,20,

Flowchart:iteration
Draw a flowchart to display odd numbers from 1 to 50 in
the reverse order
Draw a flowchart to accept n and display natural numbers
from 1 to n
Program:
n=int(input("enter the value of n:"))
for a in range(1,n+1):
print(a,end=",")

Even number
n=int(input("enter the value of n:"))
for a in range(1,n+1,2):
print(a,end=",")

Find Sum of natural numbers 1 to 10


Step 1: sum=0
Step 2: loop for generating natural numbers 1 to 10
Step 3:sum=sum +loop variable

Write a program to find sum of even number from 2 to n


n=int(input("enter the value of n:"))
sum=0 # variable to store sum of the numbers
for a in range(2,n+1,2):
sum=sum+a
print("Sum=",sum) #final answer
Because we need to display only the final value
Series:-
1.​Display series 1,2,3,4…. (print is inside the loop)
If given outside will display only the final variable
of the loop variable
2.​Sum series s=1+2+3+4 (print is outside the loop)
If given inside will display intermediate sum, as
well as final value at the end

You might also like