Thanks for reading my question:
public class Gugudan_array {
public static void main(String[] args) {
for(int i = 2; i<10; i++) {
for(int j = 1; j<10; j++) {
System.out.println(i * j);
}
System.out.println();
}
}
}
In the multiplication table above, result comes correctly from 2-9, however:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h++) {
for(int i = 2; i < 10; i++) {
for(int j = 1; j<10; j++) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a++) {
System.out.println(result[a]);
}
}
}
In this code with array, the result comes out only 81.
What have I done wrong?
Thank you!
I'll first explain why your code doesn't work, and then go over a correct solution:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int h = 0; h < result.length; h++) {
for(int i = 2; i < 10; i++) {
for(int j = 1; j<10; j++) {
result[h] = (i * j);
}
}
}
for(int a = 0; a < result.length; a++) {
System.out.println(result[a]);
}
}
}
The main problem lies in this chunk of code:
for(int i = 2; i < 10; i++) {
for(int j = 1; j<10; j++) {
result[h] = (i * j);
}
}
Here, you are constantly overwriting the value of result[h], so that once the loop ends and both i = 9, and j = 9, the code will execute result[h] = 9 * 9 and then continue on to the next h.
My solution:
public class Gugudan_array {
public static void main(String[] args) {
int[] result = new int[10];
for(int i = 2; i < 10; i++) {
for(int j = 1; j<10; j++) {
result[i - 2] = (i * j);
}
}
for(int a = 0; a < result.length; a++) {
System.out.println(result[a]);
}
}
}
Output:
18
27
36
45
54
63
72
81
0
0
First, notice how I completely got rid of the h loop. That is because we can make the index in terms of i. When we determine the first number, when i = 2, we want to store that number in the 0th index of our array. Similarly, when we get our second number, when i = 3, we want to store the result in the 1st index of our array.
To summarize, whenever we calculate a result, we will want to store it in the i - 2th index of our array.
Better Solution using 2D arrays:
int[][] result = new int[8][9];
for(int i = 2; i < 10; i++) {
for(int j = 1; j<10; j++) {
result[i - 2][j - 1] = (i * j);
}
}
for(int a = 0; a < result.length; a++) {
for(int b = 0; b < result[a].length; b++){
System.out.print(result[a][b] + " ");
}
System.out.println();
}
Output:
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
Note: If you want the output to match your original code, change System.out.print(result[a][b] + " "); to System.out.println(result[a][b])
It would make the most sense to store a multiplication table in a 2D array.
This code works by mapping i * j to the [i - 2][j - 1]'th element of the 2D array, so that 2 * 1, will end up in result[0][0]
I hope this made sense! Please let me know if you need any further help or clarification!
You have to increase h variable after all iteration. Here is whole code:
public static void main(String[] args) {
int[] result = new int[10];
for (int h = 0; h < result.length; h++) {
for (int i = 2; i < 10; i++) {
for (int j = 1; j < 10; j++) {
result[h] = (i * j);
if (h < 9) {
h++;
}
}
}
}
for (int a = 0; a < result.length; a++) {
System.out.println(result[a]);
}
}
The issue is that your current setup with 3 nested loops, the outer loop runs 10 times, the middle loop runs 8 times for each other loop, and the inner loop runs 9 times for each middle loop, which gives a total of 10x8x9=720 times... but I suspect you just want 72 results like you show in your first example, so we need to remove the outer loop first for(int h = 0; h < result.length; h++) {.
Now the issue is that we need to store all 72 results but your current array only fits 10 results int[] result = new int[10];, we can solve this with a bigger array:
int[] result = new int[8 * 9]; //72 spaces
Here is a working solution with a long array and a counter that keeps track of where to store the value, note the code comments for extra details:
//Your loops calculate 8 * 9 results, so you need an array with 72 spaces
int[] result = new int[72];
//Use a variable to track the array location
int counter = 0;
//Youn only need two loops
for(int i = 2; i < 10; i++) {
for(int j = 1; j< 10; j++) {
//Save the result in the correct location
result[counter] = (i * j);
//Incriment the counter
counter++;
}
}
//Print the stored results
for(int a = 0; a < result.length; a++) {
System.out.println(result[a]);
}
Some further important reading from the official Java guide on Arrays:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
A better solution would be to use a 2D array.
Hey I' m trying to create a pattern that's suppose to output multiples of 5 in a pattern like this:
5 10 15 20 25
30 40 45 50
55 60 65
70 75
80
but my output is like this
5 10 15 20 25
30 35 40 45
50 55 60
65 70
75
but when i put asterisks in the print :
*****
****
***
**
*
here's my code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int n = 5;
int x =5;
for(int i = n; i>=1; i--){
for(int j = n-1; j>=i; j--){
System.out.print(" ");
}
for(int k = 1; k<=i; k++){
System.out.print(x+" ");
x+=5;
}
System.out.println();
}
}
}
can somebody help me? i spent almost 3 hours trying to figure this out. any help would be appreciated. thanks!
The cleanest way to achieve what you want is to do is by using printf as shown below:
public class Main {
public static void main(String[] args) {
int n = 5;
int x = 5;
for (int i = n; i >= 1; i--) {
for(int j=1;j<n-i+1;j++)
System.out.printf("%3s"," ");
for (int k = 1; k <= i; k++) {
System.out.printf("%3d",x);
x += 5;
}
System.out.println();
}
}
}
Output:
5 10 15 20 25
30 35 40 45
50 55 60
65 70
75
Update:
If you are not comfortable with printf, you can use the following solution:
public class Main {
public static void main(String[] args) {
int n = 5;
int x = 5;
String space=" ";
for (int i = n; i >= 1; i--) {
for(int j=1;j<(n-i)*3;j++)
System.out.print(space);
for (int k = 1; k <= i; k++) {
System.out.print(x+space);
x += 5;
}
System.out.println();
}
}
}
Output:
5 10 15 20 25
30 35 40 45
50 55 60
65 70
75
You write three characters at a time (space, first digit, second digit), so you need to print three spaces to have the correct results.
Basically your same code, but with three spaces int the System.out.print:
for(int i = n; i>=1; i--){
//This is the same loop, but written in a different way.
for(int j = 0; i < n-i; j++){
System.out.print(" "); //THREE SPACES HERE
}
for(int k = 1; k<=i; k++){
System.out.print(x+" ");
x+=5;
}
System.out.println();
}
Now, you are also printing the 5 at the start, which is one digit long, so you need another space before. So you need a conditional structure in order to do this:
//This is the same loop, but written in a different way.
for(int j = 0; i < n-i; j++){
if(j == 0 && x <= 5) //First cycle (remember 5, which is 1 digit)
System.out.print(" "); //TWO SPACES HERE
else
System.out.print(" "); //THREE SPACES HERE
}
Note that this only works with 2 digits numbers so you need to do a similar thing like the one we used with the number five if you're gonna use also 3 or more digits numbers.
I think this exercise is also a good moment to practice with System.out.printf()
Here is how i would do this:
public static void main(String[] args) {
int x = 5;
int n = 35;
int max = calculateTotalNumbers(n) * x; // calculate the highest number
int maxLenght = String.valueOf(max).length(); //calculate the numbers of digits in the highest number
for(int i = n; i>=1; i--){
for (int j = 0; j < n - i; j++) {
//makes sure you will have the same length spaces everywhere
System.out.printf("%1$"+maxLenght+"s ", "");
}
for (int k = 1; k <= i; k++) {
//this formatting will ensure every digit has the same length with padding included. Check out the javadoc of System.out.printf();
System.out.printf("%" + maxLenght + "d ", x);
x += 5;
}
System.out.println();
}
}
// this method will calculate the total number of numbers you will print in the triangle
private static int calculateTotalNumbers(int n) {
if (n <= 0) return 0;
else return (n + calculateTotalNumbers(n -1));
}
public class temp {
public static void main(String[] args) {
int n = 5;
int p = 1;
for (int i = 0; i < n ; i ++){
String curr = "";
for (int j = 0 ; j < n - (n - i); j ++){
curr += " ";
if (j > 0) {
curr += " ";
}
}
for (int j = i; j < n; j ++){
curr += Integer.toString(n * p);
curr += " ";
p += 1;
}
System.out.println(curr);
}
}
}
**This is my code that look a way to find a sum in an array **
public class Piecedemonei {
public static void recherche(int[] tab) {
int num;
int quo;
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++){
if (tab[j] <= somme) {
num = somme / tab[j];
System.out.print(num+"*" + " " + tab[j]);
System.out.println(" ");
somme -= num * tab[j];
j=0;
}
}
}
}
public static void main(String[] args) {
int aba[] = { 7, 6, 4, 5 };
recherche(aba);
System.out.println();
}
}
Output
Solution 0
2 7 + 1 4
Solution 1
Solution 2
Solution 3
**I am looking a way to improve my code so that the output looks like this : **
Solution 0
2*7 + 1*4
Solution 1
3*6
Solution 2
4*4 + 1*4
Solution 3
**Why cant I reset my loop so that it does the same thing again and again ? **
Change
int somme = 18;
for (int i = 0; i <= tab.length - 1; i++) {
System.out.println("Solution " + i);
for (int j = 0; j < tab.length; j++){
...
to
for (int i = 0; i <= tab.length - 1; i++) {
int somme = 18;
System.out.println("Solution " + i);
for (int j = i; j < tab.length; j++) {
...
Notice the int j = i in the inner loop.
Why does this code show the error message java.lang.ArrayIndexOutOfBoundsException: -2146737495!!
I think that the size of array is sufficient enough.
class Main {
static int[] ara = new int[1000010];
public static void seive(){
for(int i = 0; i <= 1000000; i++)
ara[i] = 0;
ara[1] = 1;
for(int i = 2; i <= 1000000; i ++)
{
if(ara[i] == 0)
{
for(int j = i * i; j <= 1000000; j += i)
ara[j] = 1;
}
}
}
public static void main(String[] args)
{
seive();
}
}
You are doing: j = i * i
When I log the code with this piece:
for(int j = i * i; j <= 1000000; j += i)
{
System.out.println("i: " + i + " -j: " + j);
ara[j] = 1;
}
I ge this output:
i: 997 -j: 997000
i: 997 -j: 997997
i: 997 -j: 998994
i: 997 -j: 999991
i: 46349 -j: -2146737495
The j = i * i (which will be -2146737495) throws an exception.
Java arrays have size limited first by memory and after by Integer.MAX_VALUE.
You are passing the Integer.MAX_VALUE in the array position.
But value -2146737495 is negative, why???.
Because Integer.MAX_VALUE + 1 = Integer.MIN_VALUE
So I was assigned to make a diamond with asterisks in Java and I'm really stumped. Here's what I've come up with so far:
public class Lab1 {
public static void main(String[] args) {
for (int i = 5; i > -5; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j >= i; j--) {
System.out.print(" ");
}
System.out.println("*");
}
}
}
In order to make a diamond you need to set spaces and stars in shape. I have made this simple program using only nested loops since I am a beginner.
public class Diamond {
public static void main(String[] args) {
int size = 9,odd = 1, nos = size/2; // nos =number of spaces
for (int i = 1; i <= size; i++) { // for number of rows i.e n rows
for (int k = nos; k >= 1; k--) { // for number of spaces i.e
// 3,2,1,0,1,2,3 and so on
System.out.print(" ");
}
for (int j = 1; j <= odd; j++) { // for number of columns i.e
// 1,3,5,7,5,3,1
System.out.print("*");
}
System.out.println();
if (i < size/2+1) {
odd += 2; // columns increasing till center row
nos -= 1; // spaces decreasing till center row
} else {
odd -= 2; // columns decreasing
nos += 1; // spaces increasing
}
}
}
}
As you can see nos is the number of spaces. It needs to be decreased until the center row, and the number of stars needs to be increased but after the center row it's the opposite, i.e spaces increase and stars decrease.
size can be any number. I set it to 9 over here so I will have a size 9 star that is 9 rows and 9 columns max... number of space (nos) will be 9/2 = 4.5 .
But java will take it as 4 because int can not store decimal numbers and the center row will be 9/2 + 1 = 5.5, which will result in 5 as int.
So first you will make rows... 9 rows hence
(int i=1;i<=size;i++) //size=9
then print spaces like I did
(int k =nos; k>=1; k--) //nos being size/2
then finally stars
(int j=1; j<= odd;j++)
once the line ends...
You can adjust stars and spaces using an if condition.
for (int i = 0; i < 5; i++)
System.out.println(" *********".substring(i, 5 + 2 * i));
for (int i = 5; i > 0; i--)
System.out.println(" **********".substring(i - 1, 5 + (2 * i) - 3));
Note: Using Count Global variable we can manage space as well as star increment and decrement.
import java.util.*;
public class ParamidExample {
public static void main(String args[]) {
System.out.println("Enter a number");
Scanner sc = new Scanner(System.in);
int no = sc.nextInt();
int count = 1;
for (int i = 1; i <= 2 * no - 1; i++) {
for (int j = count; j <= no; j++) {
System.out.print(" ");
}
for (int k = 1; k <= count * 2 - 1; k++) {
System.out.print("* ");
}
if (i < no)
count++;
else
count--;
System.out.println("");
}
}
}
public class MyDiamond {
public static void main(String[] args) {
//Length of the pyramid that we want.151 is just an example
int numRows = 151;
//midrow is the middle row and has numRows number of *
int midrow = (numRows + 1) / 2;
int diff = 0;
for (int i = 1; i < numRows + 1; i++) {
for (int j = 1; j < numRows + 1; j++) {
if (((midrow - diff) <= j && (j <= midrow + diff))) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
if (i < midrow) {
diff++;
} else {
diff--;
}
}
}
}
public class Diamond {
//Size of the diamond
private int diagonal;
public Diamond(int diagonal) {
this.diagonal = diagonal;
}
public void drawDiamond() {
int n = diagonal;
for (int i = n / 2; i >= -n / 2; i--) {
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
for (int j = 1; j <= (n - i * 2) && i >= 0; j++) {
System.out.print("*");
}
for (int k = 1; k <= -i && i < 0; k++) {
System.out.print(" ");
}
for (int j = (n / 2) * 2 + 2 * i; j >= -(n % 2 - 1) && i < 0; j--) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
//You pass diamond size here in the constructor
Diamond a = new Diamond(21);
a.drawDiamond();
}
}
The main problem is parity of diagonal.
If it's even you can't properly draw top asterisk. So there is 2 types of diamonds - with even and odd diagonal (with 2 and 1 asterisk at the top).
I can see what you are trying to do and this is a pretty neat way to think about the diamond.
You will have some issues with the j counter when i goes negative..look at how to use Math.abs()
Also try writing some pseudo code in basic steps with comments to get the pattern clear:
//print 5 spaces + 1 star
//print 4 spaces + 2 stars
//print 3 spaces + 3 stars
//print 2 spaces+ 4 stars
.
.
.
//print 5 spaces + 1 star
Then, literally substitute variables (j and i) for the numbers.
You now have a model. This is often the hardest part in programming..getting the model right. Only jump into coding when you have a good idea for how the model works.
Once you have the variables substituted, you can try to convert the whole thing into an automated loop.
import java.util.Scanner;
public class MakeDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Let's Creat Diamonds");
System.out.println("If number increases Diamonds gets bigger. Please input number lager than 1 : ");
int user_input = sc.nextInt(); //gets user's input
System.out.println("");
int x = user_input;
int front_space = -5;
for (int i = 0; i < 2 * user_input + 1; i++) {
for (int a = front_space; a < Math.abs(i - user_input); a++) {
System.out.print(" "); //Change a bit if diamonds are not in good shape
}
if (i < user_input + 1) {
for (int b = 0; b < 2 * i + 1; b++) {
System.out.print("* "); //Change a bit if diamonds are not in good shape
}
} else if (i > user_input) {
for (int c = 0; c < 2 * x - 1; c++) {
System.out.print("* "); //Change a bit if diamonds are not in good shape
}
x--;
}
System.out.print('\n');
}
System.out.println("\nRun Again? 1 = Run, 2 = Exit : ");
int restart = sc.nextInt();
System.out.println("");
if (restart == 2) {
System.out.println("Exit the Program.");
System.exit(0);
sc.close();
}
}
}
}
When making diamonds with while or for loops.
I think using 'Math.abs' will be the simplest way to make it.
You can put number by Scanner, and when input number increases diamonds will get bigger.
I used Eclipse to make this program.
so, the Space will be differ by your running environment. like another IDE, CMD or Terminal. if diamonds are not in good shape. Just change spaces.
package com.DiamondPrintingProgram;
import java.util.Scanner;
public class DiamondPrintingProgram {
public static void main(String[] args) {
int num = getInput();
int middle = (int) num / 2 + 1;
printOutput(num,middle);
}
public static int getInput() {
Scanner sc = new Scanner(System.in);
int num;
System.out.print("Enter a odd number: ");
while (true) {
num = sc.nextInt();
if (num % 2 != 0) {
break;
}
System.out.print("Please Enter a ODD NUMBER: ");
}
return num;
}
private static void printOutput(int num, int middle){
char asterisk = '*';
for (int j = 0; j < num; j++) {
for (int i = 1; i <= num; i++) {
if (j < middle) {
if ((i < (middle - j) || i > (middle + j))) {
System.out.print(' ');
} else {
System.out.print(asterisk);
}
} else {
if ((i < (j - middle + 2)) || (i > (2 * num - j - middle))) {
System.out.print(' ');
} else {
System.out.print(asterisk);
}
}
}
System.out.println();
}
}
}
I have the exact classwork in my university which also requires me to finish it in 3 for loops.
And this is how I did it.
In a simple way of explanation, I divide the diamond into two parts.
no. of lines
no. of spaces
no. of stars
total no. of slots
1
4
1
5
2
3
3
6
3
2
5
7
4
1
7
8
5
0
9
9
6
1
7
8
7
2
5
7
8
3
3
6
9
4
1
5
I want to find the no. of slots and the no. of spaces with each line, then allocating the no. of stars would be really easy.
And considering the no. of slots, line 1 - 5 and line 6 - 9 would become two separate groups(i.e. middleLine).
The equation of the no. of slots of the first half would be numberOfLines(i.e. i) + (middleLine - 1) where middleLine - 1 would be 4 when the maxNumberOfLines is 9.
The equation of the no. of slots of the last half would be middleLine(i.e. replacement of I) + (middleLine - 1)(i.e. same as above) - (i - middleLine) where i - middleLine would be -1 when i = 6.
And for the space, the first half would be middleLine - i and last half would be i - middleLine, which are exactly in a negative relationship(or symmetrical regarding their slopes).
public class printOutDiamondWith3Loops {
public static void main(String[] args) {
int userInput = 9;
double maxNumberOfLines = userInput;
// double type is used instead of integer type in order to prevent removal of remainder when a division performed.
double middleLine = Math.ceil(maxNumberOfLines/2);
// Print out the diamond.
for (int i = 1; i <= maxNumberOfLines; i++) {
// Determine the number of lines, which is also the maximum number of slots (the line in the middle).
if (i <= middleLine) {
// Separate the whole diamond into two parts(as mentioned above).
for (int j = 1; j <= i + ((middleLine - 1)); j++) {
// Determine the no. of slots in each line from line 1 to 5.
if (j <= middleLine - i) {
// Determine the no. of spaces and stars.
System.out.print(" ");
} else {
System.out.print("*");
}
}
} else { // i > middleLine
for (int k = 1; k <= (middleLine + (middleLine - 1)) - (i - middleLine); k++) {
// Determine the no. of slots in each line from line 6 to 9.
// For better understanding, I did not simplify the above condition.
// Noticeably, the first middleLine in above for loop is a replacement of i.
if (k <= i - middleLine) {
// Determine the no. of spaces and stars.
System.out.print(" ");
} else {
System.out.print("*");
}
}
}
System.out.println();
}
}
With such a framework, it is much easier to make further changes, such as letting the user input the no. of lines they want.
Hope this answer could help you.
I could lend you a more detailed version of my work, though not necessarily in need(the above explanation already explains the concepts): print-Out-Diamond-With-3-Loops-Advanced-Version.java
You can print a diamond of asterisks (mathematical operators) as follows:
int m = 4;
int n = 4;
for (int i = -m; i <= m; i++) {
for (int j = -n; j <= n; j++) {
int val = Math.abs(i) + Math.abs(j);
System.out.print(val > Math.max(m, n) ? " " : "∗");
if (j < n) {
System.out.print(" ");
} else {
System.out.println();
}
}
}
Output:
∗
∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗ ∗ ∗
∗ ∗ ∗ ∗ ∗
∗ ∗ ∗
∗
Try this
public class Main {
public static void main(String[] args) {
int n = 50;
int space = n - 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < space; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println("");
space--;
}
space = 0;
for (int i = n; i > 0; i--) {
for (int j = 0; j < space; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println("");
space++;
}
}
}
java-11
Using String#repeat introduced as part of Java-11, you can do it using a single statement inside a single loop.
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "*".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
***
*****
*******
*********
*******
*****
***
*
By changing the space, you can also print a variant of the diamond:
public class Main {
public static void main(String[] args) {
final int MID_ROW_NUM = 5;
for (int i = 1 - MID_ROW_NUM; i < MID_ROW_NUM; i++) {
System.out.println(" ".repeat(Math.abs(i)) + "* ".repeat((MID_ROW_NUM - Math.abs(i)) * 2 - 1));
}
}
}
Output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
import java.util.Scanner;
public class Diamond {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int input = in.nextInt();
int min = 1;
for (int i = 0; i < input; i++) {
for (int j = input - 1; j > i; j--) {
System.out.print(" ");
}
for (int k = 0; k < min; k++) {
if (k % 2 == 0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
min += 2;
System.out.println();
}
int z = input + input - 3;
for (int i = 1; i < input; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < z; k++) {
if (k % 2 == 0) {
System.out.print("*");
} else {
System.out.print(".");
}
}
z -= 2;
System.out.println();
}
}
}
This should work. You probably only need most of the methods and printDiamond(_);
import java.util.Scanner;
public class StarsTry {
public static void main(String[] args) {
int reader;
Scanner kBoard = new Scanner(System.in);
do {
System.out.println("Insert a number of rows: ");
reader = kBoard.nextInt();
printDiamond(reader);
} while (reader != 0);
}
public static void printStars(int n) {
if (n >= 1) {
System.out.print("*");
printStars(n - 1);
}
}
public static void printTopTriangle(int rows) {
int x = 1;
for (int j = (rows - 1); j >= 0; j--, x += 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printSpaces(int n) {
if (n >= 1) {
System.out.print(" ");
printSpaces(n - 1);
}
}
public static void printBottomTriangle(int rows, int startSpaces) {
int x = 1 + (2 * (rows - 1));
for (int j = startSpaces; j <= (rows) && x > 0; j++, x -= 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printBottomTriangle(int rows) {
int x = 1 + (2 * (rows - 1));
for (int j = 0; j <= (rows - 1) && x > 0; j++, x -= 2) {
printSpaces(j);
printStars(x);
System.out.print("\n");
}
}
public static void printDiamond(int rows) {
printTopTriangle((int) rows / 2 + 1);
printBottomTriangle((int) rows / 2, 1);
}
}
import static java.lang.System.out;
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
sc.close();
Diamond d = new Diamond();
d.upperDiamond(row);
d.lowerDiamond(row - 2);
}
public void upperDiamond(int a) {
for (int i = 0; i < a; i++) {
for (int j = a - 1; j > i; j--)
out.print(" ");
for (int k = 0; k < 2 * i - 1; k++)
out.print("*");
out.print("\n");
}
}
public void lowerDiamond(int b) {
for (int i = 0; i < b; i++) {
for (int j = 0; j <= i; j++)
out.print(" ");
for (int k = 0; k < 2 * (b - i) - 1; k++)
out.print("*");
out.print("\n");
}
}
}
public class Main {
public static void main(String[] args) {
int number = 23,l =1,diff = 1,rem = number/2,rep = 0;
for(int i=1;i<=number;i++){
if(i < number/2 +1){
for(int k=rem;k>=1;k--)
System.out.print(" ");
for(int j=1;j<=l;j++)
System.out.print("*");
diff = 2;
rem -= 1;
}
if(i >= number/2 +1){
for(int k=0;k<rep;k++)
System.out.print(" ");
for(int j=1;j<=l;j++)
System.out.print("*");
diff =3;
rep += 1;
}
System.out.println();
l = diff == 2 ? (l + 2) : (l - 2);
}
}
}
//Suitable for only Odd numbers...
public class Main {
private static int l =1;
public static void main(String[] args) {
int number =9;
for(int i=1;i<=2*number -1;i++){
if(i<=number){
for(int j=1;j<=(number-i);j++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print("* ");
}
if(i>number){
for(int j=1;j<=i-number;j++)
System.out.print(" ");
for(int j=1;j<=number-l;j++)
System.out.print("* ");
l += 1;
}
System.out.println();
}
}
}
class Inc_Dec {
public static void main(String[] args) {
int le = 11;
int c = 0;
int j1 = (le / 2) + 1;
int j2 = le - j1;
for (int i = 1; i <= le; i++) {
if (c < j1) {
for (int k = (j1 - i); k > 0; k--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
c++;
System.out.println();
} else {
for (int k = (i - j1); k > 0; k--) {
System.out.print(" ");
}
for (int j = (le - i + 1); j > 0; j--) {
System.out.print("*" + " ");
}
System.out.println();
}
}
}
}
package practice;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
for (int i = 0; i <= 10; i++) {
if (i <= 5) {
for (int k = 1; k <= 5 - i; k++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(" *");
}
}
if (i > 5) {
for (int k = 0; k <= i - 6; k++) {
System.out.print(" ");
}
for (int j = 0; j <= 10 - i; j++) {
System.out.print(" *");
}
}
System.out.println();
}
}
}