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.
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.
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:
Output 1:
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.
Let’s implement the above logic in a Java program.
DisplayDigitSevenSegment.java
Output:
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:
12
Complexity
The time complexity for the above approach is O(n * log10n) and the space complexity is O(1).