﻿ Java - Display the number rhombus structure

# Java: Display the number rhombus structure

## Java Conditional Statement: Exercise-25 with Solution

Write a java program to display the number rhombus structure.

Test Data
Input the number: 7

Pictorial Presentation: Sample Solution:

Java Code:

``````import java.util.Scanner;
public class Exercise25 {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Input the number:  ");
int n = in.nextInt();
int count = 1;
int no_of_spaces = 1;
int start = 0;

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

for (int spc = n - no_of_spaces; spc > 0; spc--)
{
System.out.print(" ");
}
if (i < n)
{
start = i;          //for number
no_of_spaces++;    //for spaces
} else
{
start = n * 2 - i;   //for number
no_of_spaces--;      //for space
}
for (int j = 0; j < count; j++)
{
System.out.print(start);
if (j < count / 2)
{
start--;
} else
{
start++;
}
}
if (i < n)
{
count = count + 2;
} else {
count = count - 2;
}

System.out.println();
}
}
}
```
```

Sample Output:

```Input the number:  7
1
212
32123
4321234
543212345
65432123456
7654321234567
65432123456
543212345
4321234
32123
212
1
```

Flowchart: Java Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Java: Tips of the Day

IsPowerOfTwo

Checks if a value is positive power of two.

To understand how it works let's assume we made a call IsPowerOfTwo(4).

As value is greater than 0, so right side of the && operator will be evaluated.

The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.

The result of (value & value) is value. 100 & 100 => 100.

This will value the expression to true as value is equal to value.

```public static boolean isPowerOfTwo(final int value) {
return value > 0 && ((value & (~value + 1)) == value);
}
```

Ref: https://bit.ly/3sA5d4I

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook