Assignment No 1 Oop
Assignment No 1 Oop
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.
“ Counting Change”
import java.util.Scanner;
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.)
import java.util.Random;
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.
public class LargestNumberOfDivisors {
// Function to count divisors of a number
public static int countDivisors(int n) {
int count = 0;
// Loop from 1 to the square root of n to find divisors
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
count++; // i is a divisor
if (i != n / i) {
count++; // n/i is also a divisor if i != n/i
}
}
}
return count;
}
public static void main(String[] args) {
int maxDivisors = 0;
int numberWithMaxDivisors = 0;
// Iterate through numbers from 1 to 10,000
for (int i = 1; i <= 10000; i++) {
int divisorsCount = countDivisors(i);
// If the current number has more divisors, update the result
if (divisorsCount > maxDivisors) {
maxDivisors = divisorsCount;
numberWithMaxDivisors = i;
}
}
// Output the number with the largest number of divisors
System.out.println("The number between 1 and 10000 with the most divisors is: "
+ numberWithMaxDivisors);
System.out.println("It has " + maxDivisors + " 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:
San Francisco: 19887.32
Chicago: no report received
New York: 298734.12
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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.
$100 to $500: 10% discount.
Above $500: 20% discount.
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?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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
11
235
8 13 21 34
55 89 144 233 377
import java.util.Scanner;