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

Algorithms

The document contains 9 algorithms related to various mathematical and logical operations. The first algorithm converts kilograms to grams by multiplying the input kilograms by 1000. The second algorithm calculates the sum of the digits of a 5-digit input number by repeatedly taking remainders and adding them to the running sum. The third algorithm calculates the cost price of one item given the selling price and total profit of 15 items.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Algorithms

The document contains 9 algorithms related to various mathematical and logical operations. The first algorithm converts kilograms to grams by multiplying the input kilograms by 1000. The second algorithm calculates the sum of the digits of a 5-digit input number by repeatedly taking remainders and adding them to the running sum. The third algorithm calculates the cost price of one item given the selling price and total profit of 15 items.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Assignment

Assignment # : 1
Topic : Algorithm

Level 1: Easy

1.Write an algorithm to convert kilograms to grams.

Step 1: start
Step 2: Get the input kilograms
Step 3: Calculate grams = Kilograms * 1000
Step 4: Put the output grams
Step 5: Stop

2. If a five-digit number is input through the keyboard, write an algorithm


to calculate the sum of its digit.

Step 1: Start
Step 2: Get the input number, Assign sum = 0;
Step 3: Check number is less than 9999 and greater than 100000 if yes goto step 4
if not goto step 6
Step 4: Check whether the number not equal to zero if yes goto step 4.1 if not goto 5
Step 4.1 Calculate remainder = reminder of number divided by 10
Calculate Sum = Sum + Remainder
Calculate number = Number/10 goto step3
Step 5: Put the output sum
Step 6: Stop
3. If the selling price of 15 items and total profit earned on them is input
through the keyboard, write an algorithm to find the cost price of one item.

Step 1: Start
Step 2: Get the input selling price and profit
Step 3: Assign Total_items = 15
Step 4: Calculate cost price of 15 items as
Cost_price= selling price – profit
Step 5: Calculate cost price of one item as
Cost_price of one= Cost_price / Total_items
Step 6: Put cost_price of one
Step 7: Stop

4. Write an algorithm to check whether the given number is divisible by 5.

Step 1: Start
Step 2: Get the input number
Step 3: Check the remainder of number divided by 5 =0 if yes goto 3.1 otherwise goto 3.2
Step 3.1: Print “divisible by 5”
Step 3.2: Print “not divisible by 5”
Step 4: Stop

5. Write an algorithm to calculate your age.

Step 1: Start
Step 2: Get the input current_year and birth_year
Step 3: Calculate the age
Age = Current _year – Birth_year
Step 4: Put age
Step 5: Stop
6. Given the following array, write the algorithm to find the minimum value
in the array. Array = [9, 3, 0, 44, 26, 5, 67, 45, 89, 10]

Step 1: Start
Step 2: Initialize the value pos as 1 and n as 10
Step 3: Assign the smallest =100
Step 4: check whether pos less than n
If yes goto step: 4.1 if not goto step 5
Step 4.1: Get the array[pos].
Check array[pos] is less than smallest if yes goto 4.1.1
if not goto step: 4.2
Step 4.1.1: Assign smallest = array[pos]
Step 4.2: pos incremented by 1 goto step 4
Step 5: Put the value smallest
Step 6: Stop

7. A consumer consumes 500 watts per hour daily for one month. Write the
algorithm to calculate the total energy bill of that consumer if per unit rate
is 7?

Step 1: Start
Step 2: Get the energy
Step 3: Calculate Total energy = Energy*24*30
Step 4: Calculate amount = Unit*7
Step 5: Print amount
Step 6: Stop
8. Write the algorithm to find next element of the series 4, 16, 64, ...
Step 1: Start
Step 2: Get the input number
Assign initial=4
Step 3: Initialize value i as 1
Step 4: Check the i value is less then number if yes goto step 4.1 no goto step 5
Step 4.1: Put the initial
Calculate initial as
initial = initial *4
i incremented by 1 Goto step 4
Step 5: Stop

9. The length L and breadth B of a rectangle are passed as input. Write the
algorithm to calculate the perimeter of the rectangle and print the
perimeter as the output. Both L and B are positive integers. Input Format:
The first line denotes the length L. The second line denotes the breadth B.

Step 1:Start
Step 2: Get the input length and breadth
Step 3: Calculate perimeter as
Perimeter=2(length+breadth)
Step 4: Put the output perimeter
Step 5: Stop
Level 2: Medium
1. A person from USA wants to know his Body Mass index (BMI). He
knows his weight in pounds and height in inches. The evaluator knows the
formula for calculating BMI. BMI=(weight in kilograms)/(height in
m*height in m), 1 pound =0.45 kilogram and 1 inch =0.025m. Write an
algorithm to help the person in finding his BMI.

Step 1: Start
Step 2: Get the input weight_in_pounds and height_in_inches
Step 3: Assign 1 pound = 0.45kilogram
1 inch = 0.025m
Step 4: Calculate weight_in_kilogram as
Weight = 1 pound * weight_in _pounds
Calculate height_in_meters
Height= 1 inch*Heigh_ in_inches
Step 5: Calculate BMI as
BMI = Weight/(Height*Height)
Step 6: Put BMI
Step 7: Stop

2. If a five-digit number is input through the keyboard, write an algorithm


to obtain the sum of first and last digit of this number.

Step 1: Start
Step 2: Get the input number ,assign sum=0
Step 3: Check the number greater than 9999 and less than 100000 if yes goto step 3.1 if not
goto step 6
Step 3.1: Calculate first number as
First number = Number/10000
Calculate last number as
Last number = Reminder of number divided by 10
Step 4: Calculate sum as
Sum = First number + Last number
Step 5: Put sum
Step 6: Stop

3. A cloth showroom has announced the festival discount of 5% on the


purchase of items, based on the total cost of the items up to Rs.2000. Write
an algorithm to input the total cost and compute and display the amount to
be paid by the customer after availing the discount.

Step 1: Start
Step 2: Get input total cost,
Step 3: Check the total cost is less than or equal to 2000 Goto step 3.1
Step 3.1: Calculate discount of 5%
Discount =Total cost*0.05
Step 4: Calculate amount paid by customer after discount
Amount = total cost – Discount
Step 5: Put amount
Step 6 : Stop

4. Write an algorithm to print all the numbers from 1 to 100 that are
divisible by 5 and divisible by 10.

Step 1: Start
Step 2: Get input number
Step 3: Initialize i value as 1
Step 4: Check i value greater then or equal number if yes goto step 5 otherwise goto 4.1
Step 4.1: Check the reminder of i divided by 5 and divided by 10 is equal to 0
if yes goto step 4.1.1 otherwise goto step 4.1.2
Step 4.1.1: Put i
Step 4.1.2: i incremented by 1 goto step 4
Step 5: Stop
5. If the ages of Ram, Shyam and Hem are input from user, write an
algorithm to determine the youngest of the three.

Step 1: Start
Step 2: Get the input ages of Ram, Shyam and Hem
Step 3: Check whether the age of Ram is less than the ages of Shyam and Hem if yes goto
step 3.1 otherwise goto step 3.2
Step 3.1 : Print “Ram is youngest”
Step 3.2: Check the age of Shyam is less than the age Hem if yes goto 3.2.1 if not
3.2.2
Step 3.2.1: Print “Shaym is youngest”
Step 3.2.2: Print “Hem is youngest”
Step 4: Stop

6. Write an algorithm that reads the prices of a shop’s products


continuously until the user enters −1. The program should display the
minimum price, the maximum, and the average of those within [5,30],
before it terminates. Assume that none of the products costs more than
$100.

Step 1: Start
Step 2: Initialize value min as 100, Max = 0, Avg = 0, Sum =0, Count = 0,price=1
Step 3: Check the price is equal to -1 and greater than 100 goto step 7 if not goto step3.1
Step 3.1:Get the price
Step 4: Check whether min greater than the price if yes goto step 4.1
Step 4.1: Min = Price
Step 5: Check whether max less than the price goto step 5.1
Step 5.1: Max = Price
Step 6: Check whether the price is greater than 5 and less than 30 if yes goto step 6.1
If not goto step 3
Step 6.1: Assign sum = Sum+Price
Count = Count +1; Goto step 3
Step 7: Calculate average = Sum/count
Step 8: put min,max,average
Step 9: stop

7. Given an integer U denoting the amount of KWh units of electricity


consumed, the task is to write an algorithm to calculate the electricity bill
with the help of the below charges:
1 to 100 units – Rs. 10/unit
101 to 200 units – Rs. 15/unit
201 to 300 units – Rs. 20/unit
above 300 units – Rs. 25/unit

Step 1: Start
Step 2: Get the input total unit
Step 3: Check the expression of total unit
Step 3.1: Check total units between 1 to 100 then put cost = 10 goto step 4
if not goto step 3.2
Step 3.2: Check total unit between 101 to 200 then put cost = 15 goto step 4
if not goto Step 3.3
Step 3.3: Check total units between 201 to 300 then put cost = 20 goto step 4
if not goto 3.4
Step 3.4: Check total units above 300 put the cost = 25 goto step 4
Step 4: Calculate electricity bill as
Total cost = Total units * cost
Step 5: put total cost
Step 6: stop
Level 3: Hard

1.The cold faucet in the bath lets the water in at the rate of 12 liters per
minute. The hot faucet fills the bath at the rate of 10 liters per minute. The
plug hole lets the water out of the bath at the rate of 11 liters per minute.
The bath holds a maximum of 550 liters. I turn both faucets on, but forget
to put the plug in. Write an algorithm to find how many minutes does it
take for the bath to overflow?

Step 1: start
Step 2: Initialize cold as 12, hot as 10, release as 11, sum as 0, count as 0.
Step 3: Check the sum greater than capacity if yes goto step 4 if not goto step 3.1
Step 3.1: Calculate sum as
Sum=sum +cold+hot-release
Count incremented by 1 goto step 3
Step 4: put count
Step 5: stop

2. A secret locker is set by the Indian government for storing the defense
secrets. The locker can be opened if entered number is equal to sum of cube
of its individual digits. Write an algorithm to check the same.

Step 1: Start
Step 2: Get the Number, assigns sum = 0, Password =Number
Step 3: Check whether the given number not equal zero yes goto step 3.1 if not goto step 4
Step 3.1: Calculate Remainder as
remainder of number divided by 10
Step 3.2: Calculate cube as
Cube = Remainder * Remainder * Remainder
Step 3.3: Calculate sum as
Sum = Sum + Cube goto step 3
Step 4: Check whether the password is equal to the sum goto step 4.1
if not goto step 4.2
Step 4.1: Put “Password same”
Step 4.2: Put “Not same”
Step 5: stop

3. A store charges $12 per item if you buy less than 10 items. If you buy
between 10 and 99 items, the cost is $10 per item. If you buy 100 or more
items, the cost is $7 per item. Write an algorithm to prints the customer’s
name and total cost.

Step 1: Start
Step 2: Get input items , Customer name
Step 3: Check the expression items
Step 3.1: check items less than 10 if yes assign
One item as 12 and goto step 4 if not goto step 3.2
Step 3.2: Check items greater than 10 and items less than 100 if yes assign
One item as 10 and goto step 4 if not goto step 3.3
Step 3.3: Check items greater than100 if yes assign
One item as 7 and goto step 4
Step 4: Calculate total cost as Items * One item
Step 5: Print total cost
Step 6: Stop

4. Given a start number and end number, you need to write the algorithm
to display “ding” for every 5th number and “dong” for every 10th number.
Inputs: Starting number and Ending number
Outputs: Display “ding” for every 5th number and “dong” for every 10th
number

Step 1: Start
Step 2: Get the input start , end
Step 3: Initialize as count = 1,
Step 4: Check whether start greater than end if yes goto step 5 if not goto step 4.1
Step 4.1: Check the reminder of count divided by 10 is equal to 0 if yes goto step 4.1.1
if not goto step 4.2
4.1.1 print “dong”
Step 4.2: : Check the reminder of count divided by 5 is equal to 0 if yes goto 4.2.1 if
not goto 4.2.2
Step 4.2.1: Print “ding”
Step 4.2.2: Print i and goto step 4
Step 5: Stop

5. In the city of wonderland, there is a magic pond. Whenever someone dips


in and out of the pond with flowers in hand, the numbers of flowers double.
You need write an algorithm to find out how many times someone needs to
dip in and out of the pond to get a minimum of 100 flowers. A person starts
off with 1 flower.

Step 1: Start
Step 2: Initialize count as 0, start as 1
Step 3: check Start greater than or equal to 100
Step 3.1: Start = Start*2
Count = Count +1 then Goto step 3
Step 4: Stop

6. Electricity bill calculator


i. Calculate the bill for 30 days based on the given below data.
ii. There are 2 fans of 60W each. Usage of each fan is 6 hours per day.
iii. There are 3 lights of 40W each. Usage of each light is 8 hours per day.
iv. For the other electrical appliances, the total consumption per day is
3000W.
v. Cost of 1 unit is Rs.6

step 1: Start
step 2: Get the input days ,fancount ,lightcount, other
step 3: Calculate
fanusage = fancount * 60 *6
lightusage = lightcount *40 *8
step 4: Calculate total usage per day as
totalusage =fanusage + lightusag + other
Calculate total units in watts as
Units = totalusage/1000
Step 5: Calculate Total Amount as
Amount = Units * days *6
Step 6: put amount
Step 7: stop

7. A food delivery app is allowed to deliver food only at specified time slots.
If the order is placed in the allowed time slot, the order can be confirmed to
the user. If the order is outside the timeslot, it can be delivered only in the
next time slot. If the user still wants to place the order accepting the delay,
accept and confirm it.
Allowed time slots: 12 PM – 3 PM

Step 1: Start
Step 2: Get the time from customer
Step 3: assign yes as 1,no as 0
Step 4: Check whether the time greater than 12 and less than 15 if yes goto step 4.1
If not goto step 4.2
Step 4.1: put “confirm order”
Step 4.2: Get choice from user
Check choice==yes goto step 4.2.1 choice==no goto step 4.2.2
Step 4.2.1 put “confirm order”
Step 4.2.2 put “cancel order”
Step 5: stop

8. In an E-commerce website, there is an offer coupon for books worth Rs.


500 as a special promotion. There is also a separate discount for books, and
it is 10%. Apply the max discount between coupon offer and individual
discount for books in the final invoice. The user purchases the below book.
Write the algorithm to calculate the final bill amount to be paid by the user.
Harry potter book - 1 no. - Rs. 750

Step 1: Start
Step 2: Get the priceofbook
Step 3: Assign offercoupon as 500 ,
separatediscount as priceofbook * 0.10
step 4: check offercoupon less than separatediscount if yes goto step 4.1 if not goto step 4.2
step 4.1: Assume final as
final = separatediscount
step 4.2: final = offercoupon
step5: Calculate Amount as
Amount = priceofbook -final
Step 6 : Put Amount
Step 7: stop

You might also like