Diamond Pattern Program in Java
Diamond Pattern Program in Java
/*
2. * Diamond Pattern Program in Java
3. */
4.
5. import java.util.Scanner;
6. public class Diamond
7. {
8. public static void main(String args[])
9. {
10. int n, i, j, space = 1;
11. System.out.print("Enter the number of rows: ");
12. Scanner s = new Scanner(System.in);
13. n = s.nextInt();
14. space = n - 1;
15. for (j = 1; j <= n; j++)
16. {
17. for (i = 1; i <= space; i++)
18. {
19. System.out.print(" ");
20. }
21. space--;
22. for (i = 1; i <= 2 * j - 1; i++)
23. {
24. System.out.print("*");
25. }
26. System.out.println("");
27. }
28. space = 1;
29. for (j = 1; j <= n - 1; j++)
30. {
31. for (i = 1; i <= space; i++)
32. {
33. System.out.print(" ");
34. }
35. space++;
36. for (i = 1; i <= 2 * (n - j) - 1; i++)
37. {
38. System.out.print("*");
39. }
40. System.out.println("");
41. }
42. }
43. }
44. Enter the number of rows: 5
45.
46. *
47. ***
48. *****
49. *******
50. *********
51. ***********
52. *************
53. ***************
54. *************
55. ***********
56. *********
57. *******
58. *****
59. ***
60. *
Prepared by Asmare.A(Bsc)
Reverse of a number
class Main {
public static void main(String[] args) {
Output
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}
}
Run Code
Prepared by Asmare.A(Bsc)
Output
29 is a prime number.
import java.util.Scanner;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
Output
Enter a number: 12
12 is even
Prepared by Asmare.A(Bsc)