Chapter 4 - Introduction to problem solving
Draw a flowchart, write an algorithm and pseudocode for the following
1) Write an algorithm to find the square of a number.
Answer
Step 1. Start
Step 2. Input a number
Step 3. Square = number ** 2
Step 4. Print Square
Step 5. Stop
2) Draw a flowchart to solve the problem of a non-functioning light bulb.
1
3) Draw a flowchart for calculating grade from marks percentage.
4) Write an algorithm to double a number in two different ways: (i) n+n, (ii) 2x
Answer :-
Step 1. Start
Step 2. Input a number
Step 3. First = number + number
Step 4. Second = number * 2
Step 5. Print First
Step 6. Print Second
Step 7. Stop
2
5) Write an algorithm and draw a flowchart to determine if a student passed
the exam or not.( Note there are 4 subject papers and passing average is
50 or more.)
Answer :-
ALGORITHM
Step 1: Input M1, M2, M3, M4
Step 2: GRADE B (M1+ M2 +M3+M4)/4
Step 3: if (GRADE <50) then
Print "FAIL"
else
Print "PASS"
# end of if
FLOWCHART
3
6) Write pseudocode for following :-
(a) (b)
(a)
X = input "x"
Y = input "y"
Z = input "z"
If x is greater than y
If x is greater than z
Display "x is largest number"
Else
Display "z is greatest number"
Else
If y is greater than z
Display "y is largest number"
Else
Display "z is greatest number"
(b)
c=0
s=0
while c is less than 5
n = input a number
s equal to s + num
c equal to c + 1
avg = s / 5
display avg
4
7) write an algorithm to display the sum of two numbers entered by the user
using both pseudocode and flowchart
PSEUDOCODE
1. NUMBER s1, s2, sum.
2. INPUT s1.
3. INPUT s2.
4. sum=s1+s2.
5. OUTPUT sum.
ALGORITHM
FLOWCHART
5
8) To find the area and perimeter of a rectangle.
Answer:
ALGORITHM
Step 1. Start
Step2. Input side-length & breadth say L, B
Step3. AREA ← L*B
Step4. PERIMETER ← 2*(L+B)
Step5. Print AREA, PERIMETER
Step 6. Stop
PSEUDOCODE
Input L and B
Area=L*B;
Perimeter=2*(L+B);
Print area and perimeter
6
9) To calculate the area and the circumference of a circle
Answer:
ALGORITHM
Step 1. Start
Step 2. Set pi equal to 3.14
Step 3. Read r
Step 4. Calculate area = pi*r*r and circumference = 2*pi*r
Step 5. Print area and circumference
Step 6. Stop
PSEUDO CODE
Set pi equal to 3.14
Read r
Calculate area = pi*r*r and circumference = 2*pi*r
Print area and circumference
7
10) To calculate the simple interest
Answer:
ALGORITHM
Step 1. Start
Step 2. Read P, R, T
Step 3. Calculate SI = (P*R*T)/100
Step 4. Print SI
Step 5. Stop
PSEUDO CODE
Read P, R, T
Calculate SI = (P*R*T)/100
Print SI
8
11) To check whether a year is a leap year or not
Answer:
ALGORITHM
Step 1. Start
Step 2. Input year
Step 3. If year/4==0 print it is a leap year else print it is not a leap year
Step 4. Stop
PSEUDO CODE
Input year
If year/4==0 print it is a leap year else print it is not a leap year
9
12) To check if a number is a positive or negative number
Answer:
ALGORITHM:
Step 1: Start
Step 2: get a number
Step 3: if the number is < 0 print the negative number
Step 4: else the number is > 0 print a positive number
Step 5: Stop
PSEUDOCODE:
INPUT “Enter a number”
PRINT number
IF number > 0
PRINT positive number
ELSE
PRINT negative number
FLOWCHART
10
13) To check if a number is an odd or even number
Answer:
ALGORITHM:
Step 1: Start
Step 2: Get a number
Step 3: If number percent 2 == 0 print even number
Step 4: else print an odd number
Step 5: Stop
PSEUDOCODE:
INPUT “Enter a number N”
PRINT number
IF N/2 = 0
Print even number
ELSE
PRINT odd number
FLOWCHART:
11
14) To categorise a person as either child (<13), teenager (> 13 but <20) or
adult (20), based on age specified.
Answer
ALGORITHM FLOWCHART
1. Start
2. Input age
3. If age < 13
Print "Child"
Elif age >= 20
Print "adult"
Else
Print "teenager"
4. Stop
PSEUDOCODE
A = input "Age "
If a is smaller than 13
Display " Child "
Elif a is greater or equal 20
Display "adult"
Else
Display " teenager "
15) To print all natural numbers up to n
Answer:
ALGORITHM:
Step 1: Start
12
Step 2: Assign i = 1
Step 3: get a number n
Step 4: Repeat step 5 and 6 until i = n
Step 5: Print i
Step 6: compute i = i + 1
Step 7: Stop
PSEUDO CODE:
INPUT “Enter number”
PRINT number
Get i = 1
Repeat step 5 and 6 until i = n
PRINT i
Compute i = i + 1
FLOWCHART
16) To print n odd numbers.
Answer :-
ALGORITHM
1. Start
13
2. Input n
3. For I in range ( 1 , n)
If I % 2 != 0 :
Print I
4. Stop
PSEUDOCODE
N = input n
For I in range ( 1 to n)
If I is not divisible by 2
Display I
17) To print square of a number.
Answer :-
14
ALGORITHM
1. Start
2. Input a number
3. Square = number ** 2
4. Print Square
5. Stop
PSEUDOCODE
N = input "number "
Square = n * n
Display square
FLOWCHART
18) To accept 5 numbers and find their average.
Answer :-
ALGORITHM
1. Start
2. Input a , b, c , d, e
3. Sum a + b + c + d + e
4. Average Sum / 5
15
5. Print Average
6. Stop
PSEUDOCODE
A = input first number
B = input second number
C = input third number
D = input fourth number
E = input fifth number
Sum = a + b + c + d + e
Average = Sum / 5
Display Average
FLOWCHART
19) To accept numbers till the user enters and then find their average.
Answer :-
ALGORITHM FLOWCHART
1. Start
2. Sum = 0
3. Count = 0
4. While True
16
Input number
Sum += number
Count += 1
Input choice to quit enter yes
If choice == yes
Break
5. Average = Sum / count
6. Print average
7. Stop
PSEUDOCODE
Sum = 0
Count = 0
While True
N = input "Number "
Sum is equal to sum + number
Count is equal to count + number
Choice = input "to quit enter quit"
If choice is equal to quit
Break
Average = sum / count
Display Average
20) To print squares of first n numbers
ALGORITHM
Step 1. Start
Step 2. Input a number
Step 3. For I in range ( number +1 )
Square = I ** 2
17
Print Square
Step 4. Stop
PSEUDOCODE
N = input "a number"
For I in range 0 to n + 1
Square = I ** 2
Display square
FLOWCHART(Print i also)
21) To print the cube of a number
Answer
ALGORITHM
Step 1. Start
18
Step 2. Read the number A
Step 3. Cube = number ** A
Step 4. Display cube
Step 5. Stop
PSEUDOCODE
N = input a number
Cube = n ** 3
Display cube
FLOWCHART
22) To print cubes of first n numbers
Answer
ALGORITHM
Step 1. Start
19
Step 2. Input a number
Step 3. For I in range ( number +1 )
Cube = I ** 3
Print cube
Step 4. Stop
PSEUDOCODE
N = input "a number"
For I in range 0 to n + 1
Cube = I ** 3
Display cube
FLOWCHART:
23) To find the sum of n given numbers
Answer
ALGORITHM
Step 1. Start
20
Step 2. Sum = 0
Step 3. While True
Input number
Sum += number
Choice = input to quit enter quit
If choice == quit
Break
Step 4. Print Sum
Step 5. Stop
PSEUDOCODE
Sum = 0
While True
N = input a number
A sum equal to sum + n
Choice = input to quit enter quit
If the choice is to quit
Break
Display sum
FLOWCHART
24) To find factorial of a given number.
Answer :-
ALGORITHM
1. Start
21
2. Input number
3. Fact = 1
4. For I in range number + 1
Fact *= I
5. Print Fact
PSEUDOCODE
N = input a number
Fact is 1
For I in rang n +1
Fact equal to fact * I
Display Fact
FLOWCHART
25) Given the following pseudo code:
Use variables sum, product, number1, number2 of type real
display "Input two numbers"
accept number1, number2
sum number1 + number2 print "The sum is", sum
22
product number1* number2
print "The Product is ", product
end program
Draw a flow chart for the same and dry run the given pseudocode if it is
working
FLOWCHART
Dry Run:
INPUT NUMBER1 INPUT NUMBER1 SUM PRODUCT
12 10 22 120
26) Given the following pseudo code:
Use variables: choice, of the type character
ans, number1, number2, of type integer
display "choose one of the following"
display "m for multiply"
display "a for add"
23
display "s for subtract"
accept choice
display "input two numbers you want to use"
accept number1, number 2
if choice m then ans number1 number2
if choice a then ans number1 + number 21
if choices then ans number1 - number2
display ans
Draw a flow chart for the same and
dry run the given pseudocode if it is working
Dry Run:
CHOICE INPUT NUMBER1 INPUT NUMBER1 Ans/result
If choice = m then
100 50 150
choose OP = ’+’
If choice = m then
100 50 50
choose OP = ’-’
If choice = m then
100 50 5000
choose OP = ’*’
If choice = m then
100 50 2
choose OP = ’/’
27) Given the following pseudo code:
Use variables: mark of type integer
If mark 80, display "distinction”
If mark 60 and mark < 80, display "merit"
If mark> 40 and mark < 60, display "pass"
If mark < 40 display, "fail"
24
Draw a flow chart for the same and dry run the given pseudocode if it is
working fine.
Answer:
Start
Input mark
If
Yes Print
mark>=80 “distinction”
No
If
Yes
Print “merit” mark >=60
No
If
Yes
mark>=40 Print “pass”
No
Print “fail”
Stop
25
28) Given the following pseudo code:
Use variables: category of a type character
Display "input category"
Accept category
If category = "U
Display "insurance is not available"
Else If category = 'A' then
Display "insurance is double"
Else If category = "8" then
Display "insurance is normal"
Else If category= 'M' then
Display "insurance is medically dependent"
Else
Display "entry invalid"
Draw a flow chart for the same and dry run the given pseudocode if it is
working fine.
26
29) Given the following pseudo code:
Use variable: number of type real
DISPLAY "Type in a number or zero to stop"
ACCEPT number
WHILE number 0
Square number number
DISPLAY "The square of the number is", square
DISPLAY "Type in a number or zero to stop"
ACCEPT number
ENDWHILE
Draw a flow chart for the same and dry run the given pseudocode if it is
working fine.
27