Given a number N. Print the result of doing the following operation on N: • Convert N to its binary representation. • Count number of ones in the above binary representation. • Print the equivalent decimal number that its binary representation has only the number of ones that were counted above. For example: (10ldecimal" (1010)inary has 2 ones "11", after converting "11" to decimal number it will become 3

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a program in C++

Given a number N. Print the result of doing the following operation on N:
• Convert N to its binary representation.
• Count number of ones in the above binary representation.
• Print the equivalent decimal number that its binary representation has only the number of ones
that were counted above.
For example: (10)decimal = (1010)sinary has 2 ones "11", after converting "1" to decimal number it will
become 3.
Input
First line contains a number T (15T:10) number of test cases.
Next Tlines will contain a number N (15NS21-1).
Ouput
For each test case print a single line contains the answer according to the required above.
Example
Input
3
18
Output
1
Note
To convert decimal number to binary :
A decimal integer can be converted to binary by dividing it by 2.
Take the quotient, and keep dividing it by 2, until you reach zero.
Each time you perform this division, take note of the remainder. Now reverse the remainders list, and
you get the number in binary form
Example to convert 29 to binary
Step
Operation
Result
Remainder
Slep 1
29/2
14
Step 2
14/2
Slep 3
712
3
Slep 4
312
1
Step 5
1/2
for more details visit this https://flaviocapes.com/converting-decimal-to-binary/
To convert from binary to Decimal :
Binary Number - 11101,
Calculating Decimal Equivalent-
Step
Binary Number
Dacimal Number
Siep 1
11101,
((1 x 2) - (1 - 2) + (1 x 2) - 0 x 2") - (1 - 2i»a
Slep 2
11101-
(16 -8-4+0+ 1h
Slep 3
11101-
290
Second Test Case:
(7)decimal = (11)pinary has 3 ones "1", after converting "11" to decimal number it will become 7.
Third Test Case:
(8)decimal = (1000 inary has 1 one "1", after converting "1" to decimal number it will become 1.
Transcribed Image Text:Given a number N. Print the result of doing the following operation on N: • Convert N to its binary representation. • Count number of ones in the above binary representation. • Print the equivalent decimal number that its binary representation has only the number of ones that were counted above. For example: (10)decimal = (1010)sinary has 2 ones "11", after converting "1" to decimal number it will become 3. Input First line contains a number T (15T:10) number of test cases. Next Tlines will contain a number N (15NS21-1). Ouput For each test case print a single line contains the answer according to the required above. Example Input 3 18 Output 1 Note To convert decimal number to binary : A decimal integer can be converted to binary by dividing it by 2. Take the quotient, and keep dividing it by 2, until you reach zero. Each time you perform this division, take note of the remainder. Now reverse the remainders list, and you get the number in binary form Example to convert 29 to binary Step Operation Result Remainder Slep 1 29/2 14 Step 2 14/2 Slep 3 712 3 Slep 4 312 1 Step 5 1/2 for more details visit this https://flaviocapes.com/converting-decimal-to-binary/ To convert from binary to Decimal : Binary Number - 11101, Calculating Decimal Equivalent- Step Binary Number Dacimal Number Siep 1 11101, ((1 x 2) - (1 - 2) + (1 x 2) - 0 x 2") - (1 - 2i»a Slep 2 11101- (16 -8-4+0+ 1h Slep 3 11101- 290 Second Test Case: (7)decimal = (11)pinary has 3 ones "1", after converting "11" to decimal number it will become 7. Third Test Case: (8)decimal = (1000 inary has 1 one "1", after converting "1" to decimal number it will become 1.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Top down approach design
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education