OOP Assignment No. 1
OOP Assignment No. 1
Q1.Write a program that helps the user count his change. The program should ask how many
quarters the user has, then how many dimes, then how many nickels, then how many
pennies. Then the program should tell the user how much money he has, expressed in
dollars. If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over.
(This is essentially the definition of the / and % operators for integers.) Write a program
that asks the user how many eggs she has and then tells the user how many dozen eggs she
has and how many extra eggs are left over.
Q2.A gross of eggs is equal to 144 eggs. Extend your program so that it will tell the user how
many gross, how many dozen, and how many left over eggs she has. For example, if the
user says that she has 1342 eggs, then your program would respond with Your number of
eggs is 9 gross, 3 dozen, and 10 since 1342 is equal to 9*144 + 3*12 + 10.
Q3.How many times do you have to roll a pair of dice before they come up snake eyes? You
could do the experiment by rolling the dice by hand. Write a computer program that
simulates the experiment. The program should report the number of rolls that it makes
before the dice come up snake eyes. (Note: “Snake eyes” means that both dice show a value
of 1.)
Q4.Which integer between 1 and 10000 has the largest number of divisors, and how many
divisors does it have? Write a program to find the answers and print out the results. It is
possible that several integers in this range have the same, maximum number of divisors.
Your program only has to print out one of them. You might need some hints about how to
find a maximum value. The basic idea is to go through all the integers, keeping track of the
largest number of divisors that you’ve seen so far. Also, keep track of the integer that had
that number of divisors.
Q5.Suppose that a file contains information about sales figures for a company in various cities.
Each line of the file contains a city name, followed by a colon (:) followed by the data
forthat city. The data is a number of type double. However, for some cities, no data was
available. In these lines, the data is replaced by a comment explaining why the data is
missing. For example, several lines from the file might look like:
Q6.Write a program that will compute and print the total sales from all the cities together. A
store offers discounts based on the total purchase amount:
Less than $100: No discount.
University of Central Punjab
Object Oriented programming
Assignment
Assignment No. 1 Class BSCS
Ensure that the program also calculates and displays the final amount after applying the
discount. Note: How would you implement a nested if structure to handle these conditions
efficiently?
Q7.Write a program that determines if a number is odd or even without using the % operator.
Example:
Input: 4 → Output: "Even"
Input: 5 → Output: "Odd"
Hint: Use bitwise operators or subtraction logic in your if conditions.
Note: How would you use an alternative approach to replace the % operator?
Q10. Write a program that prints the first n rows of a Fibonacci Pattern.
Each row contains Fibonacci numbers starting from 0. For example:
Input: n = 5
Output:
Copy code
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377