Sum of factorials

Question:
How do we use the while loop to find the sum of the sequence 1!+2!+3!+...+n!

Code:
import java.util.Scanner;
 
public class SumOfFactorials {
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter number: ");
      int n = sc.nextInt();
 
      int total=0;
 
      int i=1;
      while(i <= n) {
        int factorial=1;
        int j=1;
        while(j <= i) {
          factorial=factorial*j;
          j = j+1;
        }
        total = total + factorial;
        i=i+1;
      }
      System.out.println("Sum: " + total);
   }
}

Output:
$ java SumOfFactorials
Enter number: 4
Sum: 33

Java Program For Pyramid Of Numbers

Question:
Write a java program to print the following pattern: 1 121 12321

Code:
import java.io.*;
import java.util.*;
 
public class Program {
  public static void main(String []args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int n=in.nextInt();
 
    for(int i=1;i<=n;i++) {
      for(int j=i;j<=n-1;j++) {
        System.out.print(" ");
      }
      for(int j=1;j<=i;j++) {
        System.out.print(j);
      }
      for(int j=i-1;j>=1;j--) {
        System.out.print(j);
      }
      System.out.println();
    }
  }
}

Output:
$ java Program
Enter a number: 4
   1
  121
 12321
1234321

Java program to swap two numbers.

Question:
How can you swap two numbers in java?

Answer:
Swapping using temporary or third variable.

Code:
import java.util.Scanner;
 
public class Swap {
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter first int: ");
      int i1 = sc.nextInt();
 
      System.out.print("Enter first int: ");
      int i2 = sc.nextInt();
 
      System.out.println("Value #1 " + i1);
      System.out.println("Value #2 " + i2);
 
      System.out.println("Swapping...");
 
      int temp = i1;
      i1 = i2;
      i2 = temp;
 
      System.out.println("Value #1 " + i1);
      System.out.println("Value #2 " + i2);
 
   }
}

Output:
$ java Swap
Enter first int: 1
Enter first int: 4
Value #1 1
Value #2 4
Swapping...
Value #1 4
Value #2 1

How to write a program in Java that calculates the difference between 2 user input dates?

Question:
How to write a program in Java that calculates the difference between 2 user input dates?

Code:
import java.util.*;
import java.text.*;
 
public class CompareDates {
   public static void main( String[] args ) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter date1 (yyyy-MM-dd): ");
      String d1 = sc.nextLine();
 
      System.out.print("Enter date2 (yyyy-MM-dd): ");
      String d2 = sc.nextLine();
 
      try {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         Date date1 = sdf.parse(d1);
         Date date2 = sdf.parse(d2);
 
         System.out.println("Date1: " + sdf.format(date1));
         System.out.println("Date2: " + sdf.format(date2));
 
         long diff = date1.getTime() - date2.getTime();
         diff = diff / (1000L*60L*60L*24L);
         System.out.println("Delta: " + diff + " days");
 
      } catch(ParseException ex) {
      }
   }
}

Output:
$ java CompareDates
Enter date1 (yyyy-MM-dd): 2000-11-11
Enter date2 (yyyy-MM-dd): 2000-10-11
Date1: 2000-11-11
Date2: 2000-10-11
Delta: 31 days

Round numbers to whole number without using the Math.round

Question:
How to round decimals to whole number, without using the Math.round or ceiling program?

Answer:
You can do it with casting.

Code:
import java.util.Scanner; 
 
public class Rounding {
   public static void main(String[] args) {
 
      Scanner scan = new Scanner(System.in);
 
      System.out.print("Enter double: " );
      Double d = scan.nextDouble();
      System.out.println("You entered: " + d);
      Integer i = d.intValue();
 
      if ( d - i >= .5 ) {
        i=i+1;
      }
 
      System.out.println("Rounded: " + i);
 
   }
}

Output:
$ java Rounding
Enter double: 12.4999
You entered: 12.4999
Rounded: 12


$ java Rounding
Enter double: 8.5
You entered: 8.5
Rounded: 9

Validating an input is a number and is greater then 99.

Question:
Design an algorithm that prompts the user to enter a number that is greater than 99 and validates the input.

Code:
import java.util.*;
 
public class ReadInt {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
      int i=0;
 
      try {
 
         System.out.print("Enter Integer: ");
         i = sc.nextInt();
         System.out.println("You entered: " + i);
 
         if(i > 99){
            System.out.println(i + " is greater than 99");
         } else {
            System.out.println(i + " is NOT greater than 99");
         }
 
      } catch (InputMismatchException e) {
         System.out.println("You did not enter a number.");
      }
 
   }
}

Output:
$ java ReadInt
Enter Integer: 55
You entered: 55
55 is NOT greater than 99

$ java ReadInt
Enter Integer: 100
You entered: 100
100 is greater than 99

$ java ReadInt
Enter Integer: foo
You did not enter a number.

A java program to find the sum of squares.

Question:
A java program to find the sum of squares.

Code:
import java.util.Scanner;
 
public class SumOfSquares {
 
   public static void main(String[] args) {
 
      Scanner sc = new Scanner(System.in);
 
      System.out.print("Enter Integer: ");
      int n = sc.nextInt();
      System.out.println("You entered: " + n);
 
      int sum = 1;
 
      for (int x=2;x<=n;x++) {
        sum = sum + (x*x);
      }
      System.out.println(sum);
 
   }
}

Output:
$ java SumOfSquares
Enter Integer: 5
You entered: 5
55