- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given a positive number let’s say num and the task is to calculate the count of changes in the Led lights displayed one by one. We will assume that initially all LEDs are off and they will be started based upon the string value.

For solving this question we need to understand what a seven segment display is and its working.

Seven segment displays are the output display device that provide a way to display information in the form of image or text or decimal numbers which is an alternative to the more complex dot matrix displays. It is widely used in digital clocks, basic calculators, electronic meters, and other electronic devices that display numerical information. It consists of seven segments of light emitting diodes (LEDs) which are assembled like numerical 8.

The number 8 is displayed when the power is given to all the segments and if you disconnect the power for ‘g’, then it displays number 0. In a seven segment display, power (or voltage) at different pins can be applied at the same time, so we can form combinations of display numerical from 0 to 9. Since seven segment displays can not form alphabet like X and Z, so it can not be used for alphabet and it can be used only for displaying decimal numerical magnitudes. However, seven segment displays can form alphabets A, B, C, D, E, and F, so it can also be used for representing hexadecimal digits.

Input− num = “123 ”Output− count is 5

**Explanation** − total LED required to light up 1 is 2, total LED required to light up 2 is 5 and total LED required to light up 3 is 5. So total numbers of changes required in displaying are − 5

Input− num = “576 ”Output− count is 10

**Explanation** − total LED required to light up 5 is 5, total LED required to light up 7 is 3 and total LED required to light up 6 is 6. So total numbers of changes required in displaying are : 10

Input the string of integer values defining the numbers to be light up

Calculate the length of the given string using the length() function that will return an integer value as per the digits in a string.

Create an array that will store all the LED values.

Take a temporary variable let’s say temp that will store the value.

Start loop for i to 1 and i less than the length

Inside the loop, set temp with temp + abs([LED[str[i] - ‘0’] - LED[n[i-1] - ‘0’]);

Return the count

Print the result.

#include<iostream> using namespace std; int countled(string str){ // number of LED required to display a digit int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 }; int len = str.length(); // calculating the change int res = Led[str[0] - '0']; for (int i = 1; i < len; i++){ res = res + abs(Led[str[i] - '0'] - Led[str[i - 1] - '0']); } return res; } int main(){ string str = "123"; cout <<"count is "<<countled(str); return 0; }

If we run the above code we will get the following output −

count is 5

- Related Questions & Answers
- Program to find island count by adding blocks into grid one by one in Python
- C# Program to read all the lines one by one in a file
- Program to count substrings that differ by one character in Python
- Adding one to number represented as array of digits in C++?
- One-to-One Relationship in DBMS
- Adding one to number represented as array of digits in C Program?
- Display all records except one in MySQL
- Count elements that are divisible by at-least one element in another array in C++
- Count of strings that become equal to one of the two strings after one removal in C++
- One-to-One Unary Relationship in DBMS
- One-to-One Relationship Model
- Count only null values in two different columns and display in one MySQL select statement?
- How to programmatically add buttons into a layout one by one in several lines in Android
- Program to check one string can be converted to another by removing one element in Python
- Count the total number of squares that can be visited by Bishop in one move in C++

Advertisements