Program to convert a decimal number to Binary

Problem statement:- Program to convert a decimal number to Binary.

Data requirement:-

   Input Data:- decimal

  Output Data:-
binary

Program in C

Here is the source code of the C program to Program to convert a decimal number to binary.

Code:

#include <stdio.h>
#include <math.h>
int
main ()
{
  int decimal;
  printf ("Enter a Decimal Number: ");
  scanf ("%d", &decimal);

  int binary = 0;
  int reminder, temp = 1;

  while (decimal != 0)
    {
      reminder = decimal % 2;
      decimal = decimal / 2;
      binary = binary + reminder * temp;
      temp = temp * 10;
    }

  printf ("The Binary Number is: %d", binary);
  return 0;
}

Input/Output:
Enter a Decimal Number:
67
The Binary Number is: 1000011

Program in C++

Here is the source code of the C++ program to Program to convert a decimal number to binary.

Code:

#include <iostream>
using namespace std;
int
main ()
{
  int decimal;
  cout<<"Enter a Decimal Number: ";
  cin>>decimal;

  int binary = 0;
  int reminder, temp = 1;

  while (decimal != 0)
    {
      reminder = decimal % 2;
      decimal = decimal / 2;
      binary = binary + reminder * temp;
      temp = temp * 10;
    }

  cout<<"The Binary Number is: "<<binary;
  return 0;
}

Input/Output:
Enter a Decimal Number: 205
The Binary Number is: 11001101

Program in Java

Here is the source code of the Java program to Program to convert a decimal number to binary.

Code:

import java.util.Scanner;
public class DecimalToBinary {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int decimal;
  System.out.println("Enter a Decimal Number: ");
decimal=cs.nextInt();

  int binary = 0;
  int reminder, temp = 1;

  while (decimal != 0)
    {
      reminder = decimal % 2;
      decimal = decimal / 2;
      binary = binary + reminder * temp;
      temp = temp * 10;
    }

  System.out.println("The Binary Number is: "+ binary);
cs.close();
}
}

Input/Output:
Enter a Decimal Number: 
78
The Binary Number is: 1001110


Program in Python

Here is the source code of the Python program to Program to convert a decimal number to binary.

Code:

print("Enter a Decimal Number: ")
decimal=int(input())
binary = 0
temp = 1
while (decimal != 0):
    reminder = decimal % 2
    decimal = decimal // 2
    binary =int (binary + (reminder * temp))
    temp =int( temp * 10)

print("The Binary Number is: ",binary)

Input/Output:
Enter a Decimal Number: 
5
The Binary Number is:  101








Post a Comment

0 Comments