Ncert Solutions
Ncert Solutions
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;
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.)
if ( conditional statement)
statement list
else
statement list
– 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
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
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.
Ans:
a) Tip
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
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
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
Explanation
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
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
Explanation
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.
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
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.
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;
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.)
– Count is initialized
– Tested
– Incremented Repetition problems are shown as:
while ( condition statement)
statement list
Final Answer
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
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
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
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.