Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

Students also viewed these Databases questions

Question

Explain the different types of marketing strategies.

Answered: 1 week ago