TRENDING NEWS

POPULAR NEWS

Have To Draw A Pattern Through Java Using For Loop

How can I print out the a pattern in Java (by using loop)?

Use a loop for the rows with a \n at the end,and inside a loop for the columns that print a start or a whitespace according to weather row+col is even or odd.First step:for(int row=0; row<4; row++){
System.print("\n");
}
compile, run and checkSecond step:for(int row=0; row<4; row++){
for(int col=0;col<9; col++){
System.print("?");
}
System.print("\n");
}
compile, run and checkThird step:for(int row=0; row<4; row++){
for(int col=0;col<9; col++){
if(col+row %2==0)
System.print("*");
else
System.print(" ");
}
System.println("\n");
}
compile, run and checkFinal step: according to the last check swap or do not swap the "*" and " "
compile, run and check [yes, you may have introduced a bug here too!]-- Beware of bugs in the above code; I have only proved it correct, not tried it. [D. Knuth]In facts, Slimy DG helped to correct on bug.

Java for loop *** pattern?

public static void main (String[] args)
{

for (int i = 0;i<10;i++){
for (int j=0;j<10;j++){
if (j>= i){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}

} // end main method

How do I print out a pattern in Java by using loop?

Print Number Pattern in JavaHere you can easily write this program using for loop or while loop. For new line we can use system.out.println().class NumberPatternDemo{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();}}}Output11 21 2 31 2 3 41 2 3 4 51 2 3 4 5 6

Can we draw a pattern with the help of only one loop in C/Java?

In c language very easy to draw a patternin my interview my interviewer say draw a pattern without using loopit is very hard question because it is not possible to draw a pattern without using loop.but one person draw a pattern without using loop.everybody see this person interviewer see this code and he was selectedeverybody was shocked. me see this code so i am shocked this code is very easy codehe writeINPUT#include#includevoid main(){printf(“*”);printf(“*”);printf(“***”);printf(“****”);printf(“*****”);getch();}OUTPUT********

In JAVA How to make a diamond pattern using a grid with colored/pixels?

Height and width are defined methods. I was able to make a grid pattern with did a diagonal line down the middle, code posted.

But how would I make a diamond shape? with a black background and a yellow diamond. I am guessing something like nested loops and if/else statements. Really confused on this one. The way the program is set up when the user types in an input (must be odd) it sets the width and height to be the same so say the user picks 27 it takes that makes the height=27 and width=27.

Could someone please explain how to make a diamond shape with a grid? The grid is a defined class that has awt import.


Here is the code for the diagonal pattern I was able to make:
http://imgur.com/kkbn8OM

here is that outputted grid pattern:
http://imgur.com/Cerzaik

please explain
Thank you

How to draw 2 triangular patterns in Java?

You could just do five println() statements to print out exactly those lines, but that's probably not the point of the exercise.

Think about what you are printing:

row 1: *, 8 spaces, *
row 2: **, 6 spaces, **
row 3: ***, 4 spaces, ***
row 4: ****, 2 spaces, ****
row 5: *****, 0 spaces, *****

So, in row #n, you print "n" asterisks, then 2*(5-n) (or 10-2n) spaces, then "n" more asterisks. I'd make a procedure to do that:

public static void printRow( int rowNo ) {
for (int i=0; ifor (int i=0; i<(10-2*rowNo); ++i) System.out.print(" ");
for (int i=0; iSystem.out.println();
}

Then you can call that in a loop for 1 to 5:

public static void main( String[] args ) {
for (int j=1; j<=5; ++j) printRow( j );
}

The calculation is a little more complex if you want the total number of rows in your figure to be variable (i.e. in addition to the 5-row version above, you could print a 10-row version that was 20 characters wide at the base).

How do I print letter 'M' and 'G' in a star pattern using Java?

To print G :Link : Program to print the pattern 'G' - GeeksforGeeksTo print M :class a{public static void main(String args[]){int i,j;for(i=1;i<=5;i++){for(j=1;j<=5;j++){ if(j==1 || j==5)System.out.print("*");else if(i==2 && (j==2 || j==4))System.out.print("*");else if(i==3 && j==3)System.out.print("*");else System.out.print(" ");}System.out.println();}}}

Question about Java using for loop?

I have to create a program that will prompt the user to enter their name and then printout the user's name with the letters in reverse order.

This is what I have, but it is not right.
I can't get the print the name in reverse order, no idea how to do it.

//Create a program that will prompt the user to enter their name and
//then printout the user's name with the letters in reverse order.

import java.util.Scanner;

public class LoopsProgram
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println ("Enter your full name");
String name = keyboard.nextLine ();
System.out.println(name);
int len;
len = name.length ();
char initial;
initial = name.charAt(len);
for ( int i = len; i <= len ; i --);
{
System.out.print(initial);
}
}

}

TRENDING NEWS