You are on page 1of 108

GE6151 - COMPUTER PROGRAMMING

PROBLEM SOLVING
GE6151 - COMPUTER PROGRAMMING

Program
• Program is a collection of instructions that will perform

some task.
GE6151 - COMPUTER PROGRAMMING

Problem Solving Steps


• Analyze the problem.

• Identify the solution for the problem and divide it into small

task.

• Algorithm has to be prepared.

• Based on the algorithm the program will be created.

• Program is fed into the computer

• Then it has to be executed.


GE6151 - COMPUTER PROGRAMMING

Characteristic of Good Program


• Integrity – Accuracy
• Clarity – Overall readability of the program
• Simplicity – Clarity & Accuracy are enhanced by keeping
things as simple as possible
• Efficiency – Execution speed & Efficient Memory
• Modularity – Large program is broken-down into subtask
or module
• Generality - Program should be as general as possible
GE6151 - COMPUTER PROGRAMMING

Program Development Cycle


• Program is a collection of instructions that will perform

some task.

• Instruction are 3 types,

 Instruction to accept the input data

 Instruction that will process the data

 Instruction to provide the output to user


GE6151 - COMPUTER PROGRAMMING

Program Development Cycle

Problem
Analysis

Program
Program
Documentation Design
& Maintenance

Program
Development
GE6151 - COMPUTER PROGRAMMING

Problem Solving Technique


• 3 ways to find solution to a given problem

1. Algorithm
2. Flowchart
3. Pseudo code
GE6151 - COMPUTER PROGRAMMING

Algorithm
• Algorithm is a finite sequence of instructions required for

producing the desired result.


GE6151 - COMPUTER PROGRAMMING

Characteristics
• The steps in the algorithm must be unambiguous

• It should be written in sequence

• Ensure that the algorithm will terminate

• It should conclude after a finite number of steps


GE6151 - COMPUTER PROGRAMMING

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

Example 5: Finding roots of the Quadratic equation


Step:1 Start
Step:2
Step:3

Step:4

Step:5
Step:6

Step:7 Stop
GE6151 - COMPUTER PROGRAMMING

Example 5: Finding roots of the Quadratic equation


Step:1 Start
Step:2 Enter the values of a,b,c
Step:3 Find the value of D Using the Formula,
D = b*b-4*a*c
Step:4 If D is greater than or equal to zero find 2
roots
root1(-b+sqrt(D))/(2*a)
root2(-b-sqrt(D))/(2*a)
Step:5 Print root1 & root2
Step:6 If D is less than zero, then print the roots
are imaginary
Step:7 Stop
GE6151 - COMPUTER PROGRAMMING

Example – Assignment

1. Find Greatest of 2 numbers


2. Find whether a given number is positive or negative
GE6151 - COMPUTER PROGRAMMING

Pseudocode
• Pseudo means imitates and code means instruction.

• It is formal design tool.

• It is also called Program Design Language.


GE6151 - COMPUTER PROGRAMMING

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

Guideline for writing Pseudocode


• Steps should be understandable

• Capitalize the keyword.

• Indent to show hierarchy.

• End multiple line structure etc,.


GE6151 - COMPUTER PROGRAMMING

Advantage & Disadvantage


• It can be easily modified

• It can be understood easily

• Compare to flowchart it is difficult to understand the

program logic.
GE6151 - COMPUTER PROGRAMMING

Control Structures
• 3 types of Control Structures

i. Sequence
ii. Selection
iii. Iteration
GE6151 - COMPUTER PROGRAMMING

1.Sequence control structure


Pseudocode Example

Process 1 READ A,B


Process 2 CALCULATE C=A+B
WRITE C
STOP
Process n
GE6151 - COMPUTER PROGRAMMING

2.SELECTION CONTROL STRUCTURE


• It is used for making decisions.
• It allows the program to make a choice from
alternative paths.
• TYPES:
• IF …THEN
• IF …THEN… ELSE
• CASE etc.,
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 condition THEN READ a, b, c


process 1 IF (a>b) and (a>c) THEN
.
. WRITE a is largest
ELSE ELSE IF (b>c) THEN
process 2 WRITE b is largest
.
.
ELSE
END IF WRITE c is largest
. ENDIF
.
stop
GE6151 - COMPUTER PROGRAMMING

IF…THEN…ELSE
Pseudocode Example

IF condition THEN READ a, b, c


process 1 IF (a>b) THEN
. ASSIGN a TO MAX
. ELSE
ELSE ASSIGN b TO MAX
process 2 ENDIF
.
IF MAX > c THEN
.
END IF PRINT MAX is greatest
. ELSE
. PRINT c is largest
ENDIF
STOP
GE6151 - COMPUTER PROGRAMMING

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

3.Iteration / Looping control structure


• It is used to execute some instructions several time based

on some condition.

• Types:

• WHILE loop

• Do…WHILE loop etc.,


GE6151 - COMPUTER PROGRAMMING

WHILE Loop
Pseudocode Example

WHILE (condition) INITIALIZE sum=0


. INITIALIZE i=0
. WHILE (i<100)
Body of the loop sum=sum+i
.
i=i+1
.
END WHILE ENDWHILE
PRINT sum
STOP
GE6151 - COMPUTER PROGRAMMING

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

• It is used to denote a decision to be made at that point

• Flow lines
• It is used to connect the symbols

• Connectors
• It is used to connect the flow lines.
GE6151 - COMPUTER PROGRAMMING

Guidelines for preparing flowcharts

• It should be simple.

• Standard symbols should be used.

• The flow lines should not intersect each others.

• In case of complex flowcharts use the connectors

symbols.
GE6151 - COMPUTER PROGRAMMING

• Only one flow line should enter the process symbol and

only one flow line should come out from a process


symbol.

• Only one flow line used with the terminal symbol.

START
STOP
GE6151 - COMPUTER PROGRAMMING

• Only one flow line should enter the decision symbol and

two or three flowlines may leave from the decision


symbol.
GE6151 - COMPUTER PROGRAMMING

Benefits of Flowcharts
• Makes Logic Clear

• Communication

• Effective Analysis

• Useful in coding

• Useful in Testing etc,.


GE6151 - COMPUTER PROGRAMMING

Control Structures
• 3 types of Control Structures

i. Sequence
ii. Selection
iii. Iteration
GE6151 - COMPUTER PROGRAMMING

1.Sequence: Multiply 2 numbers

START

Read a,b

C=a*b

Print c

STOP
GE6151 - COMPUTER PROGRAMMING

2.Selection: Greatest of 2 numbers


Start

Read a,b

yes
If a>b
no
Print a is Greater
Print b is Greater

Stop
GE6151 - COMPUTER PROGRAMMING

2.Selection: Greatest of 3 numbers


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

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

Grade of the student- Pseodocode


READ m1,m2,m3
COMPUTE Avg=(m1+m2+m3)/3
IF (Avg>60) THEN
PRINT First class
ELSE
IF (Avg>50) THEN
PRINT Second class
ELSE
IF (Avg>35) THEN
PRINT Third class
ELSE
PRINT Fail
ENDIF
STOP
GE6151 - COMPUTER PROGRAMMING
Grade of the student- Flowchart
start
Read m1,m2,m3

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

Finding roots of the Quadratic equation-Algorithm


Step:1 Start
Step:2 Enter the values of a,b,c
Step:3 Find the value of D Using the Formula,
D = b*b-4*a*c
Step:4 If D is greater than or equal to zero find 2
roots
root1(-b+sqrt(D))/(2*a)
root2(-b-sqrt(D))/(2*a)
Step:5 Print root1 & root2
Step:6 If D is less than zero, then print the roots
are imaginary
Step:7 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

Example: Finding the area of a circle


Algorithm
Step1: Start
Step2: Read the value of r
Step3: Calculate area = 3.14*r*r
Step4: Print area
Step5: 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

Find the largest among three Numbers


Algorithm
Step1: Start
Step2: Read the value of a, b, c
Step3: IF (a>b) and (a>c) THEN
print a is largest
ELSE IF (b>c) THEN
print b is largest
ELSE
print c is largest
Step4: 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

Swapping two variables


Algorithm
Step1: Start
Step2: Read the value of a, b
Step3: c = a
a=b
b=c
Step4: Print the value of a and b
Step5: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of a, b


To swap use
c=a
a=b
b=c
WRITE a, b
stop
GE6151 - COMPUTER PROGRAMMING

Flowchart
START

Read a, b

c=a
a=b
b=c

Print a, b

STOP
GE6151 - COMPUTER PROGRAMMING

Swapping two variables without using another


variable
Algorithm
Step1: Start
Step2: Read the value of a, b
Step3: a = a + b
b=a-b
a=a-b
Step4: Print the value of a and b
Step5: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of a, b


To swap use
a=a+b
b=a-b
a=a-b
WRITE a, b
stop
GE6151 - COMPUTER PROGRAMMING

Flowchart
START

Read a, b

a=a+b
b=a-b
a=a-b

Print a, b

STOP
GE6151 - COMPUTER PROGRAMMING

Finding the year is leap year or not


Algorithm
Step1: Start
Step2: Read the value of year
Step3: IF year % 4 ==0 THEN
print It is a Leap year
ELSE
print It is not a Leap year
Step4: 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

Finding the Factorial


Algorithm
Step1: Start
Step2: Read the value of n and set i =1
Step3: While i <= n do
fact =fact * i
i=i+1
else Goto step5
Step4: Goto step 3
Step5: print the value of fact
Step6: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set i =1


WHILE (i <= n) do
fact =fact * i
i=i+1
ENDWHILE
Repeat the loop until condition fails
WRITE fact
stop
GE6151 - COMPUTER PROGRAMMING

Flowchart
Start

Read n
i=1

while no
i<=n
yes
fact=fact * i
i=i+1

Print fact

stop
GE6151 - COMPUTER PROGRAMMING

Finding the Sum of the digits


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
n=n/10
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum + r
n=n/10
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
GE6151 - COMPUTER PROGRAMMING

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

Finding the Reverse of a Number


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 *10 + r
n=n/10
else Goto step5
Step4: Goto step 3
Step5: print the value of sum
Step6: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum *10 + r
n=n/10
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
GE6151 - COMPUTER PROGRAMMING

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

Finding the Fibonacci series


Algorithm
Step1: Start
Step2: Read the value of n and set f=0,f1=-1, f2=1
Step3: While (f<n) do
f=f1+f2
f1=f2
f2=f
Print f
else Goto step5
Step4: Goto step 3
Step5: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set f=0 ,f1=-1, f2=1


WHILE (f<n) do
f=f1+f2
f1=f2
f2=f
WRITE f
ENDWHILE
Repeat the loop until condition fails
stop
GE6151 - COMPUTER PROGRAMMING

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

Conversion of Celsius to Fahrenheit


Algorithm
Step1: Start
Step2: Read the value of Celsius
Step3: Fahrenheit = (1.8* Celsius) + 32
Step4: Print Fahrenheit
Step5: 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

Fahrenheit = (1.8* Celsius) + 32

Print Fahrenheit

STOP
GE6151 - COMPUTER PROGRAMMING

Conversion of Fahrenheit to Celsius


Algorithm
Step1: Start
Step2: Read the value of Fahrenheit
Step3:Calculate Celsius =(Fahrenheit – 32)/1.8
Step4: Print Celsius
Step5: 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

Celsius =(Fahrenheit – 32)/1.8

Print Celsius

STOP
GE6151 - COMPUTER PROGRAMMING

Finding the sum of odd number between 1 to n


Algorithm
Step1: Start
Step2: Read the value of n and set sum=0,i=1
Step3: While (i<=n) do
sum=sum+i
i=i+2
else Goto step5
Step4: Goto step 3
Step5: Print sum
Step6: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set sum=0,i=1


WHILE (i<=n) do
sum=sum+i
i=i+2
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
GE6151 - COMPUTER PROGRAMMING

Flowchart Start

Read n

sum=0,i=1

While i<=n

sum=sum+i
i=i+2

Print sum

stop
GE6151 - COMPUTER PROGRAMMING

Finding the sum of even number between 1 to n


Algorithm
Step1: Start
Step2: Read the value of n and set sum=0,i=0
Step3: While (i<=n) do
sum=sum+i
i=i+2
else Goto step 5
Step4: Goto step 3
Step5: Print sum
Step6: Stop
GE6151 - COMPUTER PROGRAMMING

Pseudocode

READ the value of n and set sum=0,i=0


WHILE (i<=n) do
sum=sum+i
i=i+2
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
GE6151 - COMPUTER PROGRAMMING

Flowchart Start

Read n

sum=0,i=0

While i<=n

sum=sum+i
i=i+2

Print sum

stop
GE6151 - COMPUTER PROGRAMMING

Conversion of Binary number to Decimal

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

READ the value of n and set i =0, sum=0


WHILE (n>0) do
r=n%10
sum=sum + r*pow(2,i)
n=n/10
i=i+1
ENDWHILE
Repeat the loop until condition fails
WRITE sum
stop
GE6151 - COMPUTER PROGRAMMING

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 the value of n.


2. i = 1 , SUM = 0
3. if ( i > n ) go to 7
4. S = S + i
5. i = i + 1
6. go to 3
7. Display the value of S
8. Stop
- See more at: http://simplecprogrammer.blogspot.in/2011/10/algorithm-
to-display-sum-of-n-natural.html#sthash.t2pKuj4b.dpuf

• 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.

You might also like