Created
July 4, 2021 19:42
-
-
Save SoPat712/167cb55c52e24b5d77d2391b1934873d to your computer and use it in GitHub Desktop.
Java Intermediate: 07/04/2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javafx.test; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
starWars(); | |
printSum(); | |
if(isPrime()){ | |
System.out.println("The number is prime"); | |
} | |
else{ | |
System.out.println("The number is not prime"); | |
} | |
System.out.println("Enter a number to find its factorial: "); | |
int b = scanner.nextInt(); | |
System.out.println(fact(b)); | |
} | |
public static void starWars() { | |
System.out.println("May the Force be with you"); | |
} | |
public static void printSum() { | |
Scanner input = new Scanner(System.in); | |
// prompt user to enter 2 numbers | |
System.out.print("Enter number 1: "); | |
double num1 = input.nextDouble(); | |
System.out.print("Enter number 2: "); | |
double num2 = input.nextDouble(); | |
double sum = num1 + num2; | |
// print the sum of those numbers | |
System.out.println("Sum: " + sum); | |
} | |
public static boolean isPrime() | |
{ | |
Scanner input = new Scanner(System.in); | |
System.out.println("Enter a number to check if it's prime: "); | |
int number = input.nextInt(); | |
for(int i = 2; i < number; i++) | |
{ | |
if(number % i == 0) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static int fact(int x){ | |
if(x == 0 || x == 1){ | |
return 1; | |
} | |
else{ | |
return fact(x-1)*x; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment