Initialize an array number[] by the following values:3.52, 5.96, 2.9, 1.06, 4.28, 3.13
Write a program in java that rounds off each number to the nearest integer
please answer this correctly.
Answers
Explanation:
Use the round(), floor() and ceil() static methods in the Math class. round() will round to the nearest integer, whilst floor() will round down and ceil() will round up.
float f = 9.3f;
Math.round(f); //Output is 9
Math.ceil(f); //Output is 10
Math.floor(f); //Output is 9
You may also be able to just cast the number to int:
int i = (int) 9.3f;
Answer:
An array can be one dimensional or it can be multidimensional also. When we invoke length of an array, it returns the number of rows in the array or the value of the leftmost dimension.
We can initialize an array using new keyword or using shortcut syntax which creates and initialize the array at the same time.
When we create an array using new operator, we need to provide its dimensions. For multidimensional arrays, we can provide all the dimensions or only the leftmost dimension of the array.
Let’s see some valid ways to initialize an array in java.