write
a program to accept 10 different decimal num.
bers in a single dimensional array (say A) Truncate
the fractional part of each number of the array A
and store their integer part in another arday (say B)
Answers
Answered by
3
Answer:
Anonymous has correctly said that casting a float to an integer drops the fractional part. However, not all languages play nice like this. Java forces you to jump through a few more hoops.
class MathTest
{
public static void main(String args[])
{
double a = 5.3;
int b = (int)Math.floor(a);
double c = a - b;
System.out.println("Integral = " + b);
System.out.println("Fractional = " + c);
}
}
Expect some error in the fractional part.
Edit : The above solution is for positive numbers only. To make it work for both, you can replace line 6 with
int b = a>0 ? (int)Math.floor(a) : (int)Math.ceil(a);
Similar questions
English,
4 months ago
Social Sciences,
4 months ago
Chemistry,
8 months ago
Science,
8 months ago
Political Science,
1 year ago
Biology,
1 year ago