adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C & C++ to print the binary equivalent 1 to nth number.

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

  1.  Program in C++ to Convert Binary to Decimal number Using Function
  2.  Write a program in C to Convert Decimal to Binary number.
  3.  Write a program in C to convert decimal into octal number.
  4.  Program in C++ to Convert Decimal to Hexadecimal number Using Function
  5.  Program in C++ to Convert Hexadecimal to Decimal number Using Function

Post a Comment

0 Comments