Decimal to binary program

decimal to binary in c

#include<stdio.h>
#include<conio.h>
void main()
{
	int binarynumber[10],decimalnumber,count=0;
	printf("Enter a decimal integer \n");
	scanf("%d",&decimalnumber);
	for(int i=0;decimalnumber>0;i++)
	{
		binarynumber[i]=decimalnumber%2;
		decimalnumber=decimalnumber/2;
		count++;
	}
	printf("Its binary equivalent is = ");
	for(int j=count-1;j>=0;j--)
	{
		printf("%d",binarynumber[j]);
	}
	getch();
}

Output

Enter a decimal integer
5
Its binary equivalent is = 101

decimal to binary in c++

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int binarynumber[10],decimalnumber,count=0;
	cout<<"Enter a decimal integer \n";
	cin>>decimalnumber;
	for(int i=0;decimalnumber>0;i++)
	{
		binarynumber[i]=decimalnumber%2;
		decimalnumber=decimalnumber/2;
		count++;
	}
	cout<<"Its binary equivalent is = ";
	for(int j=count-1;j>=0;j--)
	{
		cout<<binarynumber[j];
	}
	getch();
}

Output

Enter a decimal integer
7
Its binary equivalent is = 111

decimal to binary in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConvertDecimalToBinary
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] binarynumber = new int[10];
            int decimalnumber, count = 0;
            Console.WriteLine("Enter a decimal integer");
            decimalnumber=Convert.ToInt32(Console.ReadLine());
            for (int i = 0; decimalnumber > 0; i++)
            {
                binarynumber[i] = decimalnumber % 2;
                decimalnumber /= 2;
                count++;
            }
            Console.Write("Its binary equivalent is = ");
            for (int j = count - 1; j >= 0; j--)
            {
                Console.Write(binarynumber[j]);
            }
            Console.ReadLine();
        }
    }
}

Output

Enter a decimal integer
9
Its binary equivalent is = 1001

decimal to binary in java

import java.util.*;
class examples
{
public static void main(String[] args)
	{
	Scanner sc=new Scanner(System.in);
	int[] binarynumber = new int[10];
    int decimalnumber, count = 0;
    System.out.println("Enter a decimal integer");
    decimalnumber=sc.nextInt();
    for (int i = 0; decimalnumber > 0; i++)
    {
        binarynumber[i] = decimalnumber % 2;
        decimalnumber /= 2;
        count++;
    }
    System.out.print("Its binary equivalent is = ");
    for (int j = count - 1; j >= 0; j--)
    {
    	System.out.print(binarynumber[j]);
    }
	}
}

Output

Enter a decimal integer
11
Its binary equivalent is = 1011

Write a comment


Name Email Message