DSA Assignment-1
DSA Assignment-1
Programming Assignment-I
(Java Primer)
Question-1:
Write a Java program that can take a positive integer greater than 2 as input and write out the
number of times one must repeatedly divide this number by 2 before getting a value less
than 2.
Sample Run:
Question-2:
The body mass index (BMI) is commonly used by health and nutrition professionals to estimate
human body fat in populations. It is computed by taking the individual's weight (mass) in
kilograms and dividing it by the square of their height in meters. i.e.
𝑾𝒆𝒊𝒈𝒉𝒕 (𝒌𝒈)
Metric: BMI = 𝟐 (𝒎𝟐 )
(𝑯𝒆𝒊𝒈𝒉𝒕)
Write a java program by using conditional statement to show the category for a given BMI.
Category BMI
Less than 18.5 Underweight
18.5 to 24.9 Normal Weight
25.0 to 29.9 Overweight
30.0 or more Obese
Sample Run-1:
Sample Run-2:
Example:
N = 132
Sum of digit of N = 1 + 3 + 2 = 6
Product of digit of N = 1 × 3 × 2 = 6
So, 132 is a spy number.
Sample Run:
Question-4:
Write a Java program that outputs all possible strings formed by using the characters 'c', 'a', 'r',
'b', 'o', and 'n' exactly once.
Example:
Question-5:
Write a java method to calculate the sum of digits of a given number until the number is a
single digit. The method signature is as follows.
Example:
Let n = 9294
Sum = 9 + 2 + 9 + 4 = 24
Sum = 2 + 4 = 6
Sample Run:
Write a Java method, isOdd( ), that takes an int i and returns true if and only if i is odd. Your
method can't use the multiplication, modulus, or division operators, The method signature is
as follows.
Sample Run:
Enter a number 37
37 is odd: true
Question-7:
Write a java program to find the maximum and minimum and how many times they both occur
in an array of n elements. Find out the positions where the maximum first occurs and the
minimum last occurs.
Sample Run:
Enter number of elements of Array: 5
Enter elements of the array: 12 14 12 14 13
Maximum element of Array is 14 and occurs 2 times.
Minimum element of Array is 12 and occurs 2 times.
First occurrence of maximum element is at position 2.
Last occurrence of minimum element is at position 3.
Question-8:
Write a java program to print M-by-N array in the tabular format. Also, display the sum of
elements of the array.
Sample Run:
Enter number of Row and Columns of 2D-Array: 3 3
Enter elements of 2D-Array: 1 2 3 2 3 4 3 4 5
The elements of 2D array are:
1 2 3
2 3 4
3 4 5
Question-9:
Write a method that sums all the numbers in the major diagonal in an n * n matrix of double
values using the following header:
Write a java program that reads a 4-by-4 matrix and displays the sum of all its elements on the
major diagonal.
Sample Run:
1 2 3 4.0
5 6.5 7 8
9 10 11 12
13 14 15 16
Sum of the elements in the major diagonal is 34.5
Question-10:
Write a method that returns the sum of all the elements in a specified column in a matrix
using the following header:
Write a java program that reads a 3-by-4 matrix and displays the sum of each column.
Sample Run:
Home Assignment
Question-1:
Write a Java program that takes as input three integers, a, b, and c, from the Java console
and determines if they can be used in a correct arithmetic formula (in the given order), like
“a + b = c,” “a = b − c,” or “a b = c.”∗
Question-2:
Write a Java program that takes all the lines input to standard input and writes them to
standard output in reverse order. That is, each line is output in the correct order, but the
ordering of the lines is reversed.
Question-3:
Write a Java program that takes two arrays a and b of length n storing int values, and returns
the dot product of a and b. That is, it returns an array c of length n such that c[i] = a[i] · b[i],
for i = 0, . . . , n − 1.
Question-4:
Write a method to add two matrices. The header of the method is as follows:
public static double[][] addMatrix(double[][] a, double[][] b)
In order to be added, the two matrices must have the same dimensions and the same or
compatible types of elements.
Question-5:
Write a java program that randomly fills in 0s and 1s into a 4-by-4 matrix, prints the matrix,
and finds the first row and column with the most 1s.
Sample run:
0 0 1 1
0 0 1 1
1 1 0 1
1 0 1 0
**********************