Description
Programming Assignment-IV
(Iterative Statements/Looping)
1. Write a java program to input a string message and display it 10 times in the following manner. Use a while loop. Let the string message be “Hello”.
Enter a message
Hello
1st Hello
2nd Hello
3rd Hello
4th Hello
5th Hello
6th Hello
7th Hello
8th Hello
9th Hello
10th Hello
Hint: Use i % 10 and i % 100 to determine when to use st, nd, rd, or th for printing the ith Hello.
3. Write a java program that gets an integer from the user. Count from 0 to that number. Use a for loop to do it.
Count to: 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
4. Write a java program that gets three integers from the user. Count from the first number to the second number in increments of the third number. Use a for loop to do it.
Count from: 4
Count to: 13
Count by: 3
4 7 10 13
5. Write a java program that uses a for loop. With the loop, make the variable x go from -2 to 2, counting by 0.5. (This means that x can’t be an int.)
-2.0
-1.5
-1.0
-0.5
0.0 0.5 1.0 1.5
2.0
6. Write a java program that, using one for loop and one if statement, prints the integers from 1,000 to 2,000 with five integers per line. Hint: Use the % operation.
7. Write a java program that takes an integer N as a command-line argument, uses Math.random() to print N uniform random values between 0 and 1, and then prints their average value.
8. Write a java program to print the following output using loop.
1
121
1213121
121312141213121
1213121412131215121312141213121
9. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Write a java program to find the sum of all the multiples of 3 or 5 below 1000.
10. Write a java program to print the multiplication table of a number entered by the user.
Enter a no. for which you want to find the multiplication table: 8
8×1=8
8×2=16
8×3=24
8×4=32
8×5=40
8×6=48
8×7=56
8×8=64
8×9=72
8×10=80
11. Write a java program to find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
The sum of the squares of the first ten natural numbers is,
12 + 22 + … + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
12. Write a java program called FunctionGrowth that prints a table of the values log N, N, N log N, N2, N3, and 2N for N = 16, 32, 64, …, 2048. Use tabs ( characters) to line up columns.
13. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Write a java program to display each digit, starting with the rightmost digit.
Your program should also determine whether or not the number is divisible by 9. Test it on the following numbers:
n = 154368 n = 621594 n = 123456
Hint: Use the % operator to get each digit; then use / to remove that digit. So 154368 % 10 gives 8 and 154368 / 10 gives 15436. The next digit extracted should be 6, then 3 and so on.
14. Write a java program to print largest power of two less than or equal to N.
15. Write a java program to print the below given pattern using while loop as well as for loop in two different programs.
* * * * *
* * * * *
* * * * *
* * * * *
16. Write the java programs to print the following four patterns using for loop using four different programs.
(a) (b) (c) (d)
* 1 1 1 * * 1 2 2 2 2 3
* * * 1 2 3 3 3 3 4 5 6
* * * * 1 2 3 4 4 4 4 4 7 8 9 10
* * * * * 1 2 3 4 5 5 5 5 5 5 11 12 13 14 15
17. Write a java program to print the following pattern using nested loops.
* * * * * * * * * * 1
* * * * * * 2
* * * * 3
* * * * 4
* * * 5
* * * * 6
* * 7
* * * * 8
* * * 9 * * * * 10
***************
Reviews
There are no reviews yet.