[go: up one dir, main page]

0% found this document useful (0 votes)
51 views59 pages

Basic Java Programs

Uploaded by

atulmangla210503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views59 pages

Basic Java Programs

Uploaded by

atulmangla210503
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

JAVA PROGRAMS

https://www.javatpoint.com/java-programs#java-basic-programs

Fibonacci series in Java


1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed

8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}

Fibonacci Series using recursion in java


1. class FibonacciExample2{
2. static int n1=0,n2=1,n3=0;
3. static void printFibonacci(int count){
4. if(count>0){
5. n3 = n1 + n2;
6. n1 = n2;
7. n2 = n3;
8. System.out.print(" "+n3);
9. printFibonacci(count-1);
10. }
11. }
12. public static void main(String args[]){
13. int count=10;
14. System.out.print(n1+" "+n2);//printing 0 and 1
15. printFibonacci(count-2);//n-2 because 2 numbers are already printed
16. }
17. }

Prime Number Program in Java


1. public class PrimeExample{
2. public static void main(String args[]){
3. int i,m=0,flag=0;
4. int n=3;//it is the number to be checked
5. m=n/2;
6. if(n==0||n==1){
7. System.out.println(n+" is not prime number");
8. }else{
9. for(i=2;i<=m;i++){
10. if(n%i==0){
11. System.out.println(n+" is not prime number");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0) { System.out.println(n+" is prime number"); }
17. }//end of else
18. }
19. }

Prime Number Program in Java (Another way)


1. import java.util.Scanner;
2.
3. import java.util.Scanner;
4.
5. public class PrimeExample3 {
6.
7. public static void main(String[] args) {
8. Scanner s = new Scanner(System.in);
9. System.out.print("Enter a number : ");
10. int n = s.nextInt();
11. if (isPrime(n)) {
12. System.out.println(n + " is a prime number");
13. } else {
14. System.out.println(n + " is not a prime number");
15. }
16. }
17.
18. public static boolean isPrime(int n) {
19. if (n <= 1) {
20. return false;
21. }
22. for (int i = 2; i < Math.sqrt(n); i++) {
23. if (n % i == 0) {
24. return false;
25. }
26. }
27. return true;
28. }
29. }

Find prime numbers between two numbers


You can also find prime numbers between two specified numbers.

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.

Palindrome Program in Java


1. class PalindromeExample{
2. public static void main(String args[]){
3. int r,sum=0,temp;
4. int n=454;//It is the number variable to be checked for palindrome
5.
6. temp=n;
7. while(n>0){
8. r=n%10; //getting remainder
9. sum=(sum*10)+r;
10. n=n/10;
11. }
12. if(temp==sum)
13. System.out.println("palindrome number ");
14. else
15. System.out.println("not palindrome");
16. }
17. }

Factorial Program in Java


1. class FactorialExample{
2. public static void main(String args[]){
3. int i,fact=1;
4. int number=5;//It is the number to calculate factorial
5. for(i=1;i<=number;i++){
6. fact=fact*i;
7. }
8. System.out.println("Factorial of "+number+" is: "+fact);
9. }
10. }

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. }

Armstrong Number in Java


1. import java.util.Scanner;
2. import java.lang.Math;
3. public class ArmstsrongNumberExample
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 limit: ");
40. //reads the limit from the user
41. num=sc.nextInt();
42. System.out.println("Armstrong Number up to "+ num + " are: ");
43. for(int i=0; i<=num; i++)
44. //function calling
45. if(isArmstrong(i))
46. //prints the armstrong numbers
47. System.out.print(i+ ", ");
48. }
49. }

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

1. public class RightTrianglePattern


2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row=6;
8. //outer loop for rows
9. for(i=0; i<row; i++)
10. {
11. //inner loop for columns
12. for(j=0; j<=i; j++)
13. {
14. //prints stars
15. System.out.print("* ");
16. }
17. //throws the cursor in a new line after printing each line
18. System.out.println();
19. }
20. }
21. }

Output:
2. Left Triangle Star Pattern

1. public class LeftTrianglePattern


2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row = 6;
8. //Outer loop work for rows
9. for (i=0; i<row; i++)
10. {
11. //inner loop work for space
12. for (j=2*(row-i); j>=0; j--)
13. {
14. //prints space between two stars
15. System.out.print(" ");
16. }
17. //inner loop for columns
18. for (j=0; j<=i; j++ )
19. {
20. //prints star
21. System.out.print("* ");
22. }
23. //throws the cursor in a new line after printing each line
24. System.out.println();
25. }
26. }
27. }

Output:
3. Pyramid Star Pattern

1. public class PyramidPattern


2. {
3. public static void main(String args[])
4. {
5. //i for rows and j for columns
6. //row denotes the number of rows you want to print
7. int i, j, row = 6;
8. //Outer loop work for rows
9. for (i=0; i<row; i++)
10. {
11. //inner loop work for space
12. for (j=row-i; j>1; j--)
13. {
14. //prints space between two stars
15. System.out.print(" ");
16. }
17. //inner loop for columns
18. for (j=0; j<=i; j++ )
19. {
20. //prints star
21. System.out.print("* ");
22. }
23. //throws the cursor in a new line after printing each line
24. System.out.println();
25. }
26. }
27. }

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:

5. Downward Triangle Star Pattern

1. public class DownwardTrianglePattern


2. {
3. public static void main(String[] args)
4. {
5. int rows=7;
6. //inner loop
7. for (int i= rows-1; i>=0 ; i--)
8. {
9. //outer loop
10. for (int j=0; j<=i; j++)
11. {
12. //prints star and space
13. System.out.print("*" + " ");
14. }
15. //throws the cursor in the next line after printing each line
16. System.out.println();
17. }
18. }
19. }

Output:

6. Mirrored Right Triangle Star Pattern

1. public class MirroredRightTrianglePattern


2. {
3. public static void main(String[] args)
4. {
5. int n=7;
6. //inner loop
7. for (int i= 0; i<= n; i++)
8. {
9. //outer loop
10. for (int j=1; j<=n-i; j++)
11. {
12. System.out.print(" ");
13. }
14. for (int k=0;k<=i;k++)
15. {
16. System.out.print("*");
17. }
18. System.out.println("");
19. }
20. }
21. }

Output:
7. Reverse Pyramid Star Pattern

1. public class ReversePyramidPattern


2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. for (int i= 0; i<= rows-1; i++)
7. {
8. for (int j=0; j<=i; j++)
9. {
10. System.out.print(" ");
11. }
12. for (int k=0; k<=rows-1-i; k++)
13. {
14. System.out.print("*" + " ");
15. }
16. System.out.println();
17. }
18. }
19. }

Output:

8. Right Down Mirror Star Pattern

1. public class RightDownMirrorPattern


2. {
3. public static void main(String args[])
4. {
5. int row=7;
6. for (int i= row; i>= 1; i--)
7. {
8. for (int j=row; j>i;j--)
9. {
10. System.out.print(" ");
11. }
12. for (int k=1;k<=i;k++)
13. {
14. System.out.print("*");
15. }
16. System.out.println("");
17. }
18. }
19. }

Output:

9. Right Pascal's Triangle

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:

10. Left Pascal's Triangle

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:

12. Alphabet Star Pattern

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:

13. Triangle Star Pattern

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:

14. Down Triangle Pattern

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:

15. Diamond Star Pattern

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

1. public class Pattern1


2. {
3. public static void main(String args[])
4. {
5. int i, j,number, n=7;
6. //loop for rows
7. for(i=0; i<n; i++)
8. {
9. number=1;
10. //loop for columns
11. for(j=0; j<=i; j++)
12. {
13. //prints num
14. System.out.print(number+ " ");
15. //incrementing the value of number
16. number++;
17. }
18. //throws the cursor at the next line after printing each row
19. System.out.println();
20. }
21. }
22. }

Output:

2. Pattern-2

1. public class Pattern2


2. {
3. public static void main(String[] args)
4. {
5. int i, j, k = 1;
6. //inner loop
7. for (i = 1; i <= 7; i++)
8. {
9. //outer loop
10. for (j = 1; j< i + 1; j++)
11. {
12. //prints the value of k
13. System.out.print(k++ + " ");
14. }
15. //throws the cursor at the next line
16. System.out.println();
17. }
18. }
19. }

Output:

3. Pattern-3

1. public class Pattern3


2. {
3. public static void main(String[] args)
4. {
5. int n = 8; //n is the number of rows you want to print
6. for (int i = 0; i < n; i++)
7. {
8. int number = 1;
9. System.out.printf("%" + (n - i) * 2 + "s", "");
10. for (int j = 0; j <= i; j++)
11. {
12. System.out.printf("%4d", number);
13. number = number * (i - j) / (j + 1);
14. }
15. System.out.println();
16. }
17. }
18. }

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

1. public class Pattern8


2. {
3. public static void main(String[] args)
4. {
5. int rows=9; //number of rows to print
6. for (int i = 1; i <= rows; i++)
7. {
8. int num;
9. if(i%2 == 0)
10. {
11. num = 0;
12. for (int j = 1; j <= rows; j++)
13. {
14. System.out.print(num);
15. num = (num == 0)? 1 : 0;
16. }
17. }
18. else
19. {
20. num = 1;
21. for (int j = 1; j <= rows; j++)
22. {
23. System.out.print(num);
24. num = (num == 0)? 1 : 0;
25. }
26. }
27. System.out.println();
28. }
29. }
30. }

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

1. public class Pattern10


2. {
3. public static void main(String[] args)
4. {
5. int n = 10;
6. for (int i = 1; i <= n; i++)
7. {
8. for (int j = 1; j < i; j++)
9. {
10. System.out.print(" ");
11. }
12. for (int k = i; k <= n; k++)
13. {
14. System.out.print(k+" ");
15. }
16. System.out.println();
17. }
18. for (int i = n-1; i >= 1; i--)
19. {
20. for (int j = 1; j < i; j++)
21. {
22. System.out.print(" ");
23. }
24. for (int k = i; k <= n; k++)
25. {
26. System.out.print(k+" ");
27. }
28. System.out.println();
29. }
30. }
31. }

Output:
11. Pattern-11

1. public class Pattern11


2. {
3. public static void main(String[] args)
4. {
5. int rows=8;
6. //Prints upper half pattern
7. for (int i = 1; i <= rows; i++)
8. {
9. for (int j = 1; j <= i; j++)
10. {
11. System.out.print(j+" ");
12. }
13. System.out.println();
14. }
15. //prints lower half pattern
16. for (int i = rows-1; i >= 1; i--)
17. {
18. for (int j = 1; j <= i; j++)
19. {
20. System.out.print(j+" ");
21. }
22. System.out.println();
23. }
24. }
25. }

Output:

12. Pattern-12

1. public class Pattern12


2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. for (int i = 1; i <= rows; i++)
7. {
8. for (int j = rows; j >= i; j--)
9. {
10. System.out.print(j+" ");
11. }
12. System.out.println();
13. }
14. }
15. }

Output:
13. Pattern-13

1. public class Pattern14


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. for (i = rows; i >= 1; i--)
7. {
8. for (j = 1; j <= i; j++)
9. {
10. System.out.print(j+" ");
11. }
12. System.out.println();
13. }
14. }
15. }

Output:

14. Pattern-14

1. public class Pattern14


2. {
3. public static void main(String[] args)
4. {
5. int rows=7, i, j;
6. for (i = rows; i >= 1; i--)
7. {
8. for (j = i; j >= 1; j--)
9. {
10. System.out.print(j+" ");
11. }
12. System.out.println();
13. }
14. }
15. }

Output:

15. Pattern-15

1. public class Pattern15


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. for (i = 1; i <= rows; i++)
7. {
8. //Prints first half of the row
9. for (j = 1; j <= i; j++)
10. {
11. System.out.print(j+" ");
12. }
13. //Prints second half of the row
14. for (j = i-1; j >= 1; j--)
15. {
16. System.out.print(j+" ");
17. }
18. System.out.println();
19. }
20. }
21. }

Output:

16. Pattern-16

1. public class Pattern16


2. {
3. public static void main(String[] args)
4. {
5. int i, j, rows=9;
6. //Prints upper half pattern
7. for (i = rows; i >= 1; i--)
8. {
9. for (j = 1; j <= i; j++)
10. {
11. System.out.print(j+" ");
12. }
13. System.out.println();
14. }
15. //Prints lower half pattern
16. for (i = 2; i <= rows; i++)
17. {
18. for (j = 1; j <= i; j++)
19. {
20. System.out.print(j+" ");
21. }
22. System.out.println();
23. }
24. }
25. }

Output:

17. Pattern-17

1. public class Pattern17


2. {
3. public static void main(String[] args)
4. {
5. int rows=9;
6. //Prints upper half pattern
7. for (int i = 1; i <= rows; i++)
8. {
9. //Prints i spaces at the beginning of each row
10. for (int j = 1; j < i; j++)
11. {
12. System.out.print(" ");
13. }
14. //Prints i to rows value at the end of each row
15. for (int j = i; j <= rows; j++)
16. {
17. System.out.print(j);
18. }
19. System.out.println();
20. }
21. //Prints lower half pattern
22. for (int i = rows-1; i >= 1; i--)
23. {
24. //Prints i spaces at the beginning of each row
25. for (int j = 1; j < i; j++)
26. {
27. System.out.print(" ");
28. }
29. //Prints i to rows value at the end of each row
30. for (int j = i; j <= rows; j++)
31. {
32. System.out.print(j);
33. }
34. System.out.println();
35. }
36. }
37. }

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

1. public class Pattern19


2. {
3. public static void main(String args[])
4. {
5. int rows=9;
6. for (int i = 1; i <= rows; i++)
7. {
8. int num = i;
9. for (int j = 1; j <= i; j++)
10. {
11. System.out.print(num+" ");
12. num = num+rows-j;
13. }
14. System.out.println();
15. }
16. }
17. }

Output:

20. Pattern-20

1. public class Pattern20


2. {
3. public static void main(String[] args)
4. {
5. int i, j, k, rows=9;
6. for(i=1;i< rows+1 ;i++)
7. {
8. for(j=i; j < rows+1 ;j++)
9. {
10. System.out.print(j + " ");
11. }
12. for(k=1; k < i ;k++)
13. {
14. System.out.print(k + " ");
15. }
16. System.out.println();
17. }
18. }
19. }

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:

2. Repeating Alphabet Pattern

1. public class RepeatingPattern


2. {
3. public static void main(String[] args)
4. {
5. int letter = 65; //ASCII value of capital A is 65
6. //inner loop for rwos
7. for (int i = 0; i<= 9; i++)
8. {
9. //outer loop for columns
10. for (int j = 0; j <= i; j++)
11. {
12. //prints the character
13. System.out.print((char) letter + " ");
14. }
15. letter++;
16. System.out.println();
17. }
18. }
19. }

Output:

3. K-shape Alphabet Pattern

1. public class KshapePattern


2. {
3. public static void main(String[] args)
4. {
5. for (int i = 8; i >= 0; i--)
6. {
7. int alphabet = 65;
8. for (int j = 0; j <= i; j++)
9. {
10. System.out.print((char) (alphabet + j) + " ");
11. }
12. System.out.println();
13. }
14. for (int i = 0; i<= 8; i++)
15. {
16. int alphabet = 65;
17. for (int j = 0; j <= i; j++)
18. {
19. System.out.print((char) (alphabet + j) + " ");
20. }
21. System.out.println();
22. }
23. }
24. }

Output:

4. Triangle Character Pattern

1. public class TriangleCharacterPattern


2. {
3. public static void main(String[] args)
4. {
5. for (int i = 0; i <= 8; i++)
6. {
7. int alphabet = 65;
8. for (int j = 8; j > i; j--)
9. {
10. System.out.print(" ");
11. }
12. for (int k = 0; k <= i; k++)
13. {
14. System.out.print((char) (alphabet + k) + " ");
15. }
16. System.out.println();
17. }
18. }
19. }

Output:

5. Diamond Character Pattern

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

1. public class PrintAsciiValueExample1


2. {
3. public static void main(String[] args)
4. {
5. // character whose ASCII value to be found
6. char ch1 = 'a';
7. char ch2 = 'b';
8. // variable that stores the integer value of the character
9. int asciivalue1 = ch1;
10. int asciivalue2 = ch2;
11. System.out.println("The ASCII value of " + ch1 + " is: " + asciivalue1);
12. System.out.println("The ASCII value of " + ch2 + " is: " + asciivalue2);
13. }
14. }

PrintAsciiValueExample2.java

1. public class PrintAsciiValueExample2


2. {
3. public static void main(String[] String)
4. {
5. int ch1 = 'a';
6. int ch2 = 'b';
7. System.out.println("The ASCII value of a is: "+ch1);
8. System.out.println("The ASCII value of b is: "+ch2);
9. }
10. }
PrintAsciiValueExample3.java

1. public class PrintAsciiValueExample3


2. {
3. public static void main(String[] args)
4. {
5. //characters whose ASCII value to be found
6. char ch1 = 'a';
7. char ch2 = 'b';
8. //casting or converting a charter into int type
9. int ascii1 = (int) ch1;
10. int ascii2 = (int) ch2;
11. System.out.println("The ASCII value of " + ch1 + " is: " + ascii1);
12. System.out.println("The ASCII value of " + ch1 + " is: " + ascii2);
13. }
14. }

How to Reverse a Number in Java


Reverse a number using while loop
ReverseNumberExample1.java

1. public class ReverseNumberExample1


2. {
3. public static void main(String[] args)
4. {
5. int number = 987654, reverse = 0;
6. while(number != 0)
7. {
8. int remainder = number % 10;
9. reverse = reverse * 10 + remainder;
10. number = number/10;
11. }
12. System.out.println("The reverse of the given number is: " + reverse);
13. }
14. }

ReverseNumberExample2.java

1. public class ReverseNumberExample2


2. {
3. public static void main(String[] args)
4. {
5. int number = 123456, reverse = 0;
6. //we have not mentioned the initialization part of the for loop
7. for( ;number != 0; number=number/10)
8. {
9. int remainder = number % 10;
10. reverse = reverse * 10 + remainder;
11. }
12. System.out.println("The reverse of the given number is: " + reverse);
13. }
14. }

Reverse a number using recursion


ReverseNumberExample3.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. }

Java Program to Find Sum of Natural Numbers


1. public class SumOfNaturalNumber1
2. {
3. public static void main(String[] args)
4. {
5. int i, num = 10, sum = 0;
6. //executes until the condition returns true
7. for(i = 1; i <= num; ++i)
8. {
9. //adding the value of i into sum variable
10. sum = sum + i;
11. }
12. //prints the sum
13. System.out.println("Sum of First 10 Natural Numbers is = " + sum);
14. }
15. }
1. public class SumOfNaturalNumber2
2. {
3. public static void main(String[] args)
4. {
5. int num = 100, i = 1, sum = 0;
6. //executes until the condition returns true
7. while(i <= num)
8. {
9. //adding the value of i into sum variable
10. sum = sum + i;
11. //increments the value of i by 1
12. i++;
13. }
14. //prints the sum
15. System.out.println("Sum of First 100 Natural Numbers is = " + sum);
16. }
17. }

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. public class SumOfNaturalNumber4


2. {
3. //method that returns the sum of n natural numbers
4. static int naturalNumberSum(int n)
5. {
6. int sum = 0;
7. //executes until the condition becomes false
8. for (int i = 1; i <= n; i++)
9. //adding the value of i to the sum variable
10. sum = sum + i;
11. return sum;
12. }
13. //main method
14. public static void main(String args[])
15. {
16. int n = 50;
17. //calling method and prints the sum
18. System.out.println("Sum of Natural Numbers is: "+naturalNumberSum(n));
19. }
20. }

Java Program to Find Smallest of Three


Numbers Using Ternary Operato
1. import java.util.Scanner;
2. public class SmallestNumberExample1
3. {
4. public static void main(String[] args)
5. {
6. int a, b, c, smallest, temp;
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. //comparing a and b and storing the smallest number in a temp variable
17. temp=a<b?a:b;
18. //comparing the temp variable with c and storing the result in the variable na
mes smallest
19. smallest=c<temp?c:temp;
20. //prints the smallest number
21. System.out.println("The smallest number is: "+smallest);
22. }
23. }

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. }

You might also like