The expected result is 0.5f but in this algorithm what I get is 0.0f. What am I doing wrong?
public class AlgoritmoDeSequencia
{
private int n;
public AlgoritmoDeSequencia()
{
this.n = n;
}
public void algoritmo()
{
n = 2;
float primario = 1/n;
System.out.println(primario);
}
}
You're dividing two whole numbers, and that completely eliminates the fractional part, since the result is a whole number.
In the method
algoritmo()
do the following:Considering santiagames ' answer , your algorithm should look like this:
Hello, your problem is that you are dividing two integers and, therefore, the result of the division is an integer.
Therefore, you will need at least one of the two (divisor or dividend) to be double or float.
try the following
A clearer example, taken from the web http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html
Cheers