Question: Write a program that lets a user enter N and that outputs N! (meaning N*(N-1)*(N-2)*...*2*1). Hint: Initialize a variable totalValue to N, and use a

Write a program that lets a user enter N and that outputs N! (meaning N*(N-1)*(N-2)*...*2*1). Hint: Initialize a variable totalValue to N, and use a loop variable i that counts from N-1 down to 1.

I need help with my code please it is not running:

ATTEMPT ONE

import java.util.Scanner;

public class ElectionYears { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int totalVal = 0; int userInt = 0; // FIXME: Ask user to input an integer, store in userInt Scanner scnr = new Scanner(System.in); System.out.print("Enter user number: "); userInt = scnr.nextInt(); totalVal = userInt - 1; while(userInt >= 1){ totalVal = totalVal * userInt; System.out.println(userInt + "! is " + totalVal); } } }

ATTEMPT TWO:

import java.util.Scanner;

public class ElectionYears { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int totalVal = 0; int userInt = 0; // FIXME: Ask user to input an integer, store in userInt userInt= scnr.nextInt(); System.out.println("enter an integer"); totalVal = userInt; while(userInt >1){

++userInt;

totalVal = totalVal*userInt;

} System.out.println(userInt + "! is " + totalVal); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!