-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenerate Binary Numbers.cpp
More file actions
51 lines (51 loc) · 968 Bytes
/
Generate Binary Numbers.cpp
File metadata and controls
51 lines (51 loc) · 968 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Given a number n, Write a program that generates and prints all binary numbers with decimal values from 1 to n.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N.
Output:
Print all binary numbers with decimal values from 1 to n in a single line.
Constraints:
1 = T = 100
1 = N = 500
Example:
Input
2
2
5
Output
1 10
1 10 11 100 101
*/
#include<iostream>
using namespace std;
void printbinary(long long int n)
{
long long int a=1,binary=0;
long long int rem;
while(n>0)
{
rem=n%2;
binary=binary+rem*a;
a=a*10;
n=n/2;
}
cout<<binary;
}
int main()
{
int T,i;
cin>>T;
while(T--)
{
long long int i,num;
cin>>num;
for(i=1;i<=num;i++)
{
printbinary(i);
cout<<" ";
}
cout<<endl;
}
return 0;
}