A java program to print diamond of stars? A java program to print pyramid of different numbers in java? Java Program to Print Diamond Pattern. How to print diamond pattern. To Print A Pattern Program In Bluej(java). How To Design and Develop a Java Program. GUI program to display a diamond pattern of stars. Getting different shapes of stars using For Loops. I am using two for loops to print stars in differnt shapes. Introduction. I noticed some one has asked the question about printing collection of stars in rectangle shape. Here I submitted code snippets with description that prints stars in different shapes. In this part I am using only two for loops to print the different shapes. Using the code. I am going to explain the how to get the collections of stars in following shapes. Print stars Diamond program in java,write a java program to print stars,write a java program to print Diamond. Building Java Programs, 3rd. Write a complete Java program. Printing star greater symbol in Java. This uses the Math.abs function to perform the calculation of how many stars to print. Java diamond with numbers. So I was assigned to make a diamond with asterisks in Java and I'm. How to make a diamond using Nested For Loops. I used only two for loops. Left Alignment triangle - Straight and Reverse. 66 Java Patterns Numeric Diamond Part 1 . This video shows the most complex Java Pattern which is a diamond with numbers 3. Right Alignment triangle - Straight and Reverse. Square with crossed lines. LEFT ALIGNMENT - straight. Here I am using number 5 as maximum value. Instead can use a variable to get a value from user Console. Write. Line(. In the outer loop I used the variable . In our case it is up to 5. So we will get the output as follows : ***************LEFT ALIGNMENT - Reverse. If we want to print the stars in reverse triangle then we need to iterate the outer loop from max value to min value. So that next iteration starts in new line. At next iteration **** will come. Similarly at the end of both loops we get the following triangle of stars*************** If we write both codes one after another we will get a triangle facing right hand side.******************************RIGHT ALIGNMENT - Straight. The above code shows the triangle of stars facing right hand side. Similarly if we want to print the triangle of stars facing left hand side, we have the code as follows: Console. Write. Line(. I am printing SPACE if . At the end of 1st iteration of outer for loop. SPACE SPACE SPACE SPACE SPACEIn the 2nd iteration the i value will be 4, now in inner loopif (4> 0) condition true so it prints SPACE.. At the end of the 2nd iteration of outer for loop we have. SPACE SPACE SPACE SPACE *Similarly after running other values of . We print * if condition is true and SPACE if condition fails. Here the outer loop starts from 0 to 5. At first iteration i=0; In inner loop j starts from 1we are checking i less then jso now - -- > if (i< j)if (0< 1) condition true so it prints Star *if (0< 2) condition true so it prints Star *. For this example the odd numbers woule be better to use to see the results. Here also I have two loops and both starts from zero to . Note that I am using OR operator so that if any one conditon gets true the star will be printed. In else part SPACE will be printed. The first condition i==0. This condition gets true at the first iteration of outer loop. So we get top most horizontal line of square.***********************The second condition is j==0, this condition gets true on each first iteration of . Hence in 1. 1th row that is when outer i in 1. Similarly to get the vertical line in middle of square we have the sixth condition j== n/2 which is also 1. To Print Diagonals: when i==j prints diagonal from left to right. If we look at square left to right diagonal will come when i value is equal to j* - -- > i=j=0** - -- > i=j=2******************** - -- > i=j=2. For printing right to left diagonal of square, I am adding i and j and checking the sum is 2. Because when i is zero and j starts from 1 then the condition gets true when j is 2. Like this we will get * - -- -> i val is 0 j val is 2. Conclusion I just used only two for loops to get these shapes. In my next article let us see how can we print diamond shapes of stars using three for loops. STAR PATTERN PROGRAM FOR DIAMOND SHAPE IN JAVAStar pattern in Java in diamond shape import java. Scanner; /** * * @author pankaj */public class Star.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |