0% found this document useful (0 votes)
4 views

Pattern program-1

The document contains Java code for seven different pattern programs that generate various character and star patterns based on user input. Each question includes a brief description of the expected output and the corresponding Java implementation. The patterns range from alphabetical sequences to complex star arrangements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Pattern program-1

The document contains Java code for seven different pattern programs that generate various character and star patterns based on user input. Each question includes a brief description of the expected output and the corresponding Java implementation. The patterns range from alphabetical sequences to complex star arrangements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pattern program

Question 1.

ABBCCCDDDDEEEEE…..so on

import java.util.*;
class Q1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of character print: ");
int n = sc.nextInt();
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=i;j++)
{
System.out.print((char)(64+i));
}
}
}
}

Question 2.

Enter the number of character print:

ABCDEEDCBA
ABCD DCBA
ABC CBA
AB BA
A A

import java.util.*;
class Q2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of character print: ");
int n = sc.nextInt();
for(int i = 1;i<=n;i++)
{
for(int j = 1;j<=n-i+1;j++)
{
System.out.print((char)(64+j));
}
for(int j = 1;j<i;j++)
{
System.out.print(" ");
}
for(int j = n-i+1;j>=1;j--)
{
System.out.print((char)(64+j));
}
System.out.println();
}
}
}

Question 3.

* *
** **
*** ***
********
********
*** ***
** **
* *

import java.util.*;
public class Q3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines(lines should be even): ");
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
int p = i<=n/2?i:n-i+1;
for(int j=1;j<=p;j++)
System.out.print("*");
for(int k=1;k<=2*(n/2-p);k++)
System.out.print(" ");
for(int j=1;j<=p;j++)
System.out.print("*");
System.out.println();
}
}
}

Question 4.

**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********

Question 5.

********
* *
* *
* *
* *
* *
* *

* *
********

import java.util.*;

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
if(i==1||i==n)
for(int j=1;j<=n;j++)
System.out.print("* ");
else
{
System.out.print("* ");
for(int j=2;j<=n-1;j++)
System.out.print(" ");
System.out.print("* ");
}
System.out.println();
}

}
}

Question 6.
********
** **
* * * *
* * * *
* * *
* * * *
* * * *
** * *
********

import java.util.*;

public class Main


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines: ");
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
if(i==1||i==n)
for(int j=1;j<=n;j++)
System.out.print("* ");
else
{
System.out.print("* ");
for(int j=2;j<=n-1;j++)
{
if(j==i||j==n-i+1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.print("* ");
}
System.out.println();
}
}
}

Question 7.

* *
* *
* *
*
* *
* *
* *

import java.util.*;

public class Q7
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of lines (odd number): ");
int n = sc.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j==i||j==n-i+1)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}

You might also like