In: Computer Science
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number.
For the program, you can assume that the input number is a number between 0 and 100.
Sample Run 1: Assume that the user typed the following number.
5
This is the correct output of your program.
000
001
010
011
100
101
Sample Run 2: Assume that the user typed the following number.
0
This is the correct output of your program.
0
Sample Run 3: Assume that the user typed the following number.
3
This is the correct output of your program.
00
01
10
11
Code:
#include<iostream>
using namespace std;
int main(){
int n,s=2,c=1,x[8],j,i,h,w;
cout<<"enter a
number:";
//taking input from user.
cin>>n;
while(s<=n){
s=s*2;
c=c+1;
}
w=c;
for(i=0;i<=n;i++){
h=i;
for(j=0; h>0;
j++){
x[j]=h%2;
h=
h/2;
//finding binary numbers.
}
for(w;w>j;w--)
cout<<0;
w=c;
for(j=j-1 ;j>=0
;j--){
cout<<x[j];
//printing binary numbers.
}
cout<<endl;
}
}
Output: