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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started