Frank
Frank

Reputation: 137

Factorial Issue

I am new at coding Java, but I need to write a program that is from integer 1 to n. The program would ask the user to enter a positive number and if it is not positive then it will ask for another number.

Once the positive integer is entered for n in the program, it shows how the n factorial is computed followed by the result.

So my program will show everything correct except the result, it is not multiplying all the numbers together. If someone can point me in the right direction on how to get this solved that would be great!

CODE:

import java.util.Scanner;
public class Problem5 {
   public static void main(String[] args){

   int n, i =1;
   Scanner kbd = new Scanner(System.in);

   System.out.print("Enter n: ");
   n = kbd.nextInt();

   while (n <= 0) {
      System.out.print("Enter n: ");
       n = kbd.nextInt();
   }
   for (i = 1; i <= n; i++){
      System.out.print( i+ "*");
   }

   System.out.print(" is " + n * i);

   }
}

Output:

Enter n: 5 1*2*3*4*5* is 30


As you can see for the result it should be 120 and not 30.

Upvotes: 2

Views: 167

Answers (4)

Frank
Frank

Reputation: 137

import java.util.Scanner;
public class Problem5 {
   public static void main(String[] args){

   int n, i =1;
   Scanner kbd = new Scanner(System.in);

   System.out.print("Enter n: ");
   n = kbd.nextInt();

   while (n <= 0) {
      System.out.print("Enter n: ");
       n = kbd.nextInt();
   }
   int result = 1;
   for (i = 1; i <= n; i++){
      System.out.print( i+ "*");
      result *= i;
   }

   System.out.print(" is " + result);

   }
}

Output:
    Enter n: 5
    1*2*3*4*5* is 120

Upvotes: 0

Sudhansu Choudhary
Sudhansu Choudhary

Reputation: 3360

You are not doing the calculation properly. You just show the end result of n*i`.

In the below solution, I've taken an int fact = 1 and I'm multiplying it with the value of i inside the for loop and assigning back the result to factvariable. That's the core part. Thats how you get 1*2*3...*n = n!

import java.util.Scanner;
public class SomeArrayQuestion {
    public static void main(String[] args) {

        int n, i = 1;
        Scanner kbd = new Scanner(System.in);

        System.out.print("Enter n: ");
        n = kbd.nextInt();

        while (n <= 0) {
            System.out.print("Enter n: ");
            n = kbd.nextInt();
        }
        int fact = 1;
        for (i = 1; i <= n; i++) {
            System.out.print(i + "*");
            fact = fact * i;
        }

        System.out.print(" is " + fact);

    }
}

Upvotes: 1

Christoph Sommer
Christoph Sommer

Reputation: 6943

Your program is doing exactly one computation ( " is " + n * i) and this computation is not doing a factorial. You probably want to do the multiplication more than once - and with different numbers.

Upvotes: 1

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Just change that part

for (i = 1; i <= n; i++){
   System.out.print( i+ "*");
}

System.out.print(" is " + n * i);

for

int result = 1;
for (i = 1; i <= n; i++){
   System.out.print( i+ "*");
   result *= i;
}

System.out.print(" is " + result);

Your last print was wrong as you simply multiplied n with i which is a simple multiplication and have nothing to do with factorial.

Upvotes: 3

Related Questions