PF Assignment
PF Assignment
Instructions
Solve the assignments and submit on the recommended platform. Discuss with your course mentor for
feedback and improvements. During the faculty connect session, the course mentor will communicate
how many and which assignments from the following set are expected to be uploaded on the learning
platform/shared location.
Assignments
Mandatory
1) Write down an algorithm that reads in a positive integer x and tests if it is a prime number.
2) Write down an algorithm tofind out smallest and largest amongst 3 elements.
3) Modify the above algorithm so that it will work with N elements where N is accepted from the
user.
4) Write down an algorithm to read in an integer between 0 and 99 and output the value in
English. For example, if the input is 28, it should output “twenty-eight”.
Recommended
5) Write down an algorithm to get the weight (in kilograms) and height (in meters) of a person.
Then calculate and print the Body Mass Index (BMI) according to the formula:
The BMI should be printed with 3 decimal places. You should choose an appropriate data types
and names for your variables.
6) Write down an algorithm to read in a temperature in Celsius and convert it to Fahrenheit. The
relationship between Celsius and Fahrenheit is
C = (F-32)*5/9.
7) Write down an algorithm to find out roots of a quadratic equation. Please note that roots can
be imaginary.
8) Write down an algorithm that reads in the number of seconds and converts it to hours,
minutes and seconds. A sample output of your program is as follows. Don’t bother with the
singular or plural forms of the nouns.
Please enter the number of seconds --> 500500 seconds = 0 hours 8 minutes 20 seconds
Hint: Use the / and % operators.
9) Write down an algorithm that reads in a mark of a student (which is an integer between 0 and
100) and prints the corresponding grades (A-F). The mark-to-grade conversion table is as
follows:
Grade A B C D F
Range ≥80 65-79 50-64 40-49 <40
1. Modify the program to allow user-specified grade boundaries. You may need to define
more variables.
2. Modify the program so that it checks if the input is between 0 and 100. If not, it should
ask the user to input again until the input is in the correct range. Use while statements.
3. Modify the program so that it repeats the above computation on 50 students. Use while
statements.
Draw histogram
Reverse an array
Insert element at nth position in an array. Please note it should insert and not overwrite.
13) A “password” file contains username and password. Write an algorithm to accept the
username and password and crosscheck it with the password file and display appropriate
message.
Display TOS
Check whether stack is full or empty. Let the stack be a stack of Characters.
15) Write down an algorithm to simulate a Queue. It should allow following operations on
Queue.
Display Contents of queue 4) Check whether queue is full or empty. Let the Queue be a Queue
of Employee where Employee is user defined data type.
16) Write down an algorithm to read a set of sentence from user till user enters a sentence
QUIT.
Find out following statistical data such as number of characters, words and sentences.
17) In above algorithm only perform following modification. Reverse ordering of sentences as
well as reverse ordering of character within a sentence.
18) Write an algorithm to perform an encryption as well as decryption of a file. Use any
algorithm to perform above said activity. Simplest possible algorithm is to read every character,
add 128 to it and stores it while performing an encryption and do reverse step while performing
a decryption.
20) Write down an algorithm to merge two sorted STRING arrays to create third sorted STRING
array.
21) Write down an algorithm to accept string consisting of anything between ‘o’ to ‘9’ and
convert it into corresponding integer. Don’t assume any inbuilt function.
String Length
Changing case
Reversing a string
23) A file contains AAAAABBCCCDD. Write an algorithm to read such file and find out run
length of every character in above file. For example, in above case A is appearing 5 times, B 2
times and so on. So the output should look like as follows:
A5B2C3D2
Also write down another program which will read above output and come out with original data
such as AAAAABBCCCDD. By the way this is very preliminary algorithm used in compression
and is known as RunLength Coding.
1.
1. 24) Task
You are to write an algorithm for a telephone company (Company A) to generate the phone bills
for its customers. The input to the program is a sequence of call records, one for each
customer. Each call record consists of a customer ID, followed by the start and end time of
each call made by that customer during the last month. The program should calculate the
charge on each call and then sum up the total for each customer.
To attract more customers, the company has promised to offer the cheapest rate compared with
2 other rival companies, B and C. That is, for each call, it will calculate the charges for all 3
companies and take the cheapest one. The rates for the 3 companies are shown in the
following table:
Company A $0.80/min
Company B $1.00/min during 9:00 – 21:00 $0.60/min during 21:00 – 9:00
Company C $0.90/min for the first hour $0.70/min for the rest
1.
1.
1. Your program should
2. Input the number of customers (at most 99999).
3. For each customer, input his/her call record consisting of:
* You cannot use array and self-defined function for this assignment.
1. Output Format
2. Time: Each time value is represented in the format hh:mm where hh is an integer
between 0 and 23, and mm is an integer between 0 and 59.
3. Call Duration: It should be printed in a field of width 7, right-justified.
4. Money: Each monetary value should be printed with a ‘$’ sign followed by a decimal
value in fixed-point notation with 2 decimal places. The decimal value should occupy a
field of width 7, right-justified.
Sample Input/Output
Observe carefully the special cases as well as the spacing/format of the input/output.