In this post i will explain about different patterns using java program.
1)Our first pattern is
*
* *
* * *
* * * *
* * * * *
So how can we print this pattern using Java program.First observe this pattern it has five rows and columns are increasing in each row and max columns are 5 which are equal to number of rows.so for first row we have to print star one time, for row two, two time and so on.
SO how can we do it?
So we have to do two things first we have to use a loop which will iterate for 5 times.
for(int i=1;i<=5;i++)
So above statement will iterate for 5 times next we have to print stars starting from 1 and for each iteration we have to increment it by 1 and the terminating condition is when it reaches value of row number i.e. for row 1 its 1
for row 2 its 2
for row 3 its 3 and so on.
So we can use for(int j=0;j<i;j++)
So in this way the given pattern will be printed.Below is complete program:
2)Now we will print following pattern
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
So above are some patterns which can be printed using Java program.
Note:The 2nd pattern is not formatting well after coming on this page.
1)Our first pattern is
*
* *
* * *
* * * *
* * * * *
So how can we print this pattern using Java program.First observe this pattern it has five rows and columns are increasing in each row and max columns are 5 which are equal to number of rows.so for first row we have to print star one time, for row two, two time and so on.
SO how can we do it?
So we have to do two things first we have to use a loop which will iterate for 5 times.
for(int i=1;i<=5;i++)
So above statement will iterate for 5 times next we have to print stars starting from 1 and for each iteration we have to increment it by 1 and the terminating condition is when it reaches value of row number i.e. for row 1 its 1
for row 2 its 2
for row 3 its 3 and so on.
So we can use for(int j=0;j<i;j++)
So in this way the given pattern will be printed.Below is complete program:
package examples;
public class Pattern {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=0;j<i;j++){
System.out.print(" * ");
}
System.out.println("\n");
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
public class Pattern {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=0;j<i;j++){
System.out.print(" * ");
}
System.out.println("\n");
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
2)Now we will print following pattern
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Program Code:
package examples;
public class Pattern1 {
public static void main(String[] args) {
int n=5;
//for iterating the rows
for (int i = 1; i <= n; i++)
{
//for printing number of spaces before printing *
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
//for printing * till half part i.e.
// *
// * *
// * * *
// * * * *
// * * * * *
for (int l = 1; l <= i; l++)
System.out.print("*");
//for printing rest of the part
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
//for printing lower part in same way as upper part is printed
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int l = 1; l <= i; l++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
package examples;
public class Pattern1 {
public static void main(String[] args) {
int n=5;
//for iterating the rows
for (int i = 1; i <= n; i++)
{
//for printing number of spaces before printing *
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
//for printing * till half part i.e.
// *
// * *
// * * *
// * * * *
// * * * * *
for (int l = 1; l <= i; l++)
System.out.print("*");
//for printing rest of the part
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
//for printing lower part in same way as upper part is printed
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int l = 1; l <= i; l++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
System.out.println();
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
So above are some patterns which can be printed using Java program.
Note:The 2nd pattern is not formatting well after coming on this page.
0 comments:
Post a Comment