0% found this document useful (0 votes)
0 views

Programming Assignment IV (1)

The document outlines a programming assignment focused on Java, specifically involving iterative statements and looping. It includes a series of tasks requiring the implementation of Java programs to perform various operations such as counting, generating patterns, and calculating mathematical functions. Each task specifies the expected input and output, guiding the student in their programming exercises.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming Assignment IV (1)

The document outlines a programming assignment focused on Java, specifically involving iterative statements and looping. It includes a series of tasks requiring the implementation of Java programs to perform various operations such as counting, generating patterns, and calculating mathematical functions. Each task specifies the expected input and output, guiding the student in their programming exercises.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Assignment-IV

(Programming using JAVA)


(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
Output:
1st Hello
2nd Hello
3rd Hello
4th Hello
5th Hello
6th Hello
7th Hello
8th Hello
9th Hello
10th Hello
2. Rewrite the above java program in such a way that it takes the number of lines to
print as a command-line argument. You may assume that the argument is less than
1000.
Hint: Use i % 10 and i % 100 to determine when to use st, nd, rd, or th for printing.

Page 1 of 8
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.
Count from: 4
Count to: 13
Count by: 3
Output: 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).

Page 2 of 8
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 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
8x1=8
8x2=16
8x3=24
8x4=32
8x5=40
8x6=48
8x7=56
8x8=64
8x9=72
8x10=80

Page 3 of 8
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 Function Growth 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 (\t 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.

Page 4 of 8
*****
*****
*****
*****
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
** 12 22 23
*** 123 333 456
**** 1234 4444 7 8 9 10
***** 12345 55555 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

Page 5 of 8
18. 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
Output:
0 1
1 2
2 4
3 8
4 16
5 32

19. 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.

20. Write a java program to compute the harmonic mean. The harmonic mean is
defined by

21. Write a java program to compute the sum of the first n terms (n>=1) of the series.
S=1-3+5-7+9- ………

22. Input a number n, write a java program to compute n factorial (written as n!)
where n>=0.

23. For a given x and a given n, write a java program to compute xn/n!.

24. 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.

Page 6 of 8
25. 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.

26. 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! +
……
27. 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, ......

28. 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

29. Write a java program that accepts a positive integer n and reverses the order of its
digits.

30. Write a java program that puts the binary representation of a positive integer N
into a String s.

31. Write a java program Checkerboard that takes one command-line argument N and
uses a loop within a loop to print out a two-dimensional N-by-N checkerboard pattern
with alternating spaces and asterisks.

32. 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.

Page 7 of 8
33. Write a java program to find the sum of the first n terms of the series
fs=0!+1!+2!+3!+........+n! (n>=0)

34. 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.

35. 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.

36. Write a java program that counts the no of digits in an integer.

37. Write a java program to compute the sum of the digits in an integer.

38. Write a java program to find all common prime divisors of two integers.

39. 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.

Page 8 of 8

You might also like