You are on page 1of 3

CSE 1001: Introduction to Computer Programming

Programming Assignment-V
(Fundamental Algorithms)

1. Given three variables of integer type A, B and C. Write a java program to exchange their
values using fourth temporary variable.

2. Write a java program to make the following exchanges without using the fourth
temporary variable.

A B C

(The arrows indicate that B is to assume the value of A, C the


value of B and so on)

3. Write a java program to make the following exchanges.

A B C D

(The arrows indicate A is to assume the value of B, B the value


of C, C the value of D and D the value of A)

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

5. Let the marks of the students in the above question be stored in a data file named as
“marks.dat”. Write a java program to read the marks until an end-of-file is encountered.
For this set of marks determine the total number of marks, the number of passes, and the
percentage of pass rate.
Hints: Code template

public class Student {

public static void main(String[] args) throws IOException {


……
/* Read the data from the file */

Scanner inFile=new Scanner (new FileReader ("marks.dat"));

/* Read the data values from the file until an EOF is


encountered*/

while (inFile.hasNextInt())
{

mark=inFile.nextInt();

}

inFile.close ();

}
}

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

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

8. Write a java program to compute the sum of the square of n numbers. That is,

   


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



∑ 
1 
10. Write a java program to generate the first n terms of the sequence without using
multiplication.

1 2 4 8 16 32 ...

11. Write a java program to prints out n values of the sequence

1 -1 1 -1 1 -1 ….

12. Write a java program to compute the sum of the first n terms (n>=1) of the series.

S=1-3+5-7+9- ………

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

14. For a given n, write a java program to compute 1/n!.

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

16. Write a java program to determine whether or not a number n is a factorial number.

17. For some integer n, write a java program to find the largest factorial number present as
factor in n.

18. Write a java program to simulate multiplication by addition. Your program should accept
as input two integers (they may be zero, positive, or negative).

**********

You might also like