0% found this document useful (0 votes)
7 views

Ncert Solutions

The document provides various pseudocode examples and algorithms for problem-solving in programming, including reading numbers, determining winners in games, calculating bills, and finding the largest or smallest numbers. It emphasizes the use of conditionals, loops, and basic programming constructs to solve problems effectively. Additionally, it explains the importance of pseudocode as a tool for planning algorithms before coding.

Uploaded by

tsodey444
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)
7 views

Ncert Solutions

The document provides various pseudocode examples and algorithms for problem-solving in programming, including reading numbers, determining winners in games, calculating bills, and finding the largest or smallest numbers. It emphasizes the use of conditionals, loops, and basic programming constructs to solve problems effectively. Additionally, it explains the importance of pseudocode as a tool for planning algorithms before coding.

Uploaded by

tsodey444
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/ 14

Chapter – 4

INTRODUCTION TO PROBLEM SOLVING

1. Write pseudocode that reads two numbers and divide one by another and display the
quotient.

Ans:

Tip
Pseudocode is not an actual programming language. It is an artificial language which helps
programmers develop algorithms.

Explanation:
Below are the steps which we need to follow:-

Start
Input a, b
Divide = a/b
Output Divide
Stop

Final Answer:
Pseudocode uses simple English languages for coding programs before being converted into
programming language.

2. Two friends decide who gets the last slice of a cake by flipping a coin five times. The first
person to win three flips wins the cake. An input of 1 means player 1 wins a flip, and a 2 means
player 2 wins a flip. Design an algorithm to determine who takes the cake?

Ans:
Tip:
a) A set of rules that are to be followed in solving a problem by a computer is known as
an algorithm.
b) Since the winner is decided on input from the user we have to use if conditions to
determine the winner
c) Additionally, we can add an else condition to reject any output other than 1 and 2.

Explanation
Create variables ‘p1’ and ‘p2’ to store how many times player1 and player2 has won
respectively. And variable ‘coin’ to store user input
For p1 < 3 and p2 < 3
INPUT coin
If coin == 1
p1 = p1 +1
else if coin ==2
p2 = p2 + 1
If p1 == 3
DISPLAY P1 TAKES THE CAKE
Else
DISPLAY P2 TAKES THE CAKE

Final Answer:
The above algorithm can be used to determine which player gets the cake based on who wins
three coin flips first. For loop is used to check if either of the players has already won 3 times.
And the if condition after the for loop is used to check which of the player has won three times.

3. Write the pseudocode to print all multiples of 5 between 10 and 25 (including both 10 and
25).

Ans:
Tip
Pseudocode can be broken down into five components:

a. Variables
b. Assignment
c. Input/output
d. Selection
e. Repetition

Explanation

 A variable has a name, a data type, and a value. There is a location in memory
associated with each variable. A variable can be called anything or be given any
name. It is considered good practice to use variable names that are relevant to
the task at hand.

 Assignment is the physical act of placing a value into a variable. Assignment can
be shown using set = 5;

set = num + set;


The left side is the variable a value is being stored in and the right side is where the
variable is being accessed. When a variable is assigned a value, the old value is written
over with the new value so the old value is gone. x = 5 does not mean that x is equal to
5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5.

 Input / Output both deal with an outside source (can be a user or another
program) receiving or giving information. An example would be assuming a fast
food restaurant is a program. A driver (user) would submit their order for a burger
and fries (input), they would then drive to the side window and pick up their
ordered meal (output.)

• Output – Write / display / print


• Input – Read / get / input

 Selection construct allows for a choice between performing an action and


skipping it. It is our conditional statements. Selection statements are written as
such:

if ( conditional statement)
statement list
else
statement list

 Repetition is a construct that allows instructions to be executed multiple times (IE


repeated). In a repetition problem

– Count is initialized
Tested
– Incremented Repetition problems are shown as:
while ( condition statement)
statement list

Final Answer

1. set i = 1
2. while i*5 <= 25
3. i=i+1
4. print i*5

4. Give an example of a loop that is to be executed a certain number of times.


Ans:
Tip
Loops repeat a set or sequence of instructions until a condition is met which stops the
loop.
Loops are of 3 types: for loop, while loop, and do-while loop
Explanation
Loop to print numbers from 1 to 10:
num = 1;
for (num = 1; num<=10; num++)
{
print num;
}
Final Answer
The above loop example uses for loop to print numbers between 1 and 10 including 1
and 10. The loop will run exactly 10 times and on each iteration the value of num will
increase by 1

5. Suppose you are collecting money for something. You need ` 200 in all. You ask your
parents, uncles and aunts as well as grandparents. Different people may give either ` 10, ` 20
or even ` 50. You will collect till the total becomes 200. Write the algorithm.
Ans:
Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as algorithm.
 You will need to use a loop to check if the total amount has reached 200 or not

Explanation
Algorithm:-

1. Create variable ‘total’ and ‘receive’ to store the total amount of money collected
and the money given by relative respectively.
2. Ask a relative to input money they are willing to give (10, 20, or 50) and store it in
the receive
3. Calculate total = total + receive
4. If total < 200 go to step 2, else go to step 5
5. END

Final Answer
The above algorithm will ask the user to input a number 10, 20 or 50 and store it in a
variable. It will then sum the received amount until the total reaches 200 and then will
end.

6. Write the pseudocode to print the bill depending upon the price and quantity of an item.
Also print Bill GST, which is the bill after adding 5% of tax in the total bill.
Ans:
Tip
Pseudocode is a detailed description of an algorithm that does not follow the syntax of
a specific programming language but is readable by all.
Explanation
Pseudocode:
Declare variables bill, gstbill, price and quantity.
Input the quantity of item and store it in quantity
Input price of item and store it in price
Bill = quantity * price
gstbill = bill + 0.05 * bill
Print bill
Print gstbill
FInal Answer
The above pseudocode can be used to make a program that asks the user to input the
price and quantity of an item. The program will then calculate the bill and bill with GST
and display the output.

7. Write pseudocode that will perform the following:


a) Read the marks of three subjects: Computer Science, Mathematics and Physics, out of 100
b) Calculate the aggregate marks
c) Calculate the percentage of marks

Ans:
a) Tip

 A pseudocode is detailed description of an algorithm that does not follow the


syntax of a specific programming language but is readable by all.
 You have to ask the user for marks of each subject separately for easy
clarification and storing of data.

Explanation
Pseudocode:
Declare variable phy, com and maths
INPUT com
INPUT phy
INPUT maths
Final Answer
The above pseudocode can be used to make a program that aggregates the marks of
three subjects.
b) Tip

 Pseudocode is a detailed description of an algorithm that does not follow syntax


of a specific programming language but is readable by all.
 Aggregate marks is the sum total of all marks

Explanation
Pseudocode:
Declare variable agg
Agg = phy + maths + com;
Final Answer
The above pseudocode can be used to make a program that aggregates the marks of
three subjects.
c) Tip

 Pseudocode is a detailed description of an algorithm that does not follow syntax


of a specific programming language but is readable by all.
 Percentage is calculated by aggregate marks divided by total marks multiplied by
100

Explanation
This Pseudocode is continued from (ii):
Declare variable perc
Perc = (aggregate / 300 ) X 100
Final Answer
The above pseudocode can be used to make a program that calculates the percentage
from aggregate marks.

8. Write an algorithm to find the greatest among two different numbers entered by the user.

Ans:
Tip
Algorithms are often created as functions which serve as a small programs that can be
referenced by larger programs.
Explanation

1. Start
2. Input variables a and b
3. Read variables a and b
4. If a > b
5. Display a is the largest number
6. Else
7. Display b is the largest number
8. Stop

Final Answer
Use of highly efficient algorithms ensures that the program run as fast as possible and
use less system resources.
9. Write an algorithm that performs the following:
Ask a user to enter a number. If the number is between 5 and 15, write the word GREEN. If
the number is between 15 and 25, write the word BLUE.if the number is between 25 and 35,
write the word ORANGE. If it is any other number, write that ALL COLOURS ARE
BEAUTIFUL.
Ans:
Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as an algorithm.
 We have to use if else statements to display different outputs depending upon
user input.

Explanation

1. Declare a variable choice


2. Ask the user to input a number and store it in variable choice
3. If choice >= 5 and choice <15 display GREEN
4. If choice >=15 and choice <25 display BLUE
5. If choice >=25 and choice <35 display ORANGE
6. Else display ALL COLOURS ARE BEAUTIFUL

Final Answer
The above algorithm describes the process to ask user to input a number. Depending
upon the input of the user it displays different color.

10. Write an algorithm that accepts four numbers as input and find the largest and smallest of
them.
Ans:
Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as algorithm.
 We have to use if else statements to compare the four numbers to determine the
largest and smallest.

Explanation
Declare variables num1, num2, num3, num4, min and max
Ask user to input 4 numbers and store it on variables num1, num2, num3 and num4
If num1 >= num2, set max = num1 and min = num2
If num3 > max, set max = num3
If num4 > max set max = num4
If num3 < min, set min = num3
If num4 < min, set min = num4
Display min and max
Final Answer
The above algorithm describes the process to ask user to input 4 numbers and it
compares the number to display the smallest and largest number of the 4 numbers
provided by the user.

11. Write an algorithm to display the total water bill charges of the month depending upon
the number of units consumed by the customer as per the following criteria:
• for the first 100 units @ 5 per unit
• for next 150 units @ 10 per unit
• more than 250 units @ 20 per unit
Also add meter charges of 75 per month to calculate
the total water bill .
Ans:
Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as algorithm.
 Since the charge has to be determined based on units consumed the total units
consumed has to divided into different sectors

Explanation

1. Declare variables units and bill


2. Ask user to input units and store it in variable units
3. If units <= 100, calculate bill = (units x 5) + 75
4. Else If units > 100 and units <= 250, calculate bill = (100 x 5) + ((units – 100) x
10) +75
5. Else calculate bill = (100 x 5) + (150 x 10) + ((units – 250) x 20) + 75
6. Display bill

Final Answer
The above algorithm describes the process to ask user to input total units of water
consumed in a month. It calculates the total bill depending upon the total units consume
and adds 75 per month to calculate total bill.

12. What are conditionals? When they are required in a program?


Ans:
Tip
Conditionals perform different actions depending on whether a programmer-specified
algebraic condition evaluates to true or false.
Explanation
Conditionals are those features of programming language which depends on operators
to evaluate an expression and executes instructions depending upon the outcome of
the evaluation. Conditionals make programming much more powerful and are used to
decide the flow of execution based on different conditions.
Final Answer
Conditional statement are usually used in imperative programming (programming that
change a program’s state) and conditional expression are used in functional
programming (programming where programs are conducted by applying and
composing functions).

14. Following is an algorithm for going to school or college. Can you suggest improvements
in this to include other options?
Reach_School_Algorithm
a) Wake up
b) Get ready
c) Take lunch box
d) Take bus
e) Get off the bus
f) Reach school or college

Ans:
Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as an algorithm.
 Since this is a subjective algorithm we can add any number of steps that you
think can improve the algorithm

Explanation
Complete algorithm after adding other options is as follows

a. Wake up
b. Brush teeth
c. Eat breakfast
d. Get ready
e. Take lunch box
f. Walk to bus stop
g. Take bus
h. Get off the bus
i. Reach school or college

Final Answer
The above algorithm adds point b, c, and f as improvements. These steps are added so
that the student is fresh and has eaten before going to school. It also adds the step
highlighting the part of the student walking to the bus stop which is a major part of
going to school or college.

15. Write a pseudocode to calculate the factorial of a number


( Hint: Factorial of 5, written as 5! =5 × 4× 3× 2×1) .

Ans:
Tip
Pseudocode can be broken down into five components:

a. Variables
b. Assignment
c. Input/output
d. Selection
e. Repetition

Explanation

 A variable has a name, a data type, and a value. There is a location in memory
associated with each variable. A variable can be called anything or be given any
name. It is considered good practice to use variable names that are relevant to
the task at hand.

 Assignment is the physical act of placing a value into a variable. Assignment can
be shown using set = 5;

set = num + set;


The left side is the variable a value is being stored in and the right side is where the
variable is being accessed. When a variable is assigned a value, the old value is written
over with the new value so the old value is gone. x = 5 does not mean that x is equal to
5; it means set the variable x to have the value 5. Give x the value 5, make x equal to 5.

 Input / Output both deal with an outside source (can be a user or another
program) receiving or giving information. An example would be assuming a fast
food restaurant is a program. A driver (user) would submit their order for a burger
and fries (input), they would then drive to the side window and pick up their
ordered meal (output.)

• Output – Write / display / print


• Input – Read / get / input

 Selection construct allows for a choice between performing an action and


skipping it. It is our conditional statements. Selection statements are written as
such:
if ( conditional statement)
statement list
else
statement list

 Repetition is a construct that allows instructions to be executed multiple times (IE


repeated). In a repetition problem

– Count is initialized
– Tested
– Incremented Repetition problems are shown as:
while ( condition statement)
statement list
Final Answer

1. Declare N and F as integer variable.


2. Initialize F=1.
3. Enter the value of N.
4. Check whether N>0, if not then F=1.
5. If yes then, F=F*N.
6. Decrease the value of N by 1.
7. Repeat step 4 and 5 until N=0.
8. Now print the value of F.

16. Draw a flowchart to check whether a given number is an Armstrong number. An


Armstrong number of three digits is an integer such that the sum of the cubes of its digits is
equal to the number itself.
For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Ans:
Tip
 A flowchart is a diagrammatical representation of algorithms. It has boxes of various
kinds and are connected by arrows.
Explanation
Flowchart to determine if given number is Armstrong number or not
Final Answer
The flowchart explains the process flow of asking the user to input a number. The
number is divided into digits and each digit is cubed and summed. The sum of the cube
of the digits is then compared to the number given by the user. And depending upon
the result of the comparison the number is then determined as Armstrong number or
not.
17. Following is an algorithm to classify numbers as
“Single Digit”, “Double Digit” or “Big”.
Classify_Numbers_Algo
INPUT Number
IF Number < 9
"Single Digit"
Else If Number < 99
"Double Digit"
Else
"Big"
Verify for (5, 9, 47, 99, 100 200) and correct the
algorithm if required.

Ans:
Tip

A set of rules that are to be followed in solving a problem by a computer is known as


algorithm.
Explanation

1. Verifying for 5, 5 is less than 9 so the output is Single Digit which is correct
2. Verifying for 9, 9 is not less than 9 so we compare it with 99. 9 is less than 99 so
the output is Double-Digit which is incorrect
3. Verifying for 47, 47 is not less than 9 so compare it with 99. 47 us less than 99 so
the output is double-digit which is correct
4. Verifying for 99, 99is not less than 9 so we compare it with 99. 99 is not less than
99 so the output is Big which is incorrect.
5. Verifying for 100, 100 s not less than 9 so we compare it with 99. 100 is not less
than 99 so the output is Big which is correct.
6. Verifying for 200, 200 s not less than 9 so we compare it with 99. 200 is not less
than 99 so the output is Big which is correct.

The above algorithm is incorrect in the case of number 9 and number 99 so we have to
modify the algorithm as follows.
INPUT number
IF number<= 9
“single digit”
ELSE IF number <= 99
“double digit”
ELSE
“big

FInal Answer
The algorithm provided was incorrect for the values 9 and 99 as it did not check for
value equal to the test values. The algorithm was corrected by changing the condition
from less than to less than or equal to.

18. For some calculations, we want an algorithm that accepts only positive integers
upto 100.
Accept_1to100_Algo
INPUT Number
IF (0<= Number) AND (Number <= 100)
ACCEPT
Else
REJECT
a) On what values will this algorithm fail?
b) Can you improve the algorithm?

Ans:
a) Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as algorithm.
 If conditions are used to check for conditions.

Explanation
The algorithm will fail for the value 0. Since the if a condition is true if the value of the
NUMBER is 0 it will accept the input. But the algorithm is supposed to accept numbers
between 1 and 100 only.
Final Answer
The algorithm will fail for the value 0. The algorithm is not supposed to accept value 0
as it is to accept positive integers up to 100 only. But the algorithm will accept 0 which
is not a positive integer.

b) Tip

 A set of rules that are to be followed in solving a problem by a computer is known


as an algorithm.
 If conditions are used to check for conditions.

Explanation
The algorithm can be improved as follows:
INPUT NUMBER
If (0 < NUMBER) AND (NUMBER <= 100)
ACCEPT
ELSE
REJECT
FInal Answer
The algorithm is improved than the given algorithm. The improved algorithm rejects
when the input value is 0. Since 0 is equal to 0 and not greater the if condition will be
false and will output the code from the else condition. As the else condition rejects the
input the algorithm fulfils its task.

Question 18, 4. Introduction to Problem Solving, Computer Science | Brainly

You might also like