Description
Programming Assignment-V
(Iterative Statements/Looping)
1. Write a java program that takes the value of N through keyboard and prints a table of the power of 2 that are less than or equal to 2N.
Enter a number
5
0 1
1 2
2 4
3 8
4 16
5 32
2. Given a set of n student’s examination marks (in the range 0 to 100). Write a java program to count the number of students that passed the examination. A pass is awarded for all marks of 40 and above.
3. Write a java program that displays all the numbers from 100 to 1,000, ten per line, that are divisible by 5 and 6. Numbers are separated by exactly one space.
4. Write a java program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. Here is a sample run:
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
5. Given a set of n numbers. Write a java program that adds these numbers and returns the resultant sum and compute the average. Assume n is greater than or equal to zero.
6. Write a java program to compute the harmonic mean. The harmonic mean is defined by
𝑛
𝐻
7. Write a java program to compute the sum of the first n terms (n>=1) of the series.
S=1-3+5-7+9- ………
8. Input a number n, write a java program to compute n factorial (written as n!) where n>=0.
9. For a given x and a given n, write a java program to compute xn/n!.
10. Write a java program to evaluate the function sin(x) as defined by the infinite series expansion.
sin (x) = x- x3/3! + x5/5! – x7/7! +…
The acceptable error for computation is 10-6.
11. Write a java program to evaluate the function cos(x) as defined by the infinite series expansion.
cos (x) =1- x2/2! + x4/4! – x6/6! +….
The acceptable error for computation is 10-6.
12. Assume that x is a positive variable of type double. Write a code fragment that uses the Taylor series expansion to set the value of sum to ex = 1 + x + x2/2! + x3/3! + ……
13. Write a java program to generate and print the first n terms of the Fibonacci sequence where n>=1.
The first few terms are:
0, 1, 1, 2, 3, 5, 8, 13, ……
Each term beyond the first two is derived from the sum of its two nearest predecessors i.e. a new term in the series (Except the first two) is found by the following formula.
new term=preceding term +term before the preceding term
Let us define: c as new term b as the preceding term
a as the term before the preceding term
So, c=b+a
Your program should handle for all positive values of n.
Example: If n=1, it will display as: Fibonacci Series is: 0
If n=2, it will display as: Fibonacci Series is: 0, 1
If n=3, it will display as: Fibonacci Series is: 0, 1, 1 ….
If n=10, it will display as: Fibonacci Series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
14. Write a java program to generate and print the first n terms of the Fibonacci numbers using an efficient algorithm. In this case, you need to find a pair of Fibonacci terms, in each iteration and display them and adjust the preceding term b and the term before the preceding term a. Your program should handle all positive values of n.
Example:
If n=10, it will display as: Fibonacci Series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
If n=11, it will display as: Fibonacci Series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
15. Suppose you save $100 each month into a savings account with the annual interest rate 5%. So, the monthly interest rate is 0.05 / 12 = 0.00417. After the first month, the value in the account becomes
100 * (1 + 0.00417) = 100.417
After the second month, the value in the account becomes
(100 + 100.417) * (1 + 0.00417) = 201.252
After the third month, the value in the account becomes
(100 + 201.252) * (1 + 0.00417) = 302.507
and so on.
Write a java program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 5), and the number of months (e.g., 6) and displays the amount in the savings account after the given month.
16. Write a java program that accepts a positive integer n and reverses the order of its digits.
17. Write a java program to compute the square root of a number using Newton’s method.
18. Using Newton’s method, write a java program that takes integers N and k as command-line arguments and prints the kth root of N.
19. Write a java program that puts the binary representation of a positive integer N into a String s.
20. Write a java program that reads an integer and displays all its smallest factors in increasing order. For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
21. Write a java program GCD that finds the greatest common divisor (gcd) of two integers using
Euclid’s algorithm, which is an iterative computation based on the following observation: if x is greater than y, then if y divides x, the gcd of x and y is y; otherwise, the gcd of x and y is the same as the gcd of x % y and y.
22. Write a java program to check a number n is prime or not. The number to be inputted through keyboard.
23. Write a java program called PrimeCounter that takes a command line argument N and finds the number of primes less than or equal to N.
24. Write a java program that takes a command-line argument N and prints out all integers less than or equal to N that can be expressed as the sum of two cubes in two different ways. In other words, find distinct positive integers a, b, c, and d such that a3 + b3 = c3 + d3. Use four nested for loops.
Homework
1. Write a java program that reads a list of numbers and makes a count of the number of negatives and the number of non-negative members in the set.
2. Write a java program to compute the sum of the square of n numbers. That is,
𝑛
𝑠 = ∑(𝑎𝑖)2
𝑖=1
3. Write a java program to generate the first n terms of the sequence without using multiplication.
1 2 4 8 16 32 ……….
4. Write a java program to prints out n values of the sequence
1 -1 1 -1 1 -1 ………
5. For a given n, write a java program to compute 1/n!.
6. Write a java program to determine whether or not a number n is a factorial number.
7. For some integer n, write a java program to find the largest factorial number present as factor in n.
9. Write a java program to find the sum of the first n terms of the series fs=0!+1!+2!+3!+……..+n! (n>=0)
10. The first few members of the Lucas sequence which is a variation on the Fibonacci sequence are:
1 3 4 7 11 18 29 ……….
Write a java program to generate the Lucas sequence.
11. . Given a=0, b=1 and c=1 are the first three numbers of some sequence. All other numbers in the sequence are generated from the sum of their three most recent predecessors. Write a java program to generate this sequence.
12. Given two numbers d and e are suspected of being consecutive members of the Fibonacci sequence. Write a java program that will refute or confirm this conjecture.
13. Write a java program to generate the sequence where each member is the sum of the adjacent factorials. f3=1! + 0! f4=2! + 1!
f5=3! + 2!
Note that by definition 0! = 1.
14. Write a java program that counts the no of digits in an integer.
15. Write a java program to compute the sum of the digits in an integer.
16. Write a java program that reads in a set of n single digits and converts them into a single decimal integer. For example, the program should convert the set of 5 digits {2, 7, 4, 9, 3} to the integer 27493.
17. Input an integer n. Write a java program that will find the smallest exact divisor other than one.
18. Write a java program to find the list of all exact divisors of a given positive integer n using an efficient algorithm.
19. For an integer in the range 1 to 100, write a java program to find the number that has the most divisors. Write a java program to compute GCD which does not use either division or mod operator.
20. Write a java program to compute the smallest common multiple (SCM) of two non-zero
positive integer’s n and p.
(Note: The SCM is defined as the smallest integer m such that n and p divide exactly into m.)
21. Write a java program to compute the smallest common divisor other than one of two positive non-zero integers.
22. Given some integer x, write a java program to compute the value of xn where n is a positive integer considerably greater than 1.
23. Input a number n. Write a java program to generate the nth member of the Fibonacci sequence using an efficient algorithm.
24. Write a java program to find all common prime divisors of two integers.
25. Amicable numbers are pair of numbers each of whose divisors add to the other number. Example: The smallest pair of amicable numbers is (220, 284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.
Note: 1 is included as a divisor but the numbers are not included as their own divisors.
Write a java program that tests whether a given pair of numbers is amicable numbers or not.
26. A perfect number is one whose divisors add up to the number.
Example: The first perfect number is 6, because 1, 2, and 3 are its proper divisors, and
1+2+3=6
Write a java program that prints all perfect numbers in between 1 and 500.
***************
Reviews
There are no reviews yet.