Home > Java > javaTutorial > body text

Number Patterns in Java

王林
Release: 2024-08-30 16:25:12
Original
1006 people have browsed it

Number Patterns are quite a trend for freshers to get as part of interview questions as it provides a good brainstorming session to analyze a person’s creativity and innovation. Solving more of patterns show logical and mathematical capability. It is indeed a good way to form different patterns using all these conditional loops and syntaxes in java. It helps in improvising the skills of optimization and helps develop logical and analytical aptitude. It can be substituted with any character or symbol. If you learn number pattern, you can frame any of the patterns based on java.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Top Examples of Number Patterns

Here we will learn how to face some of the good Number Patterns. Let’s have a peek at some of the good Number Patterns in java with examples and code implementation, which are explained below in detail:

Example #1

Code:

import java.util.Scanner;
public class Pattern1 {
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows ");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #2

Code:

import java.util.Scanner;
public class Pattern2 {
public static void main (String [] args) {
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #3

Code:

import java.util.Scanner;
public class Pattern_3 {
public static void main (String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #4

Code:

import java.util.Scanner;
public class Pattern4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #5

Code:

import java.util.Scanner;
public class Pattern5 {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #6

Code:

import java.util.Scanner;
public class Pattern6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows ");
int rows = scanner. nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #7

Code:

import java.util.Scanner;
public class Pattern7 {
public static void main (String [] args)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Enter the number of rows ");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #8

Code:

import java.util.Scanner;
public class Pattern8 {
public static void main (String[] args)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = rows; i >= 1; i--)
{
for (int j = rows; j >= i; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #9

Code:

import java.util.Scanner;
public class Pattern9
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern ");
for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #10

Code:

import java.util.Scanner;
public class Pattern10
{
public static void main(String [] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
int k = 1;
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #11

Code:

import java.util.Scanner;
public class Pattern11
{
public static void main(String[] args)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Enter number of rows");
int rows = scanner.nextInt();
System.out.println("Printing Pattern");
for (int i = 1; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #12

Code:

import java.util.Scanner;
public class Pattern12
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Pattern Printing");
for (int i = 1; i <= rows; i++)
{
int temp = i;
for (int j = i; j >= 1; j--)
{
System.out.print(temp + " ");
temp = temp + rows;
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #13

Code:

import java.util.Scanner;
public class Pattern13
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = scanner.nextInt();
System.out.println("Pattern Printing");
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j > i; j--)
{
System.out.print(" ");
}
int temp= 1;
for (int k = 1; k <= i; k++)
{
System.out.print(temp + " ");
temp = temp * (i - k) / (k);
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Example #14

Code:

import java.util.Scanner;
public class Pattern14
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of rows");
int rows = scanner.nextInt();
System.out.println("Pattern Printing");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
for (int k = i - 1; k >= 1; k--)
{
System.out.print(k + " ");
}
System.out.println();
}
}
}
Copy after login

Output:

Number Patterns in Java

Conclusion

Solving number Patterns or any design pattern improves a person’s analytical and logical building aptitude. In bigger domains, it provides an overview of how to create and fulfil requirements given for a project and how effectively it can be tackled with confidence.

The above is the detailed content of Number Patterns in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!