The below code works accurate on int
public class Example {
public static void main(String[] args) {
int a = 3, b = 3, mul = 0;
// mul = a * b //this should not be used
for (int i = 1; i <= a; i++)
mul = mul + b;
System.out.println(a + "*" + b + "-->" + mul);
}
}
output : 3*3-->9 Now this is perfect
But when i used double :
public class Example {
public static void main(String[] args) {
double a = 3.5, b = 3.5, mul = 0;
// mul = a * b //this should not be used
for (int i = 1; i <= a; i++)
mul = mul + b;
System.out.println(a + "*" + b + "-->" + mul);
}
}
output:
3.5*3.5-->10.5 Now this is wrong since the correct answer of
3.5*3.5-->12.25
The problem is i need to multiply decimal values where as for loop wont support iteration over double value.