0% found this document useful (0 votes)
50 views

Oop in Java Diamond

The document contains code for a Java program that prints a diamond pattern of asterisks of a given size. It prompts the user to enter the number of rows, then uses nested for loops to print spaces and asterisks in increasing and decreasing patterns to create the shape of a diamond on each line.

Uploaded by

ali jawad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Oop in Java Diamond

The document contains code for a Java program that prints a diamond pattern of asterisks of a given size. It prompts the user to enter the number of rows, then uses nested for loops to print spaces and asterisks in increasing and decreasing patterns to create the shape of a diamond on each line.

Uploaded by

ali jawad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. import java.util.

Scanner;
2. public class Diamond
3. {
4. public static void main(String args[])
5. {
6. int n, i, j, space = 1;
7. System.out.print("Enter the number of rows: ");
8. Scanner s = new Scanner(System.in);
9. n = s.nextInt();
10. space = n - 1;
11. for (j = 1; j <= n; j++)
12. {
13. for (i = 1; i <= space; i++)
14. {
15. System.out.print(" ");
16. }
17. space--;
18. for (i = 1; i <= 2 * j - 1; i++)
19. {
20. System.out.print("*");
21. }
22. System.out.println("");
23. }
24. space = 1;
25. for (j = 1; j <= n - 1; j++)
26. {
27. for (i = 1; i <= space; i++)
28. {
29. System.out.print(" ");
30. }
31. space++;
32. for (i = 1; i <= 2 * (n - j) - 1; i++)
33. {
34. System.out.print("*");
35. }
36. System.out.println("");
37. }
38. }
39. }

Output:
$ javac Diamond.java
$ java Diamond
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Program

1. import java.util.Scanner;
2.
3. public class DifferentPatternPrograms1 {
4. public static void main(String args[])
5. {
6. int n,i,j,k,l,m,p,q,r,s;
7. Scanner sc=new Scanner(System.in);
8. System.out.println("Enter the n values");
9. n=sc.nextInt();
10. p=n;
11. q=n;
12. for(i=n;i>=1;i--)
13. {
14. for(j=1;j<=i;j++)
15. {
16. System.out.print("*");
17. }
18. for(k=p*2;k<n*2-1;k++)
19. {
20. System.out.print(" ");
21. }
22. for(l=i;l!=0;l--)
23. {
24. if(l==n)
25. {
26. continue;
27. }
28. System.out.print("*");
29. }
30. p--;
31. System.out.println();
32. }
33. for(i=1;i<=n;i++)
34. {
35. for(j=1;j<=i;j++)
36. {
37. System.out.print("*");
38. }
39. for(k=q*2-2;k>1;k--)
40. {
41. System.out.print(" ");
42. }
43. for(m=i;m!=0;m--)
44. {
45. if(m==n)
46. {
47. continue;
48. }
49. System.out.print("*");
50. }
51. System.out.println();
52. q--;
53. }
54.
55. }
56.
57.
58. }

Output
Enter the n values
8
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
1. Left Triangle

public class LeftTriangle {


public static void main(String args[]){
int num = 5;

for(int i = 1; i <= num; i++) {


for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}

Output:

2. Inverted Left Triangle

public class InvertedLeftTriangle {


public static void main(String args[]){
int num = 5;

for(int i = num; i >= 1; i--) {


for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
view rawInvertedLeftTriangle.java hosted with ❤ by GitHub

Output:

3. Triangle

public class Triangle {


public static void main(String args[]){
int num = 5;
int count = num - 1;

for(int i = 1; i <= num; i++) {


for(int j = 1; j <= count; j++) {
System.out.print(" ");
}

count--;

for(int k = 1; k <= (2 * i) - 1; k++) {


System.out.print("*");
}

System.out.println("");
}
}
}
view rawTriangle.java hosted with ❤ by GitHub

Output:

4. Right Triangle

public class RightTriangle {


public static void main(String args[]){
int num = 5;

for(int i = 1; i <= num; i++) {


for(int j = 1; j <= (num - i); j++) {
System.out.print(" ");
}
for(int k = 1; k <= i; k++) {
System.out.print("*");
}
System.out.println("");
}
}
}
view rawRightTriangle.java hosted with ❤ by GitHub

Output:

5. Diamond

public class Diamond {


public static void main(String args[]){
int num = 5;
int count = num - 1;
for(int i = 1; i <= num; i++) {
for(int j = 1; j <= count; j++) {
System.out.print(" ");
}
count--;

for(int k = 1; k <= (2 * i) - 1; k++) {


System.out.print("*");
}

System.out.println("");
}
count = 1;

for(int i = 1; i <= (num - 1); i++) {


for(int j = 1; j <= count; j++) {
System.out.print(" ");
}

count++;

for(int k = 1; k <= (2 * (num - i)) - 1; k++) {


System.out.print("*");
}

System.out.println("");
}
}
}
view rawDiamond.java hosted with ❤ by GitHub
Output:

You might also like