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

Diamond Pattern Program in Java

The document contains Java programs demonstrating how to create a diamond pattern, reverse a number, check if a number is prime, and determine if a number is even or odd. Each program includes user input and outputs the results accordingly. The document is prepared by Asmare.A with a BSc qualification.

Uploaded by

Kassa Mamuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Diamond Pattern Program in Java

The document contains Java programs demonstrating how to create a diamond pattern, reverse a number, check if a number is prime, and determine if a number is even or odd. Each program includes user input and outputs the results accordingly. The document is prepared by Asmare.A with a BSc qualification.

Uploaded by

Kassa Mamuka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

/*
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) {

int num = 1234, reversed = 0;

System.out.println("Original Number: " + num);

// run loop until num becomes 0


while(num != 0) {

// get last digit from num


int digit = num % 10;
reversed = reversed * 10 + digit;

// remove the last digit from num


num /= 10;
}

System.out.println("Reversed Number: " + reversed);


}
}

Output

Reversed Number: 4321

Checks weather the number is prime or not

public class Main {

public static void main(String[] args) {

int num = 29;


boolean flag = false;

// 0 and 1 are not prime numbers


if (num == 0 || num == 1) {
flag = true;
}

for (int i = 2; i <= num / 2; ++i) {

// condition for nonprime number


if (num % i == 0) {
flag = true;
break;
}
}

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.

//Checks the number even or odd

import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: ");


int num = reader.nextInt();

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)

You might also like