Javatpoint Logo
Javatpoint Logo

Seven Segment Display Problem in Java

Seven segments display is an output display device. It provides a way to display information in the form of images, text, or decimal numbers. It is an alternative to complex dot matrix displays. Seven segment displays are widely used in digital or electronic devices like calculators, watches, game consoles, electronic meters, etc. In this section, we will discuss how to represent a number in seven-segment display in Java.

Seven Segment Display Problem in Java

What is seven segment display?

It is a display device that forms a number using seven segments of LEDs (horizontal and vertical bars) that look like the number 8. These seven segments can form any number from 0 to 9. For example, consider the following figure.

Working of Seven Segment

The following table depicts the digits and corresponding segment code where 0 represents no power supply and 1 represents power supplied.

Digit No. of Segment to
Display a Number
Seven Segment Code
a b C d e f g
0 6 1 1 1 1 1 1 0
1 2 0 1 1 0 0 0 0
2 5 1 1 0 1 1 0 1
3 5 1 1 1 1 0 0 1
4 4 0 1 1 0 0 1 1
5 5 1 0 1 1 0 1 1
6 6 1 0 1 1 1 1 1
7 3 1 1 1 0 0 0 0
8 7 1 1 1 1 1 1 1
9 6 1 1 1 0 0 1 1

We observe that the minimum number of segments to represent a digit is 2.

Seven Segment Display Problem in Java

Before moving to the solution, first, we will see the Java program that displays the seven-segment digits.

Java Program to Represent a Digit in Seven Segment Form

There are the following two ways to print digits in seven-segment form:

  • Using switch case
  • Using Hexadecimal Encoding

Using switch Case

It is the naïve approach to print digit as seven segment display. In the following Java program, we have read a digit from the user and switched the corresponding case that prints the digit as seven segment display.

DisplaySevenSegment.java

Output 1:

Seven Segment Display Problem in Java

Output 1:

Seven Segment Display Problem in Java

Let's see another way to print digits in seven segment display.

Using Hexadecimal Encoding

In the following Java program, we have used hexadecimal encoding to represent a digit in seven segment form. The following table depicts the same. In the table on represent, 1 and off represents 0.

Seven Segment Display Problem in Java

Let's implement the above logic in a Java program.

DisplayDigitSevenSegment.java

Output:

Seven Segment Display Problem in Java

Problem Statement

The problem is divided into the following two subproblems.

  • In the first subproblem, we will compute the minimum number of segments required to display a number.
  • In the second subproblem, we will find the element that uses the minimum number of segments to display a number.

Let's discuss the subproblems one by one.

Subproblem 1

We have given an N-digit number and we have to find the minimum number of segments required to display each digit. Sum up all the segments and return the total number of segments to display the N-digit number.

Example:

Suppose, N=6 (number of digits) and s="234567".

The minimum number of segments requires to represent digit 2 is 5.

The minimum number of segments requires to represent digit 3 is 5.

The minimum number of segments requires to represent digit 4 is 4.

The minimum number of segments requires to represent digit 5 is 5.

The minimum number of segments requires to represent digit 6 is 6.

The minimum number of segments requires to represent digit 7 is 3.

So, the total number of segments that we require is 28 (5 + 5 + 4 + 5 + 6 + 3).

Let's implement the above approach in a Java program.

SegmentSum.java

Output:

Seven Segment Display Problem in Java

Subproblem 2

In this problem, we have given an array of natural numbers. The task is to find which natural number uses the minimum number of segments to display a number. There might be a condition in which multiple natural numbers have the minimum number of segments, in such a case, the output will be the number that has the smallest index.

For example, consider the following array.

arr[] = {22, 46, 88, 12, 67, 90, 23, 55, 61}

22 -> 5+5 = 10

46 -> 4+6 = 10

88 -> 7+7 = 14

12 -> 2+5 = 7

67 -> 6+3 = 9

90 -> 6+6 = 12

23 -> 5+5 = 10

55 -> 5+5 = 10

61 -> 5+5 = 8

We observe that number 12 uses the minimum number of segments to display the number. So, the output will be 12.

Java Program to Find Element Using Minimum Segments in Seven Segments Display

In the following Java program, we have stored the number of segments of each digit. After that, we have defined the two functions one Computes segments for the digits and the second function computes the minimum segment for the entire number. After that, we have compared each digit's segments to each other and return the number that uses the minimum number of segments.

Let's see the Java program for the same.

SevenSegment.java

Output:

12

Complexity

The time complexity for the above approach is O(n * log10n) and the space complexity is O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA