a program on java which can draw for any number odd n a diamond like n=5 + +++ +++++ +++ + n + on the line (n+1)/2 and it decrease and increases as above ,just give me the idea of how to write he space -j+4 time System.out.print(" "); (-j+4) times ! please any hints are welcomed
for example n=7 out put is + 3 spaces here from the begining +++ 2 here +++++ 1here ++++++ non hrer +++++ 1 +++ 2 + 3
i'd make 2 loops (1 for the decreasing part and 1 for the increasing part of the diamond). in each loop 2 loops for the + and the space. For the increasing main loop use integer like i = 1, if i < lets say 10, i+=2 it will start with 1 +, next 3 +, next 5 +, 7+ 9+ here it stops because 11+ is higher than 10. so the increasing part of the diamond has n=9 In your case n=5 so you'd set 6 instead of 6. for the decreasing (lower) part of the diamond use same loop but now set i to a value and decrease the integer with 2. when you set the integer of the upper part to 6, you will get n=5, So you will need so set the lower part on 3 because under the 5+ row you need 3+. Try this one: class Diamond { public static void main(String[] args) { int x = 6; //the integer for the upper part int y = 3; // the integer for the lower part for (int i = 1; i < x; i += 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("+"); System.out.print("\n"); } for (int i = y; i > 0; i -= 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("+"); System.out.print("\n"); } } } It will output: + +++ +++++ +++ + When you want n=7 you need to set x to 8 and y to 5
you can make it more easy add this int n = 5; int x = n+1; //the integer for the upper part int y = n-2; // the integer for the lower part It will automatically set x to 6 and y to 3 when you say n=5 it will set x to 8 and y to 5 now you only have to fill in n
i meant n=7 in last post :P
@thomaster there is just one thing my teacher told me that it's not general and I have to make more generalized, because it doesn't work with big numbers like 10001 where you can see just the right side of the diamond but I like this way though thank you for helping ,..
package test; import java.io.InputStreamReader; import java.util.Scanner; public class Test { private static int n; private static String data; private static int input; private static int i; public static void main(String[] args) throws Exception { System.out.println("Helloo I am mounir and I am coming very fast"); System.out.println("write your name"); //for fun~~!! Scanner name= new Scanner(System.in); data = name.nextLine(); System.out.println("heloo " + data); System.out.println("ok " + data + " input an odd number to get a diamond"); Scanner sc= new Scanner (System.in); int n= sc.nextInt(); int x = n+1; //the integer for the upper part int y = n-2; // the integer for the lower part for (int i = 1; i < x; i += 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("+"); System.out.print("\n"); } for (int i = y; i > 0; i -= 2) { for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); for (int j = 0; j < i; j++) System.out.print("+"); System.out.print("\n"); } if (n%2==0 ){ System.out.println("will nice if you input an odd number any way see you "+data); } else { System.out.println("hope you like this thank you " +data+ " see you soon"); } } }
uhh if you replace this: for (int j = 0; j < 9 - i / 2; j++) System.out.print(" "); by this: (for both space loops) for (int j = 0; j < n - i / 2; j++) System.out.print(" "); will it then work?
perfectly did work thank you again .
Join our real-time social learning platform and learn together with your friends!