`{"id":"da228161-1b5d-41f7-8d35-4fab8a575530","name":"Count Set Bits In First N Natural Numbers","description":"1. You are given a number n.\r\n2. You have to print the count of set bits of first n natural numbers.","inputFormat":"A number n","outputFormat":"A number","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint solution(int n){\n //write your code here\n}\nint main(){\n int n;\n cin>>n;\n cout<<solution(n);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n System.out.println(solution(n));\r\n }\r\n\r\n public static int solution(int n){\r\n //write your code here\r\n \r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"17","sampleOutput":"35\r\n","questionVideo":"https://www.youtube.com/embed/g6OxU-hRGtY?end=408","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"b8f92e85-71ee-46bb-8865-3643f64f4ef2","name":"Count Set Bits In First N Natural Numbers","slug":"count-set-bits-in-first-n-natural-numbers","type":1}],"next":{"id":"4cf8713b-dc3c-4e62-821c-8f8f09d99624","name":"Count Set Bits in First N Natural Numbers MCQ","type":0,"slug":"count-set-bits-in-first-n-natural-numbers-mcq"},"prev":{"id":"23abb545-68ff-4fb0-8bae-37b7b7a5af09","name":"CHECK DIVISIBILITY BY 3","type":3,"slug":"check-divisibility-by-3"}}}`

# Count Set Bits In First N Natural Numbers

1. You are given a number n. 2. You have to print the count of set bits of first n natural numbers.

`{"id":"da228161-1b5d-41f7-8d35-4fab8a575530","name":"Count Set Bits In First N Natural Numbers","description":"1. You are given a number n.\r\n2. You have to print the count of set bits of first n natural numbers.","inputFormat":"A number n","outputFormat":"A number","constraints":"1 &lt;= n &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nint solution(int n){\n //write your code here\n}\nint main(){\n int n;\n cin>>n;\n cout<<solution(n);\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n System.out.println(solution(n));\r\n }\r\n\r\n public static int solution(int n){\r\n //write your code here\r\n \r\n return 0;\r\n }\r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"17","sampleOutput":"35\r\n","questionVideo":"https://www.youtube.com/embed/g6OxU-hRGtY?end=408","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"b8f92e85-71ee-46bb-8865-3643f64f4ef2","name":"Count Set Bits In First N Natural Numbers","slug":"count-set-bits-in-first-n-natural-numbers","type":1}],"next":{"id":"4cf8713b-dc3c-4e62-821c-8f8f09d99624","name":"Count Set Bits in First N Natural Numbers MCQ","type":0,"slug":"count-set-bits-in-first-n-natural-numbers-mcq"},"prev":{"id":"23abb545-68ff-4fb0-8bae-37b7b7a5af09","name":"CHECK DIVISIBILITY BY 3","type":3,"slug":"check-divisibility-by-3"}}}`

Editor

# Count Set Bits In First N Natural Numbers

easy

1. You are given a number n. 2. You have to print the count of set bits of first n natural numbers.

1 <= n <= 10^9

A number n

A number

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}17`

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}35 ```

Question Video

Discussions

Show Discussion

Related Resources