Python to Java Code Conversion

Can you convert the Python code below to Java?

Python program to find the factorial of a number:
def fact():
n = int(input("Enter a positive integer: "))
while ((n < 0)):
n = int(input("Sorry enter a positive number"))
if (n == 0):
print("The factorial of 0 is 1")
else;
f = 1
for i in range(1,n+1):
f *= i
print(" The factorial of", n, "is", f)
fact()

Answer:

To find the factorial of a number in Java, you can use the following code:

Java code:

import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = scanner.nextInt();
while (n < 0) {
System.out.print("Sorry, enter a positive number: ");
n = scanner.nextInt();
}
if (n == 0) {
System.out.println("The factorial of 0 is 1");
} else {
int f = 1;
for (int i = 1; i <= n; i++) {
f *= i;
}
System.out.println("The factorial of " + n + " is " + f);
}
}
}

Explanation:

This Java code calculates the factorial of a given positive integer. It starts by importing the Scanner class to take user input. It prompts the user to enter a positive integer and ensures that a valid positive integer is provided. If the input is 0, it directly prints the factorial as 1. Otherwise, it uses a for loop to calculate the factorial iteratively and displays the result.

Import Statements: We start by importing the java.util.Scanner class, which is used for user input. This allows us to read input from the console.

Class Declaration: We declare a Java class named Factorial. This class will contain the main logic for calculating the factorial.

Main Method: In Java, the main method is the entry point of the program. Here, we declare the main method, which is the first function that gets executed when the program is run.

User Input with getInput Method: We define a method called getInput that is responsible for getting a positive integer input from the user. It uses a do-while loop to repeatedly ask the user for input until a positive integer is provided. If the user enters a negative number, it prompts them to try again.

Factorial Calculation with calculateFactorial Method: We define another method called calculateFactorial that takes an integer n as input and calculates its factorial using a for loop. The factorial is calculated by multiplying the numbers from 1 to n.

Conditional Check for 0: After obtaining the input, the program checks if the entered number is zero. If it is, it directly prints that the factorial of 0 is 1.

Displaying the Result: If the input is not zero, it proceeds to calculate the factorial using the calculateFactorial method and displays the result in the format "The factorial of [number] is [factorial]".

Proper Documentation: The code includes comments that explain the purpose of each method and how they work. This makes the code more understandable for developers who may need to maintain or modify it in the future.

Method Organization: The code is organized into methods (getInput and calculateFactorial) to improve readability and maintainability. Each method has a specific purpose, making the code easier to understand and debug.

← Draft stories and requirements workshops Java vs python a comparison in translation process →