Professional Documents
Culture Documents
PROBLEM SOLVING
GE6151 - COMPUTER PROGRAMMING
Program
• Program is a collection of instructions that will perform
some task.
GE6151 - COMPUTER PROGRAMMING
• Identify the solution for the problem and divide it into small
task.
some task.
Problem
Analysis
Program
Program
Documentation Design
& Maintenance
Program
Development
GE6151 - COMPUTER PROGRAMMING
1. Algorithm
2. Flowchart
3. Pseudo code
GE6151 - COMPUTER PROGRAMMING
Algorithm
• Algorithm is a finite sequence of instructions required for
Characteristics
• The steps in the algorithm must be unambiguous
Representations
• Flowcharts
• Normal English
• Pseudo code etc,.
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1:
Step2:
Step3:
Step4:
Step5:
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1: Start
Step2:
Step3:
Step4:
Step5:
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1: Start
Step2: Read a, b
Step3:
Step4:
Step5:
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1: Start
Step2: Read a, b
Step3: Add the value of a with b and store the result in sum.
Step4:
Step5:
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1: Start
Step2: Read a, b
Step3: Add the value of a with b and store the result in sum.
Step4: Display the value of sum
Step5:
GE6151 - COMPUTER PROGRAMMING
Example 1
• Addition of two numbers
Step1: Start
Step2: Read a, b
Step3: Add the value of a with b and store the result in sum.
Step4: Display the value of sum
Step5: Stop
GE6151 - COMPUTER PROGRAMMING
Example 2
• Sum & Average of two numbers
Step1: Start
Step2: Read a, b
Step3: Add the value of a with b and store the result in sum.
Step4: Calculate average as sum/ 2
Step5: Display the value of sum and average
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Example 3
• Sum & Average of n numbers
Step1: Start
Step2: Read limit n value
Step3: Read all n values
Step3: Calculate sum of n values
Step4: Calculate average as sum/ n
Step5: Display the value of sum and average
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Example 4: Method 1
• Greatest among 3 numbers
GE6151 - COMPUTER PROGRAMMING
Example 4: Method 1
• Greatest among 3 numbers
Step1: Start
Step2: Read A, B, C
Step3:
Step4:
Step5:
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Example 4: Method 1
• Greatest among 3 numbers
Step1: Start
Step2: Read A, B, C
Step3: Compare A and B. If A is greatest perform step 4
else perform step 5
Step4: Compare A and C. If A is greatest, output “A is
greatest” else output “C is greatest”
Step5: Compare B and C. If B is greatest, output “B is
greatest” else output “C is greatest”
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Example 4: Method 2
• Greatest among 3 numbers
Step1: Start
Step2: Read A, B, C
Step3: Compare A and B. If A is greatest, store in MAX,
else store B in MAX
Step4: Compare MAX and C. If MAX is greatest, output
“MAX is greatest” else output “C is greatest”
Step5: Stop
GE6151 - COMPUTER PROGRAMMING
Step:4
Step:5
Step:6
Step:7 Stop
GE6151 - COMPUTER PROGRAMMING
Example – Assignment
Pseudocode
• Pseudo means imitates and code means instruction.
Characteristics of Pseudocode
• Composed of a sequence of statements or steps
• Statements are written in simple English
• Each statement is written on a separate line
• Keywords and indentation are used to signify control
structures or blocks of repetition
• There is no fixed syntax.
.
GE6151 - COMPUTER PROGRAMMING
Keywords
• Input : INPUT,READ,GET
• Output : PRINT,DISPLAY
• Calculation : COMPUTE,CALCULATE,ADD,
SUBTRACT,INITIALIZE
• Incrementing : INCREMENT
• Decrementing : DECREMENT
GE6151 - COMPUTER PROGRAMMING
program logic.
GE6151 - COMPUTER PROGRAMMING
Control Structures
• 3 types of Control Structures
i. Sequence
ii. Selection
iii. Iteration
GE6151 - COMPUTER PROGRAMMING
IF…THEN
Pseudocode Example
READ a,b
IF condition THEN
IF a>0
process 1
PRINT a is positive
.
ENDIF
.
END IF STOP
GE6151 - COMPUTER PROGRAMMING
IF…THEN…ELSE
Pseudocode Example
IF…THEN…ELSE
Pseudocode Example
CASE structure
Pseudocode
CASE Type
Case Type-1:
Process 1
Case Type-2:
Process 2
.
.
Case Type-n:
Process n
.
.
END CASE
GE6151 - COMPUTER PROGRAMMING
on some condition.
• Types:
• WHILE loop
WHILE Loop
Pseudocode Example
DO…WHILE Loop
Pseudocode Example
DO INITIALIZE sum=0
. INITIALIZE i=0
. DO
Body of the loop sum=sum+i
.
i=i+1
.
WHILE (condition) WHILE (i<100)
ENDWHILE ENDWHILE
PRINT sum
STOP
GE6151 - COMPUTER PROGRAMMING
Flowcharts
• It is the pictorial representation of the algorithm.
GE6151 - COMPUTER PROGRAMMING
Flowchart Symbols
• Terminal symbol
• It is used to represent the start, end of the program logic.
• Input/Output
• It is used for input or output.
• Process Symbol
• It is used to represent the calculations, data movements,
initialization operations etc,.
GE6151 - COMPUTER PROGRAMMING
• Decision Symbol
• Flow lines
• It is used to connect the symbols
• Connectors
• It is used to connect the flow lines.
GE6151 - COMPUTER PROGRAMMING
• It should be simple.
symbols.
GE6151 - COMPUTER PROGRAMMING
• Only one flow line should enter the process symbol and
START
STOP
GE6151 - COMPUTER PROGRAMMING
• Only one flow line should enter the decision symbol and
Benefits of Flowcharts
• Makes Logic Clear
• Communication
• Effective Analysis
• Useful in coding
Control Structures
• 3 types of Control Structures
i. Sequence
ii. Selection
iii. Iteration
GE6151 - COMPUTER PROGRAMMING
START
Read a,b
C=a*b
Print c
STOP
GE6151 - COMPUTER PROGRAMMING
Read a,b
yes
If a>b
no
Print a is Greater
Print b is Greater
Stop
GE6151 - COMPUTER PROGRAMMING
Read a,b,c
If yes
(a>b) and Print a
(a>c) Is largest
no
If yes
Print b
b>c
Is largest
no
Print c
Is largest
stop
GE6151 - COMPUTER PROGRAMMING
3. Iteration
Start
Sum=0
i=0
is no
i<100
yes
sum=sum+i
i=i+1
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
EXAMPLES
GE6151 - COMPUTER PROGRAMMING
Avg=(m1+m2+m3)/3
If yes Print
Avg>=60 First Class
No
yes
If Print
Avg>=50 Second Class
No
yes
If Print
Avg>=35 Third Class
No
Fail
stop
GE6151 - COMPUTER PROGRAMMING
Flow chart
Start
Read a,b,c
D=b*b-4*a*c
no
If D>=0
yes
Root1=[-b+sqrt(D)]/(2*a)
Root2=[-b+sqrt(D)]/(2*a) Print
roots are imaginary
Print root1,root2
Stop
GE6151 - COMPUTER PROGRAMMING
GE6151 - COMPUTER PROGRAMMING
Example
Start
Num=0
Num=Num+1
yes
while
Num<5
no
Print Num
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Set area
READ the r
COMPUTE area=3.14*r*r
PRINT area
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart
START
Read r
area=3.14*r*r
Print area
STOP
GE6151 - COMPUTER PROGRAMMING
Pseudocode
READ a, b, c
IF (a>b) and (a>c)
THEN
WRITE a is largest
ELSE IF (b>c) THEN
WRITE b is largest
ELSE
WRITE c is largest
ENDIF
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart
START
Read a,b,c
If yes
(a>b) and Print a
(a>c) Is largest
no
If yes
Print b
b>c
Is largest
no
Print c
Is largest
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Set root1,root2
READ the value of a, b, c
Find D b*b-4*a*c
IF D>=0 THEN
calculate root1=(-b+sqrt(D))/(2*a)
root2=(-b-sqrt(D))/(2*a)
ELSE
Roots are imaginary
END IF
WRITE root1,root2
Stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart
START
Read a, b
c=a
a=b
b=c
Print a, b
STOP
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart
START
Read a, b
a=a+b
b=a-b
a=a-b
Print a, b
STOP
GE6151 - COMPUTER PROGRAMMING
Pseudocode
READ year
IF year % 4 ==0 THEN
WRITE It is a Leap year
ELSE
WRITE It is not a Leap year
ENDIF
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart
Start
Read year
yes
year % 4 ==0
no
Print It is
a Leap year Print It is not a
Leap year
Stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart
Start
Read n
i=1
while no
i<=n
yes
fact=fact * i
i=i+1
Print fact
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
r = 0,sum=0
while no
n>0
yes
r=n%10
sum=sum + r
n=n/10
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
r = 0,sum=0
while no
n>0
yes
r=n%10
sum=sum *10 + r
n=n/10
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
Armstrong Number
Example: 153
13 +53 + 33 =153
GE6151 - COMPUTER PROGRAMMING
Finding an Armstrong Number
Algorithm
Step1: Start
Step2: Read the value of n and set a = n, sum = 0
Step3: While n>0 do
r=n%10
sum=sum + r*r*r
n=n/10
else Goto step5
Step4: Goto step 3
Step5: If a = sum then
Print Armstrong Number
Else
Print It is Not an Armstrong Number
Endif
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
READ the value of n and set a =n, sum=0
WHILE (n>0) do
r=n%10
sum=sum + r*r*r
n=n/10
ENDWHILE
Repeat the loop until condition fails
IF a=sum THEN
WRITE Armstrong Number
ELSE
WRITE It is not an Armstrong Number
ENDIF
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart Start
Read n
a = n,sum=0
while
n>0 no
yes
r=n%10
sum=sum + r*r*r
n=n/10
if
a=sum
Print It is Not an
Print Armstrong No Armstrong No
stop
GE6151 - COMPUTER PROGRAMMING
Fibonacci series
Example:
0 1 1 2 3 5 8 11….
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
f=0,f1= -1,f2=1
while no
f<n
yes
f=f1+f2
f1=f2
f2=f
Print f
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Set Fahrenheit
READ the Celsius
COMPUTE Fahrenheit = (1.8* Celsius) + 32
PRINT Fahrenheit
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart
START
Read Celsius
Print Fahrenheit
STOP
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Set Celsius
READ the Fahrenheit
COMPUTE Celsius =(Fahrenheit – 32)/1.8
PRINT Celsius
stop
GE6151 - COMPUTER PROGRAMMING
Flowchart
START
Read Fahrenheit
Print Celsius
STOP
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
sum=0,i=1
While i<=n
sum=sum+i
i=i+2
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
sum=0,i=0
While i<=n
sum=sum+i
i=i+2
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
Algorithm
Step1: Start
Step2: Read the value of n and set i = 0, sum = 0
Step3: While n>0 do
r=n%10
sum=sum + r*pow(2,i)
n=n/10
i=i+1
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
GE6151 - COMPUTER PROGRAMMING
Pseudocode
Flowchart Start
Read n
sum=0,i=0
While n>0
r=n%10
sum=sum + r*Pow(2,i)
n=n/10
i=i+1
Print sum
stop
GE6151 - COMPUTER PROGRAMMING
• Read n.
2. Initialize N=1.
3. Initialize sum S=0.
4. Calculate S=S+N.
5. Calculate N=N+1.
6. If N>n, then goto step 7 else goto step 4.
7. Write the sum S.
8. Stop.