Print Pyramid Star Pattern in Java Program
* *
* * * *
* * * * * *
* * * * * * * *
how to print this pattern in java ,
public class Pattern4{
public static void main(String args[]){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<i*2;j++){
System.out.print("*");
}
System.out.println();
}
}
}
or
Hi how can you display this output?
*
**
***
****
*****
when the input is 5?
UnknownJune 14, 2016 at 8:07 AM
import java.util.*;
public class Pattern5{
static Scanner s=new Scanner(System.in);
public static void main(String args[]){
int i,j,n;
System.out.print("Input Number : ");
n=s.nextInt();
for(i=0;i<=n;i++){
for(j=0;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
=========================================================
Please help me print this pattern
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
package manoj2;
public class Patern {
public static void main(String[] args)
{
int m=5;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(m);
}
m--;
System.out.println("");
}
int n=2;
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print(n);
}
n++;
System.out.println("");
}
}
}
public static void main(String[] args) {
for (int i=5;i>=1;i--){
for (int j=1;j<=6-i;j++){
System.out.print(i);
}System.out.println();
}
for (int i=1;i<5;i++){
for (int j=1;j<=5-i;j++){
System.out.print(1+i);}
System.out.println();
}}
=====================================================================================
How do you print following pyramid pattern of stars in Java:
******
*******
********
*******
******
**********
*********
*******
binduOctober 5, 2016 at 4:09 AM
public class Pattern10 {
public static void main(String[] args) {
int k=8;
for (int i = 6; i <=7 ;i++) {
for (int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
while(k<=12)
{
for (int i = 1; i <=3; i++) {
for (int j = 1; j <=k; j++) {
System.out.print("*");
}
k--;
System.out.println();
}
k=k+5;
}
}
}
=====================================================
Hi how can you display this output?
abcde abcde
abcd bcde
abc cde
ab de
a e
are you kidding ?
UnknownAugust 3, 2016 at 4:24 AM
import java.io.*;
class patt1{
public static void main(String args[]){
char a=(char)97,b,c;
int i,j,n=5;
for(i=0;ii;j--){
System.out.print(b);
b++;
}
for(int k=0;ki;j--){
System.out.print(c);
c++;
}
System.out.print("\n");
}
}
}
UnknownAugust 5, 2016 at 9:53 AM
just print it. hahahaha
====================================================================
please help me with this....
*
*A*
*A*A*
*A*A*A*
class pat
{
public static void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==0)
System.out.print("A");
else
System.out.print("*");
}
System.out.println();
}
}
}
can u please write the code for below pattern
A B C D E A B C D E
A B C D A B C D
A B C A B C
A B A B
A A
A B A B
A B C A B C
A B C D A B C D
A B C D E A B C D E
=================================================================
999999999999999999
88888888 88888888
7777777 7777777
55555 55555
4444 4444
333 333
22 22
1 1
public static void main(String[] args) {
int number = 9;
for(int i=0;i<9;i++){
for(int j=0;j<18;j++){
if(number==6)
continue;
System.out.print(number);
}
if(number != 6)
System.out.println();
number--;
}
}
UnknownOctober 14, 2016 at 9:48 AM
public static void main(String[] args) {
// TODO code application logic here
int i, j,l;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
System.out.print(j);
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();}
for(i=2;i<=5;i++)
{
for(j=1;j<=i;j++)
System.out.print(j);
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();}
}
}
===========================
1
121
12321
121
1
plz help
how to code this
*
**
***
****
*****
******
*
*
***
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
==============================================================
Guys i have found easy way to print pattern
public class Try {
public static void main(String[] args) {
int count =6;
for(int i=5;i>0;i--)
{
count--;
for(int j=count;j>0;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
===================output==========================
*****
****
***
**
*
Reply
========================================================================
how to print
*
* *
* * *
* * * *
* * *
* *
*
public class Star3 {
public static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
how to reverse the logic i mean to print in decreasing order from 10stars to 1 and from 1start to 10 could anyone help me out in doin this
========================================================================
write a program in java that prints the following pyramid:
----------------------1
--------------------1 2 1
------------------1 2 4 2 1
----------------1 2 4 8 4 2 1
-------------1 2 4 8 16 8 4 2 1
---------1 2 4 8 16 32 16 8 4 2 1
------1 2 4 8 16 32 64 32 16 8 4 2 1
--1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
/* program 11: Full Number Pattern 11 Pyramid
*/
//imports
import java.util.Scanner;
class FullNumberPatternPyramid11
{
public static void main(String[] args)
{
//declarations
int input,RefNum;
System.out.println("enter the size of the pyramid");
input=new Scanner(System.in).nextInt();
if(input>0)
{
for (int rows=1;rows<=input ;rows++ )
{
RefNum=rows;
for(int spaces=1;spaces<=input-rows;spaces++)
{
System.out.print(" ");
}//for
for (int col=1;col<=(2*rows)-1 ;col++ )
{
if(col<=rows)
{
System.out.print(col);
System.out.print(" ");
}//if
else
{
RefNum--;
System.out.print(RefNum);
System.out.print(" ");
}//else
}//for
System.out.println();
}//for
}//if
else
{
System.out.println("enter positive integers only");
}//else
}//main
}//class
=======================================
Can somebody write a program in java that prints the following pyramid:
----------------------1
--------------------1 2 1
------------------1 2 4 2 1
----------------1 2 4 8 4 2 1
-------------1 2 4 8 16 8 4 2 1
---------1 2 4 8 16 32 16 8 4 2 1
------1 2 4 8 16 32 64 32 16 8 4 2 1
--1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
int n = 10;
// Loop through the lines from 1 to n
for (int i = 1; i <= n; i++) {
// printing spaces, 4 at a time from j=0 to j= n-i
for (int j = 1; j <= (n - i); j++)
{
System.out.print(" ");
}
// Printing number increamentally from 0 to i-1
for (int j = 0; j < i; j++) {
System.out.printf("%4d", (int) Math.pow(2, j));
}
// Printing number decreamentally from i-2 to 0
for (int j = i - 2; j >= 0; j--) {
System.out.printf("%4d", (int) Math.pow(2, j));
}
System.out.println();
}
}
orrrr
public class Star3 {
public static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
or
public class Star3 {
public static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (i + j > n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
===========================
00
1001
010010
10100101
0101001010
101010010101
101010010101
0101001010
10100101
010010
1001
00
====================================================
print this please using loops
*
*
* *
* *
* * *
public class Star3 {
public static void main(String[] args) {
int n = 10;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Reply
*
**
***
**
*
*
***
******
****
**
*
Reply
A
AB
ABC
ABCD
ABCDE
*
*-*
*-*-*
*-*-*-*
===========================================
1
01
101
0101
class pattern
{
public static void main(String args[])
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
if((i+j)%2==0)
{
System.out.print("1");
}
else
{
System.out.print("0");
}}
System.out.println();
}
}
}
* *
** **
*****
** **
* *
please given the Logic........?????
===========\
how to print
* * *
* * *
* * *
Reply
===================================================
1
23
345
4567
56789
class patt5
{
public static void main(String[]args)
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<i;j++)
{
System.out.print(i+j);
}
System.out.println();
}
}
}
=========================================================================
1
22 22
333 333 333
4444 4444 4444 4444
UnknownFebruary 21, 2017 at 1:33 AM
/*
Print this output
1
22 22
333 333 333
4444 4444 4444 4444
*/
public class pattern {
public static void main(String[] args) {
int i=0;
int a=8; // its ur number to be patterned
System.out.println("========");
for (i=1; i<=a;i++){
for(int j=1;j<=i;j++){
System.out.print(i);
if(i>1){
for(int k=2; k<=i;k++){
System.out.print(i);
}
System.out.print(" ");
}
}
System.out.println();
}
}
}
=====================
i want
1
23
456
78910
===============================
UnknownSeptember 6, 2016 at 12:17 PM
int x=1;
for(int i=0;i<=4;i++)
{
for(int j=0; j<=i;j++)
{
System.out.print(x);
x++;
}
System.out.println();
}
=============================================
HOW To Print??
1******
12*****
123****
1234***
12345**
123456*
1234567
int i,j,n=1,k;
for(i=1;i<=7;i++){
for(j=1;j<=i;j++){
System.out.print(""+j);
}
for(k=n;k<=6;k++){
System.out.print("*");
}
n++;
System.out.println(" ");
}
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
======================================================================
Hi how to print *
*****
***
*
How to print
* *
* *
*
* *
* *
Pls help
public static void main(String[] args) {
int i, j;
for(i=3;i>1;i--){
for(j=1;j<=2;j++){
System.out.print("*");
}
System.out.println(" ");
}
for(i=1;i>=1;i--){
System.out.println("*");
}
for(i=3;i>1;i--){
for(j=1;j<=2;j++){
System.out.print("*");
}
System.out.println(" ");
}
}
**
**
*
**
**
can u pls tell me how to print
*****
* *
* *
* *
*****
===A Hollow square??
How to print\
*
**
***
****
*****
ANS
UnknownDecember 10, 2016 at 7:18 AM
import java.io.*;
class Pat3
{
public static void main(String s[])
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print("*");
}
System.out.println(" ");
}
}
}
====================================
***********
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
***********
public class Aryan
{
public static void main(String[] args) {
for (int i = 11; i >=1; i--) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <=11; i++) {
for (int j = 1; j <=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
sir how to print
**********
********
******
****
**
****
******
********
**********
======================================================================
what about this?
ABCDEF
BCDEF
CDEF
DEF
EF
F
Thanks for this site, an another way to print the star pattern in triangular shape is:
public static void main(String[] args) {
for(int i=0;i<=4;i++)
{
for(int j=0;j<=4;j++)
{
if(j<4-i)
{
System.out.print(" ");
}
else
{
System.out.print("* ");
}
}
System.out.println();
}
}
UnknownOctober 3, 2016 at 11:03 PM
how can print this pattern:
1
21
321
4321
54321
A=========================================
How to print this pattern
---*
--**
-***
****
in Java (excluding that dashes)
============================================
hi...how to print the following output
####
###
##
#
==========================
1
12
123
1234
123
12
1
please solve this?
==========================================================
2
4 4
6 6 6
8 8 8 8
hey.... How to print this output??
class PritStarTraingle{
public static void main(String args[]) {
int i,j,k=2;
for(i=0;i<4;i++)
{
for(j=0;j<=i;j++){
System.out.print(k);
}
k=k+2;
System.out.println();
}
}
}
how to print this pattern pls ... i need it badly please
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
1 1
1 2 1
1 2 2 1
1 2 2 2 1
1 2 2 2 2 1
How to print this output??
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
========================
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1
12
123
1234
12345
How to print this output?
===========================================================================
MarkOctober 17, 2016 at 8:03 AM
1
12
123
1234
12345
How to print this output??
import java.io.*;
class Pat4
{
public static void main(String s[])
{
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j < i; j++)
{
System.out.print(j+1);
}
System.out.println(" ");
}
}
}
or
class PritStarTraingle{
public static void main(String args[]) {
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
or
public static void main(String[] args) {
int i,j,n=1,k;
for(i=1;i<=7;i++){
for(j=1;j<=i;j++){
System.out.print(""+i*2);
}
for(k=n;k<=6;k++){
System.out.print("*");
}
n++;
System.out.println(" ");
}
}
==========================================
can anyone plz help me out with this pattern
7
14 15
28 29 30 31
56 57 58 59 60 61 62 63
hi does anyone know ho to print a triangle with a shape chosen by the user ?? (with textIO or scanner) thanks alot
1
22
333
4444
55555
=========================================================================
how to printing this? plz help
1
22
333
4444
55555
class PritStarTraingle{
public static void main(String args[]) {
int i,j;
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++){
System.out.print(i);
}
System.out.println();
}
}
}
==========================================================
This program to print a triangle..
class StarTriangle
{
public static void main(String[] args)
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
=========================================
*
***
*****
*******
*********
===============================================
How to print that pattern(excluding '-'):
--------*
------*-*
----*-*-*
--*-*-*-*
*-*-*-*-*
Thanks in advance.
or
public class PrintStar {
public static void main(String a[])
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print(" ");
}
for(k=5;k>=i;k--)
{
System.out.print("*");
}
System.out.println();
}
}
}
0r
for (char i = 'A'; i <= 'Z'; i++) {
for (char j = 'Z'; j >i; j--) {
System.out.print(" ");
}
for (char l = 'A'; l < (2*i-64); l++) {
System.out.print(i+"");
}
System.out.println(" ");
}
=================================================================
* * * * *
* * *
*
* * *
* * * * *
pattern program in c
======================================================================
print this pls
1
21
321
4321
54321
Reply
==============================================================
how to print this pattern
1
21
321
4321
54321
Reply
=================================================================
The method takes an integer argument and prints the following pattern, shown n = 4.
abcdcba
abc cba
ab ba
a a
ab ba
abc cba
abcdcba
===========================================
Write a program for the below given pattern:
11
11 10 11
11 10 9 10 11
11 10 9 8 9 10 11
Can somebody please help me?
===================================
**0**
*0*0*
0*0*0
*0*0*
**0**
==========================
1 2 3 4 3 2 1
1 2 3 * 3 2 1
1 2 * * * 2 1
1 * * * * * 1
==================
*
-*
-*-
*-*-
*-*-*
-*-*-*
=================
1
2 3
4 5 6
7 8 9 10
=========================================
* * * * *
* * * *
* * *
* *
*
public class Demo {
public static void main(String[] args) {
int i,j,k;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
}
}
}
==================================
How to Print Pyramid Pattern in Java? Program Example
Pattern based exercises are a good way to learn nested loops in Java. There are many pattern based exercises and one of them is printing Pyramid structure as shown below:
*
* *
* * *
* * * *
* * * * *
You need to write a Java program to print the above pyramid pattern. How many levels the pyramid triangle would have will be decided by the user input. You can print this kind of pattern by using print() and println() method from System.out object. System.out.print() just prints the String or character you passed to it, without adding a new line, useful to print stars in the same line.
While, System.out.println() print characters followed by a newline character, which is useful to move to the next line. You can also use the Scanner class to get input from the user and draw a pyramid up to that level only. For example in the above diagram, the pyramid has 5 levels.
Analysis
If you look at the problem then you will find that you need to print the star (*) character in the same line as well as a new line to generate a pyramidical pattern. You can also see that * are separated by space. In programming, to do a task repeatedly e.g. printing star, you can use a loop.
This kind of problem, which require printing in row and column usually require two loops, one inside another. Also, known as nested loops. You can use for() loop to create this pattern as shown below:
public static void drawPyramidPattern() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
There are three loops nested at two level, first is for printing each line and inner loops for printing pattern in each line.
Java Program to Print Pyramid Pattern
Here is our Java program to draw the pyramid pattern as shown in the problem statement. In this program, we have two examples of printing pyramids, in the first, we have a printed pyramid of star characters, while, in the second example, we have drawn a pyramid of numbers.
The key here is to use both print() and println() method from PrintStream class, which is also easily accessible as System.out object. We have also used nested for loop to draw the pyramid which you will often use to solve this kind of problem.
How to print Pyramid Pattern in Java with example
Sample code in Java to Print the Pyramid Pattern
import java.util.Scanner;
/**
* Simple Java Program to draw a pyramid pattern. We have used both
* System.out.println() and System.out.print() methods to draw stars(*)
* in pyramid shape.
*
* @author WINDOWS 8
*
*/
public class PrintPyramidTest {
public static void main(String args[]) {
System.out.println("Pyramid pattern of star in Java : ");
drawPyramidPattern();
System.out.println("Pyramid of numbers in Java : ");
drawPyramidOfNumbers();
}
/**
* This method draws a pyramid pattern using asterisk character. You can
* replace the asterisk with any other character to draw a pyramid of that.
*/
public static void drawPyramidPattern() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
/**
* This method draws a pyramid of numbers.
*/
public static void drawPyramidOfNumbers() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5 - i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print(k + " ");
}
System.out.println();
}
}
}
Output :
Pyramid pattern of star in Java :
*
* *
* * *
* * * *
* * * * *
Pyramid of numbers in Java :
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
That's all about how to print Pyramid using Java pattern. You can find many pattern printing exercises in Java or C++ programming books. You can further refine this program to print any other character instead of * or you can ask the user to enter the number of rows. You can even modify this program to print a pyramid of numbers.
public class simple3 {
public static void main(String[] args)
{
for(int i =0;i<=5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(" "+i);
}
System.out.println(" ");
}
}
}
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
public class simple3 {
public static void main(String[] args)
{
for(int i =0;i<=5;i++)
{
for(int j=0;j<i;j++)
{
System.out.print(" "+i);
}
System.out.println(" ");
}
}
}
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
class Pattern1
{
void main(int n)
{
for(int i=n;i>=1;i--)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int j=i-1;j>=1;j--)
{
System.out.print("*");
}
System.out.println();
}
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
System.out.print(" ");
}
for(int j=1;j<=i;j++)
{
System.out.print("*");
}
for(int j=i-1;j>=1;j--)
{
System.out.print("*");
}
System.out.println();
}
}
}
-------
* *
* * * *
* * * * * * *
In third line if we have to print six star then code is:
for(int i=0;i<4;i++)
{
for(int j=0;j<i*2;j++)
{
print("*");
}
println();
}
------
*
**
***
****
*****
for (int i = 0; i < n; i++)
{
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
or
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
sop("*");
}
sopln();
}
=================
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
int count = 1;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i%2!=0)
{
System.out.print(count++);
}
else
{
System.out.print(count--);
}
}
if(i%2!=0)
{
count = count+3;
}
else
{
count = count+5;
}
System.out.println();
}
====================================
or
int m = 1;
for(int i=1; i<=5; i++)
{
if(i % 2 != 0)
{
for(int j = m;j <= m+4;j++)
{
System.out.print( j+ " ");
}
}
else
{
for(int j = m+4;j >= m;j--)
{
System.out.print( j+ " ");
}
}
System.out.println("");
m= m+5;
}
or
public static void main(int n)
{int c=1,j=0,k=1,l=0;
for(int i=1;i<=n;i++)
{
if(i%2!=0)
{for( j=k;j<=(n*i);j++)
System.out.print(j+" ");
}
else
{
for( k=j, l=(n*i);k<=(n*i);k++,l--)
System.out.print(l+" ");
}
System.out.println();
}
}
or
public static void main(String[] args) {
// TODO Auto-generated method stub
int l = 1;
int n = 4;
for (int i = 1; i <=n; i++) {
if(i%2!=0){
for (int j = 1; j <=n; j++) {
System.out.print(l++);
}
}
else
{
for (int j = 1; j <=n; j++) {
System.out.print(l+4-j);
}
l=l+4;
}
System.out.println("");
}
}
or
package com.test;
public class Test {
public static void main(String[] args) {
int count=0; // TODO Auto-generated method stub
int k;
for(int i=1;i<16;i++)
{
if(count==4)
{
count=1;System.out.println("");
int count1=0;
for(k=i+3;k<=16;k--)
{
i++;
System.out.print(k+" ");
count1++;
if(count1==4)
{
System.out.println("");
break;
}
}
}
else{count++;}
if(i>16)
break;
System.out.print(i+" ");
}
}
}
======
How to print these patterns:
(i)
*
*
*
*
*
(ii)
* *
* *
**
* *
* *
1
for(i=0;i<5;i++)
{
for(j=0;j<1;j++)
System.out.print(" * ");
System.out.println();
}
or1
(I)
class Pattern
{
public static void main (String[] args)
{
for(int i =0;i<=5;i++)
{
System.out.print(" "+i);
System.out.println(" ");
}
}
}
(ii)
for(i=0;i<5;i++)
{
for(j=0;j<2;j++)
System.out.print(" * ");
System.out.println();
}
or public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for( int j=1;j<=2;j++){
if(i%3!=0){
System.out.print("*");
System.out.print(" ");
}
else
{
System.out.print("*");
}}
System.out.println();
}}
============
class test
{
public static void main(String args[])
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int row=sc.nextInt();
for(int i=0;i<=row;i++)
{
for(int j=0;j<=i;j++)
{
if((i%2)!=0)
System.out.print("*");
else
System.out.print("+");
}
System.out.println();
}
}
}
oy=r
Class test
{
Public ststic void main(string args)
{
For(i=1;i<=5;i++)
{
For(j=1;j<=i;j++)
{
If(i%2==0)
{
System.out.println("+");
}else{
System.out.println("*");
}
}
System.out.println(/n);
}
}
}
}
=========================
AAAAA
BBBB
CCC
DD
E
class Test4
{
public static void main(String[] args)
{
int a=65;
for (int i=1;i<=5;i++)
{
for (int j=5;j>=i;j--)
{
char ch=(char)a;
System.out.print(ch);
}
a++;
System.out.println();
}
}
}
=============
(i)12345
1234
123
12
1
For your first question following is the program:
public class PyramidNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i=5;i>=0;i--){
for (int j=1;j<=i;j++){
System.out.print(j);
}//End of j loop
System.out.println("");
}//End of i loop
}
}
For your second question , here you don't have to user nested loop. only one loop is enough , i have commented the first loop in the program:
public class PyramidSamerowNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
//for (int i=5;i>0;i--){
for (int j=5;j>=1;j--){
System.out.print(j);
System.out.print(j);
System.out.print(j);
System.out.print(j);
System.out.println("");
}
System.out.println("");
//}
}
}
====
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
for (int j = 1; j <=5-i ; j++) {
System.out.print(j);
}
System.out.println("");
}
}
===========
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(""+j);
}
System.out.println("");
}
}
}
====================
public static void main(String[] args) {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(""+j);
}
System.out.println("");
}
}
}
========
*****
* *
* *
* *
*****
class Star2
{
public static void main(String[] args)
{
int n=5;
for(int i=0;i<n;i++)
{
int c=0;
for(int j=0;j<n;j++)
{
if(i==0||i==n-1)
{
System.out.print("*");
}
else
{
if(c<2)
{
System.out.print("* ");
c++;
}
}
}
System.out.println();
}
}
}
==================
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
how to create
for(int i=1; i<=5; i++)
{
int s = 4,cnt=1;
int r=i;
int sum = i;
while(cnt= 0 )
{
System.out.print( sum + " ");
sum = sum+s;
s--;
}
}
System.out.println("");
}
U
for(int i=1; i<=5; i++)
{
int s = 4,cnt=1;
int r=i,sum = i;
while(cnt= 0 )
{ System.out.print( sum + " ");
sum = sum+s;
s--;
}
}
System.out.println("");
}
or
while(cnt=0)----- may be it's error.....!!!....???
binduSeptember 29, 2016 at 4:46 AM
for (int i = 1; i < 6; i++) {
if(i==1)
{
System.out.print(i);
//System.out.println();
}
else
{
int sum=i;
int k=4;
System.out.print(i);
for (int j = 1; j < i; j++) {
sum=sum+k;
System.out.print(sum);
k--;
}
}
System.out.println();
==========
1
12
123
1234
12345
1234
123
12
1
plz can any one give code for this program
Reply
Replies
My Mind, My MonologueMay 6, 2016 at 1:13 AM
class Pattern
{
public static void main(String[] args)
{
int ck=0,c=2;
while(c>0)
{
if(ck==0)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
ck++;
}
else
{
for(int i=1,r=5-1;i<=5-1;i++,r--)
{
for(int j=1;j<=r;j++)
{
System.out.print(j);
}
System.out.println();
}
}
c--;
}
}
}
orrrr
package manoj2;
public class Patern {
public static void main(String[] args)
{
int m=5;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(m);
}
m--;
System.out.println("");
}
int n=2;
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print(n);
}
n++;
System.out.println("");
}
}
}
Output:
5
44
333
2222
11111
2222
333
44
5
or
public static void main(String []args){
for (int i = 1; i <= 6; i++)
{
for(int j=1;j<i;j++)
{
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 6; i++)
{
for(int k=1;k<6-i;k++)
{
System.out.print(k);
}
System.out.println();
}
}
===================================