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

DSA Assignment-1

The document contains 10 questions about Java programming concepts and methods. It asks the student to write Java code to: 1) Count the number of times a number must be divided by 2 to become less than 2. 2) Calculate BMI based on weight and height and return the weight category. 3) Check if a number is a "spy number" where the sum and product of its digits are equal. 4) Generate all possible strings using a set of characters. 5) Calculate the sum of a number's digits until it is a single digit. 6) Check if a number is odd without using certain operators. 7) Find maximum/minimum values and their positions in an array. 8

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

DSA Assignment-1

The document contains 10 questions about Java programming concepts and methods. It asks the student to write Java code to: 1) Count the number of times a number must be divided by 2 to become less than 2. 2) Calculate BMI based on weight and height and return the weight category. 3) Check if a number is a "spy number" where the sum and product of its digits are equal. 4) Generate all possible strings using a set of characters. 5) Calculate the sum of a number's digits until it is a single digit. 6) Check if a number is odd without using certain operators. 7) Find maximum/minimum values and their positions in an array. 8

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSE 2001: Data Structure & Algorithms

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:

The positive integer greater than 2 from command line argument


is 67.
The number of times one must repeatedly divide this number by 2
before getting a value less than 2 is 5.

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:

Enter person Weight in kg: 68


Enter height of person in meter: 2
The person is Underweight.

Sample Run-2:

Enter person Weight in kg: 96


Enter height of person in meter: 1.4
The person is Obese.
Question-3:

Write a java program to check whether a number is a spy number or not.

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:

Enter a number: 1124


1124 is a spy number.

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:

For 3 characters ‘C’, ‘A’, ‘R’, the possible strings are


CAR
CRA
RCA
RAC
ACR
ARC

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.

public static int sum_Of_Digits(int n)

Example:

Let n = 9294
Sum = 9 + 2 + 9 + 4 = 24
Sum = 2 + 4 = 6

Sample Run:

Enter a number 9294


Sum of digits of 9294 until the number is a single digit is 6
Question-6:

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.

public static boolean isOdd(int n)

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

The sum of elements of the 2D-Array is 27

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:

public static double sumMajorDiagonal(double[][] m)

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:

Enter a 4-by-4 matrix row by row:

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:

public static double sumColumn(double[][] m, int columnIndex)

Write a java program that reads a 3-by-4 matrix and displays the sum of each column.

Sample Run:

Enter a 3-by-4 matrix row by row:


1.5 2 3 4
5.5 6 7 8
9.5 1 3 1
Sum of the elements at column 0 is 16.5
Sum of the elements at column 1 is 9.0
Sum of the elements at column 2 is 13.0
Sum of the elements at column 3 is 13.0

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

The largest row index: 2

The largest column index: 2

**********************

You might also like