Program in C & C++ to print the binary equivalent 1 to nth number.
C PROGRAM
#include<stdio.h> #include<conio.h> int Bin(int dec) { long int bin,base; for(bin=0,base=1;dec!=0;) { bin=dec%2*base+bin; base=base*10; dec=dec/2; } return bin; } main() { int i,n,bin; printf("Enter a number:-"); scanf("%d",&n); printf("Decimal no. Binary no.\n"); for(i=1;i<=n;i++) { bin=Bin(i); printf(" %d\t\t %d\n",i,bin); } getch(); }
C++ PROGRAM
#include<iostream> #include<conio.h> using namespace std; int Bin(int dec) { long int bin,base; for(bin=0,base=1;dec!=0;) { bin=dec%2*base+bin; base=base*10; dec=dec/2; } return bin; } int main() { int i,n,bin; cout<<"Enter a number:-"; scanf("%d",&n); cout<<"Decimal no. Binary no.\n"; for(i=1;i<=n;i++) { bin=Bin(i); cout<<" "<<i<<"\t\t "<<bin<<endl; } return 0; }
Output :-
Related Programs
- Program in C++ to Convert Binary to Decimal number Using Function
- Write a program in C to Convert Decimal to Binary number.
- Write a program in C to convert decimal into octal number.
- Program in C++ to Convert Decimal to Hexadecimal number Using Function
- Program in C++ to Convert Hexadecimal to Decimal number Using Function
0 Comments