Basic Java Programs
Basic Java Programs
https://www.javatpoint.com/java-programs#java-basic-programs
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}
1. import java.util.Scanner;
2. public class PrimeExample4 {
3. public static void main(String[] args) {
4. Scanner s = new Scanner(System.in);
5. System.out.print("Enter the first number : ");
6. int start = s.nextInt();
7. System.out.print("Enter the second number : ");
8. int end = s.nextInt();
9. System.out.println("List of prime numbers between " + start + " and " + e
nd);
10. for (int i = start; i <= end; i++) {
11. if (isPrime(i)) {
12. System.out.println(i);
13. }
14. }
15. }
16. public static boolean isPrime(int n) {
17. if (n <= 1) {
18. return false;
19. }
20. for (int i = 2; i <= Math.sqrt(n); i++) {
21. if (n % i == 0) {
22. return false;
23. }
24. }
25. return true;
26. }
27. }
28.
1. class FactorialExample2{
2. static int factorial(int n){
3. if (n == 0)
4. return 1;
5. else
6. return(n * factorial(n-1));
7. }
8. public static void main(String args[]){
9. int i,fact=1;
10. int number=4;//It is the number to calculate factorial
11. fact = factorial(number);
12. System.out.println("Factorial of "+number+" is: "+fact);
13. }
14. }
ArmstrongNumberExample2.java
1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample2
4. {
5. //function to check if the number is Armstrong or not
6. static boolean isArmstrong(int n)
7. {
8. int temp, digits=0, last=0, sum=0;
9. //assigning n into a temp variable
10. temp=n;
11. //loop execute until the condition becomes false
12. while(temp>0)
13. {
14. temp = temp/10;
15. digits++;
16. }
17. temp = n;
18. while(temp>0)
19. {
20. //determines the last digit from the number
21. last = temp % 10;
22. //calculates the power of a number up to digit times and add the resultant to t
he sum variable
23. sum += (Math.pow(last, digits));
24. //removes the last digit
25. temp = temp/10;
26. }
27. //compares the sum with n
28. if(n==sum)
29. //returns if sum and n are equal
30. return true;
31. //returns false if sum and n are not equal
32. else return false;
33. }
34. //driver code
35. public static void main(String args[])
36. {
37. int num;
38. Scanner sc= new Scanner(System.in);
39. System.out.print("Enter the number: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. if(isArmstrong(num))
43. {
44. System.out.print("Armstrong ");
45. }
46. else
47. {
48. System.out.print("Not Armstrong ");
49. }
50. }
51. }
Star Pattern
1. Right Triangle Star Pattern
Output:
2. Left Triangle Star Pattern
Output:
3. Pyramid Star Pattern
Output:
4. Diamond Shape Pattern
1. import java.util.Scanner;
2. public class DiamondPattern
3. {
4. public static void main(String args[])
5. {
6. int row, i, j, space = 1;
7. System.out.print("Enter the number of rows you want to print: ");
8. Scanner sc = new Scanner(System.in);
9. row = sc.nextInt();
10. space = row - 1;
11. for (j = 1; j<= row; j++)
12. {
13. for (i = 1; i<= space; i++)
14. {
15. System.out.print(" ");
16. }
17. space--;
18. for (i = 1; i <= 2 * j - 1; i++)
19. {
20. System.out.print("*");
21. }
22. System.out.println("");
23. }
24. space = 1;
25. for (j = 1; j<= row - 1; j++)
26. {
27. for (i = 1; i<= space; i++)
28. {
29. System.out.print(" ");
30. }
31. space++;
32. for (i = 1; i<= 2 * (row - j) - 1; i++)
33. {
34. System.out.print("*");
35. }
36. System.out.println("");
37. }
38. }
39. }
Output:
Output:
Output:
7. Reverse Pyramid Star Pattern
Output:
Output:
1. import java.util.Scanner;
2. public class RightPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10. for (i= 0; i<= rows-1; i++)
11. {
12. for (j=0; j<=i; j++)
13. {
14. System.out.print("*"+ " ");
15. }
16. System.out.println("");
17. }
18. for (i=rows-1; i>=0; i--)
19. {
20. for(j=0; j <= i-1;j++)
21. {
22. System.out.print("*"+ " ");
23. }
24. System.out.println("");
25. }
26. }
27. }
Output:
1. import java.util.Scanner;
2. public class LeftPascalTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10. for (i= 1; i<= rows ; i++)
11. {
12. for (j=i; j <rows ;j++)
13. {
14. System.out.print(" ");
15. }
16. for (k=1; k<=i;k++)
17. {
18. System.out.print("*");
19. }
20. System.out.println("");
21. }
22. for (i=rows; i>=1; i--)
23. {
24. for(j=i; j<=rows;j++)
25. {
26. System.out.print(" ");
27. }
28. for(k=1; k<i ;k++)
29. {
30. System.out.print("*");
31. }
32. System.out.println("");
33. }
34. sc.close();
35. }
36. }
Output:
11. Sandglass Star Pattern
1. import java.util.Scanner;
2. public class SandglassPattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. n = sc.nextInt();
10. for (i= 0; i<= n-1 ; i++)
11. {
12. for (j=0; j<i; j++)
13. {
14. System.out.print(" ");
15. }
16. for (k=i; k<=n-1; k++)
17. {
18. System.out.print("*" + " ");
19. }
20. System.out.println("");
21. }
22. for (i= n-1; i>= 0; i--)
23. {
24. for (j=0; j<i; j++)
25. {
26. System.out.print(" ");
27. }
28. for (k=i; k<=n-1; k++)
29. {
30. System.out.print("*" + " ");
31. }
32. System.out.println("");
33. }
34. sc.close();
35. }
36. }
Output:
1. import java.util.*;
2. public class AlphabetPattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n=8;
7. // Outer for loop for number of lines
8. for (i = 0; i<=n; i++)
9. {
10. // Inner for loop for logic execution
11. for (j = 0; j<= n / 2; j++)
12. {
13. // prints two vertical lines
14. if ((j == 0 || j == n / 2) && i != 0 ||
15. // print first line of alphabet
16. i == 0 && j != n / 2 ||
17. // prints middle line
18. i == n / 2)
19. System.out.print("*");
20. else
21. System.out.print(" ");
22. }
23. System.out.println();
24. }
25. }
26. }
Output:
1. import java.util.Scanner;
2. public class TrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=1; i<= rows ; i++)
8. {
9. for (j = i; j < rows ; j++)
10. {
11. System.out.print(" ");
12. }
13. for (k = 1; k <= (2*i -1) ;k++)
14. {
15. if(k==1 || i == rows || k==(2*i-1))
16. {
17. System.out.print("*");
18. }
19. else
20. {
21. System.out.print(" ");
22. }
23. }
24. System.out.println("");
25. }
26. }
27. }
Output:
1. import java.util.Scanner;
2. public class DownTrianglePattern
3. {
4. public static void main(String[] args)
5. {
6. int i, j, k, rows=9;
7. for (i=rows; i>= 1 ; i--)
8. {
9. for (j = i; j<rows ; j++)
10. {
11. System.out.print(" ");
12. }
13. for (k = 1; k <= (2*i -1) ;k++)
14. {
15. if( k==1 || i == rows || k==(2*i-1))
16. {
17. System.out.print("*");
18. }
19. else
20. {
21. System.out.print(" ");
22. }
23. }
24. System.out.println("");
25. }
26. }
27. }
Output:
1. import java.util.*;
2. public class DiamondPattern
3. {
4. public static void main(String[] args)
5. {
6. Scanner sc = new Scanner(System.in);
7. System.out.println("Enter the number of rows you want to print: ");
8. int rows = sc.nextInt();
9. for (i=1; i<= rows ; i++)
10. {
11. for (j = rows; j > i ; j--)
12. {
13. System.out.print(" ");
14. }
15. System.out.print("*");
16. for (k = 1; k < 2*(i -1) ;k++)
17. {
18. System.out.print(" ");
19. }
20. if( i==1)
21. {
22. System.out.println("");
23. }
24. else
25. {
26. System.out.println("*");
27. }
28. }
29. for (i=rows-1; i>= 1 ; i--)
30. {
31. for (int j = rows; j > i ; j--)
32. {
33. System.out.print(" ");
34. }
35. System.out.print("*");
36. for (int k = 1; k < 2*(i -1) ;k++)
37. {
38. System.out.print(" ");
39. }
40. if(i==1)
41. System.out.println("");
42. else
43. System.out.println("*");
44. }
45. }
46. }
Output:
Number Pattern
1. Pattern-1
Output:
2. Pattern-2
Output:
3. Pattern-3
Output:
4. Pattern-4
1. public class Pattern4
2. {
3. public static void main(String[] args)
4. {
5. for (int i = 1; i <= 4; i++)
6. {
7. int n = 8;
8. for (int j = 1; j<= n - i; j++)
9. {
10. System.out.print(" ");
11. }
12. for (int k = i; k >= 1; k--)
13. {
14. System.out.print(k);
15. }
16. for (int l = 2; l <= i; l++)
17. {
18. System.out.print(l);
19. }
20. System.out.println();
21. }
22. for (int i = 3; i >= 1; i--)
23. {
24. int n = 10;
25. for (int j = 0; j<= n - i; j++)
26. {
27. System.out.print(" ");
28. }
29. for (int k = i; k >= 1; k--)
30. {
31. System.out.print(k);
32. }
33. for (int l = 2; l <= i; l++)
34. {
35. System.out.print(l);
36. }
37. System.out.println();
38. }
39. }
40. }
Output:
5. Pattern-5
1. import java.util.*;
2. public class Pattern5
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. rows = sc.nextInt();
10. for (i = 1; i <= rows; i++)
11. {
12. for (j = 1; j <= i; j++)
13. {
14. System.out.print(i+" ");
15. }
16. System.out.println();
17. }
18. }
19. }
Output:
6. Pattern-6
1. import java.util.*;
2. public class Pattern6
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows youy want to print: ");
9. rows = sc.nextInt();
10. for (i = rows; i >= 1; i--)
11. {
12. for (j = rows; j >= i; j--)
13. {
14. System.out.print(j+" ");
15. }
16.
17. System.out.println();
18. }
19. }
20. }
Output:
7. Pattern-7
1. import java.util.Scanner;
2. public class Pattern7
3. {
4. public static void main(String[] args)
5. {
6. int i, j, n;
7. Scanner sc = new Scanner(System.in);
8. System.out.print("Enter the number of rows you want to print: ");
9. n = sc.nextInt();
10. for (i = 1; i <= n; i++)
11. {
12. for (j = i; j >= 1; j--)
13. {
14. System.out.print(j+" ");
15. }
16. System.out.println();
17. }
18. }
19. }
Output:
8. Pattern-8
Output:
9. Pattern-9
1. import java.util.Scanner;
2. public class Pattern9
3. {
4. public static void main(String[] args)
5. {
6. int i, j, rows=9;
7. for (i = 1; i <= rows; i++)
8. {
9. for (j = 1; j <= i; j++)
10. {
11. if(j%2 == 0)
12. {
13. System.out.print(0);
14. }
15. else
16. {
17. System.out.print(1);
18. }
19. }
20. System.out.println();
21. }
22. }
23. }
Output:
10. Pattern-10
Output:
11. Pattern-11
Output:
12. Pattern-12
Output:
13. Pattern-13
Output:
14. Pattern-14
Output:
15. Pattern-15
Output:
16. Pattern-16
Output:
17. Pattern-17
Output:
18. Pattern-18
1. public class Pattern18
2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. for (int i = 1; i <= rows; i++)
7. {
8. for (int j = 1; j <= rows-i; j++)
9. {
10. System.out.print(1);
11. }
12. for (int j = 1; j <= i; j++)
13. {
14. System.out.print(i);
15. }
16. System.out.println();
17. }
18. }
19. }
Output:
19. Pattern-19
Output:
20. Pattern-20
Output:
21. Pattern-21
1. import java.util.Scanner;
2. public class Pattern21
3. {
4. public static void main(String[] args)
5. {
6. int i, j, min, n; //n is the number up to you want to print
7. System.out.print("Enter the value of n: ");
8. Scanner sc= new Scanner(System.in);
9. n=sc.nextInt();
10. //loo for upper left part
11. for (i = 1; i <= n; i++)
12. {
13. for (j = 1; j <= n; j++)
14. {
15. min = i < j ? i : j;
16. System.out.print(n - min + 1+ " ");
17. }
18. //loop for upper right part
19. for (j = n - 1; j >= 1; j--)
20. {
21. min = i < j ? i : j;
22. System.out.print(n - min + 1+ " ");
23. }
24. System.out.println();
25. }
26. //loop for lower left part
27. for (i = n - 1; i >= 1; i--)
28. {
29. for (j = 1; j <= n; j++)
30. {
31. min = i < j ? i : j;
32. System.out.print(n - min + 1+ " ");
33. }
34. //loop for lower right part
35. for (j = n - 1; j >= 1; j--)
36. {
37. min = i < j ? i : j;
38. System.out.print(n - min + 1+ " ");
39. }
40. System.out.println();
41. }
42. }
43. }
Output:
Character Pattern
1. Right Triangle Alphabetic Pattern
1. public class RightAlphabaticPattern
2. {
3. public static void main(String[] args)
4. {
5. int alphabet = 65; //ASCII value of capital A is 65
6. //inner loop for rows
7. for (int i = 0; i <= 8; i++)
8. {
9. //outer loop for columns
10. for (int j = 0; j <= i; j++)
11. {
12. //adds the value of j in the ASCII value of A and prints the corresponding alph
abet
13. System.out.print((char) (alphabet + j) + " ");
14. }
15. System.out.println();
16. }
17. }
18. }
Output:
Output:
Output:
Output:
1. import java.util.Scanner;
2. public class DiamondCharacterPattern
3. {
4. public static void main(String[] args)
5. {
6. char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q'
, 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
7. int alphabet _number = 0;
8. String[] diamond = new String[26]; // array of strings
9. System.out.print("Enter a Character between A to Z : ");
10. Scanner reader = new Scanner(System.in);
11. try
12. {
13. char user_ alphabet = reader.next("[A-Z]").charAt(0);
14. // search for letter number in the array letter
15. for (int i = 0; i < alphabet.length; i++)
16. {
17. if (letter[i] == user_ alphabet)
18. {
19. alphabet _number = i;
20. break;
21. }
22. }
23. //construct diamond
24. for (int i = 0; i <= alphabet _number; i++)
25. {
26. diamond[i] = "";
27. //add initial spaces
28. for (int j = 0; j < alphabet _number - i; j++)
29. {
30. diamond[i] += " ";
31. }
32. // add alphabet
33. diamond[i] += alphabet
34. //add space between letters
35. if (alphabet[i] != 'A')
36. {
37. for (int j = 0; j < 2 * i - 1; j++)
38. {
39. diamond[i] += " ";
40. }
41. // add alphabet
42. diamond[i] += alphabet[i];
43. }
44. // Draw the first part of the diamond
45. System.out.println(diamond[i]);
46. }
47. for (int i = alphabet _number - 1; i >= 0; i--)
48. {
49. // Draw the second part of the diamond
50. // prints the diamondArray in the reverse order
51. System.out.println(diamond[i]);
52. }
53. }
54. catch (Exception e)
55. {
56. e.printStackTrace();
57. }
58. finally
59. {
60. reader.close();
61. }
62. }
63. }
Output:
How to Print ASCII Value in Java
PrintAsciiValueExample1.java
PrintAsciiValueExample2.java
ReverseNumberExample2.java
1. import java.util.Scanner;
2. public class ReverseNumberExample3
3. {
4. //method for reverse a number
5. public static void reverseNumber(int number)
6. {
7. if (number < 10)
8. {
9. //prints the same number if the number is less than 10
10. System.out.println(number);
11. return;
12. }
13. else
14. {
15. System.out.print(number % 10);
16. reverseNumber(number/10);
17. }
18. }
19. public static void main(String args[])
20. {
21. System.out.print("Enter the number that you want to reverse: ");
22. Scanner sc = new Scanner(System.in);
23. int num = sc.nextInt();
24. System.out.print("The reverse of the given number is: ");
25. //method calling
26. reverseNumber(num);
27. }
28. }
1. import java.util.Scanner;
2. public class SumOfNaturalNumber3
3. {
4. public static void main(String[] args)
5. {
6. int num, i, sum = 0;
7. //object of Scanner class
8. Scanner sc = new Scanner(System.in);
9. System.out.print("Sum from: ");
10. //takes an integer as input
11. i = sc.nextInt();
12. System.out.print("Sum up to: ");
13. //takes an integer as input
14. num = sc.nextInt();
15. while(i <= num)
16. {
17. //adding the value of i into sum variable
18. sum = sum + i;
19. //increments the value of i by 1
20. i++;
21. }
22. //prints the sum
23. System.out.println("Sum of Natural Numbers = " + sum);
24. }
25. }
1. import java.util.Scanner;
2. public class SmallestNumberExample2
3. {
4. public static void main(String[] args)
5. {
6. int a, b, c, smallest;
7. //object of the Scanner class
8. Scanner sc = new Scanner(System.in);
9. //reading input from the user
10. System.out.println("Enter the first number:");
11. a = sc.nextInt();
12. System.out.println("Enter the second number:");
13. b = sc.nextInt();
14. System.out.println("Enter the third number:");
15. c = sc.nextInt();
16. smallest = c < (a < b ? a : b) ? c : ((a < b) ? a : b);
17. System.out.println("The smallest number is: "+smallest);
18. }
19. }