Number Pattern programs in java
Simple number pattern program in java
1)
Enter a number : 5
5)
Enter a number : 5
1)
Enter a number : 5
1
12
123
1234
12345
import java.util.Scanner; public class Pattern1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
2)
Enter a number : 5
3)
Enter a number :5
Enter a number : 5
12345
1234
123
12
1
import java.util.Scanner; public class Pattern2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
3)
Enter a number :5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
import java.util.Scanner; public class Pattern3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = 1; i <= n; i++) { for (int k = n; k > i; k--) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } }
4)
Enter a number : 6
123456
12345
1234
123
12
1
import java.util.Scanner; public class Pattern4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int k = n; k > i; k--) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } }
5)
Enter a number : 5
54321
4321
321
21
1
import java.util.Scanner; public class Pattern5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int j = i; j >= 1; j--) { System.out.print(j); } System.out.println(); } } }
Nice article and very useful. Java Number Pattern Programs .
ReplyDeletethanks.