n >= 1 in the while condition returns no value

Asked

Viewed 101 times

1

Good night.

I’m starting to learn Java and I made a program with swing that calculates the factorial of a number. I did using while, and in the condition, I tried to do with the condition (n >= 1) (I will put the code snippet below), and the program returns 0 for all values. However, when I try with (n > 1) it works perfectly. I believe that this last multiplication by 1 is unnecessary, but it should be mathematically feasible, no?

int n = Integer.parseInt(txtN.getValue().toString()); //pego o valor colocado no spinner

int fat = n; // crio uma variável para calcular e atribuo o valor do spinner à ela

while (n >=  1){         //determino que vai parar quando for multiplicado por 1
     fat = fat *(n-1);  //multiplico fat por ele menos um para achar o fatorial
        n--; //decremento do valor da multiplicação             
 }
  • Note: another quick question. When I declared the variable as double, to calculate larger values, the last digits appeared in hexadecimal. Someone can explain why?
  • Also put the code with the double so we can understand what it is you tried to do that ended up producing these hexadecimal numbers.

1 answer

2

When n reaches 1, n - 1 is equal to 0 and any number multiplied by zero is equal to zero. Therefore fat is equal to 0 when you use n >= 1.

I did a search and found the answer to your second question. Java data types have limits and particularities. When a double number becomes large it becomes expressed in scientific notation, where E is the exponent of base 10 of scientific notation.

Here is a table of how JAVA shows factor numbers in different types:

n,          int,   Integer,   long,   float,   double
n = 1,   1,     1,              1,       1.0,      1.0
n = 2,   2,     2,              2,       2.0,      2.0
n = 3,   6,     6,              6,       6.0,      6.0
n = 4,   24,    24,          24,      24.0,    24.0
n = 5,   120,    120,      120,    120.0,   120.0
n = 6,   720,     720,      720,     720.0,    720.0
n = 7,   5040,    5040,      5040,     5040.0,    5040.0
n = 8,   40320,    40320,      40320,     40320.0,    40320.0
n = 9,   362880,    362880,      362880,     362880.0,    362880.0
n = 10,  3628800,    3628800,      3628800,     3628800.0,    3628800.0
n = 11,  39916800,    39916800,      39916800,     3.99168E7,    3.99168E7
n = 12,  479001600,    479001600,      479001600,     4.790016E8,    4.790016E8
n = 13,                  ,                      ,    6227020800,     6.2270208E9,   6.2270208E9
n = 14,                ,                     ,   87178291200,                      ,    8.71782912E10
n = 15,              ,                  ,  1307674368000,                   ,    1.307674368E12
n = 16,            ,                 ,  20922789888000,                  ,    2.0922789888E13
n = 17,          ,               ,  355687428096000,                ,    3.55687428096E14
n = 18,        ,             ,  6402373705728000,              ,    6.402373705728E15
n = 19,      ,           ,  121645100408832000,           ,    1.21645100408832E17
n = 20,    ,         ,  2432902008176640000,         ,    2.43290200817664E18
n = 21,  ,       ,                                          ,       ,    5.109094217170944E19

Source: http://webtutsdepot.com/factorial-table-and-the-associated-data-type-limits-in-java/

To display without scientific notation you can format the double number using printf with %f (see this page for more printf options: https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm )

Follow the code I’ve run to test:

public class Fatorial
{
    public static void main(String[] args)
    {
        double  numero = 11;
        double n = numero; //contador


        double fat = n; // crio uma variável para calcular e atribuo o valor do spinner à ela

        while (n > 1)
        {         
             fat = fat * (n-1);  
             n--; //decremento do valor da multiplicação             
         }


        System.out.println("O fatorial de " + numero + " é: " + fat + " (double padrão com notação científica)");
        System.out.printf("Fatorial Double formatado: %.0f\n", fat);

    }    

}

Output on console:

O fatorial de 11.0 é: 3.99168E7 (double padrão com notação científica)
Fatorial Double formatado: 39916800
  • Dude, worse than I did until a table test, I don’t know how I missed it. Thank you! I’ll leave it open in case someone answers the second :)

  • @Jhonatancruz updated the answer by answering his second question. I hope the explanation is satisfactory. :-)

  • Note: This n at the end is just to skip a line and the other console information is not glued.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.