Skip to content

Instantly share code, notes, and snippets.

@SoPat712
Created July 8, 2021 22:26
Show Gist options
  • Save SoPat712/c49297e431dfbefd24871a3bbd207b95 to your computer and use it in GitHub Desktop.
Save SoPat712/c49297e431dfbefd24871a3bbd207b95 to your computer and use it in GitHub Desktop.
Java Intermediate: 07/08/2021
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