Java 16.2 Practice Math Class. Can someone help me solve that please ? I have no idea how to do it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Java 16.2 Practice Math Class. Can someone help me solve that please ? I have no idea how to do it.

For your math class you need a program to calculate the factorial of a number. You're given a program which takes a number as input. Task Complete the program to calculate the factorial of the given number and output it. Sample input 6 Sample output 720 Explanation The factorial of a number is equal to the product of all numbers less than or equal to the given number. The factorial of 6 will be 6*5*4*3*2*1 = 720. Hint Use while loop to calculate the factorial of the number.

29th Dec 2021, 1:03 PM
malib
malib - avatar
6 Answers
+ 1
1. Declare a variable (int fact) and initialize it with 1. 2. Read a number from input whose factorial is to be found. Store it in a variable (int num). 3. Set the while loop to the condition (i <= num) where initial value of i = 1. 4. Inside the while loop, multiply the variable fact and variable i, and store the result in variable fact. Increment the loop variable i by 1 (i++). 5. Print the factorial. Ex: int fact = 1; int i = 1; while(i <= num){ fact *= i; i++; }
29th Dec 2021, 1:11 PM
Maharnab Saikia
Maharnab Saikia - avatar
+ 3
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int fact = 1; //your code goes here while(number>0){ fact *= number --; } System.out.println(fact); } }
11th Feb 2023, 6:29 PM
Ghulam Qambar Dobal
Ghulam Qambar Dobal - avatar
0
Thank you, it works ! int number = scanner.nextInt(); int fact = 1; int i = 1; while(i <= number){ fact*= i; i++; } System.out.println(fact);
29th Dec 2021, 2:07 PM
malib
malib - avatar
0
what is the answer it is not working.
20th Aug 2022, 9:54 AM
Manchala Raj kumar
Manchala Raj kumar - avatar
0
The answer import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int fact = 1; //your code goes here while( number >0){ fact *= number --; } System.out.println(fact); } }
9th Oct 2022, 6:50 PM
Lindsey Udeh
0
You are making a program for a chess tournament, that needs to calculate the points earned by a player. A win is worth 1 point, while a tie is worth 0.5 points. The given program declares two variables: wins and ties with the corresponding values. Create a program to calculate and output the points earned by the player.
22nd Sep 2023, 3:59 AM
ADITYA LAMBA
ADITYA LAMBA - avatar