Home > OS >  Factorial Calculator
Factorial Calculator

Time:12-14

How can I make the program to perform a new or repeat the operation or ask the user to input again a number and know the factorial of it.

import java.util.Scanner;

public class Loops {

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        int num, num2 = 1, x, i;

        System.out.println("-------------Factorial Calculator-------------");
        System.out.print("Enter a positive integer: ");
        num = input.nextInt();

        if (num > 0) {
            System.out.print(num   "! = ");
            for (i = 1; i <= num; i  ) {
                if(i < num){
                    System.out.print(i   " x ");
                    num2 = num2 * i;
                }
                if(i == num){
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of "   num   " is "   num2);
                    System.out.println();
                }
            }
        }else if (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
        }
    }
}

CodePudding user response:

Put the code that computes the factorial in a separate method and call it from within a loop in main().

import java.util.Scanner;

public class Loops {

    public static void printFactorial(int num) {
        int i = 0;
        int num2 = 0;
        if (num > 0) {
            System.out.print(num   "! = ");
            for (i = 1; i <= num; i  ) {
                if(i < num){
                    System.out.print(i   " x ");
                    num2 = num2 * i;
                }
                if(i == num){
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of "   num   " is "   num2);
                    System.out.println();
                }
            }
        }else if (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
            System.exit(0);
        }

    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int num = 0;
        System.out.println("-------------Factorial Calculator-------------");
        while ( true ) {
            System.out.print("Enter a positive integer: ");
            num = input.nextInt();
            printFactorial(num);
        }
    }
}

CodePudding user response:

Use a while loop

import java.util.Scanner;

public class Loops {
    static boolean readInput = true;
    public static void main(String[] args) {
        while(readInput) calculateFactorial();
    }
}

private static void calculateFactorial() {
    Scanner input = new Scanner(System.in);

    int num, num2 = 1, x, i;

    System.out.println("-------------Factorial Calculator-------------");
    System.out.print("Enter a positive integer: ");
    num = input.nextInt();
    if(num <= 0) {
        readInput = false;
        return;
    }

    if (num > 0) {
        System.out.print(num   "! = ");
        for (i = 1; i <= num; i  ) {
            if(i < num){
                System.out.print(i   " x ");
                num2 = num2 * i;
            }
            if(i == num){
                System.out.print(i);
                num2 = num2 * i;
                System.out.println("\nThe factorial of "   num   " is "   num2);
                System.out.println();
            }
        }
    }else if (num < 0) {
        System.out.println("Please input a valid integer. Program stopped.");
    }
}

CodePudding user response:

import java.util.Scanner;

public class Main {
public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    
    
    boolean runMore = true;
    
    while(runMore){
    
    

    int num, num2 = 1, x, i;

    System.out.println("-------------Factorial Calculator-------------");
    System.out.print("Enter a positive integer: ");
    num = input.nextInt();

    if (num > 0) {
        System.out.print(num   "! = ");
        for (i = 1; i <= num; i  ) {
            if(i < num){
                System.out.print(i   " x ");
                num2 = num2 * i;
            }
            if(i == num){
                System.out.print(i);
                num2 = num2 * i;
                System.out.println("\nThe factorial of "   num   " is "   num2);
                System.out.println();
            }
        }
    }else if (num < 0) {
        System.out.println("Please input a valid integer. Program stopped.");
    }
    
    System.out.println("Want to contine : Enter '1' for yes and '0' for no");
    int wantToContinue = input.nextInt();
    
    if(wantToContinue==0){
        
        runMore = false;
    }
    
    }
}
}
    
    

CodePudding user response:

use while loop when user not typing a positif number :

System.out.print("Enter a positive integer: ");
num = input.nextInt();
while(num<0) {
    System.out.println("Please input a valid integer. Program stopped.");
    num = input.nextInt();
}

code :

    import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int num, num2 = 1, x, i;

        System.out.println("-------------Factorial Calculator-------------");
        System.out.print("Enter a positive integer: ");
        num = input.nextInt();
        input.nextLine();
        while (num < 0) {
            System.out.println("Please input a valid integer. Program stopped.");
            System.out.print("Enter a positive integer: ");
            num = input.nextInt();
        }
        if (num > 0) {
            System.out.print(num   "! = ");
            for (i = 1; i <= num; i  ) {
                if (i < num) {
                    System.out.print(i   " x ");
                    num2 = num2 * i;
                }
                if (i == num) {
                    System.out.print(i);
                    num2 = num2 * i;
                    System.out.println("\nThe factorial of "   num   " is "   num2);
                    System.out.println();
                }
            }
        }
    }
}
  • Related