Write a program for Decimal to binary conversion

16 May 2022

Write a program for Decimal to binary conversion


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a decimal number as input from the user and print the corresponding binary value as output.

Input

8

Output

1000

C Program

 

#include<stdio.h>    

int main()

{  

int a[10],n,i;

printf("Enter a decimal number: ");

scanf("%d",&n);    

for(i=0;n>0;i++)    

 {    

    a[i]=n%2;    

    n=n/2;    

 }    

for(i=i-1;i>=0;i--)    

    printf("%d",a[i]);

return 0;  

}  

C++ Program

 

#include <iostream>

using namespace std;

int main()

{  

int a[10],n,i;

cout<<"Enter a decimal number: ";

cin>>n;    

for(i=0;n>0;i++)    

 {    

    a[i]=n%2;    

    n=n/2;    

 }    

for(i=i-1;i>=0;i--)    

    cout<<a[i];

return 0;  

}  

Java

 

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);    

System.out.print("Enter a decimal number : ");

int dec = sc.nextInt();

    System.out.println(Integer.toBinaryString(dec));

}

}

Python

dec = int(input("Enter a decimal number: "))

binary = bin(dec)

print('Number after conversion is :' + str(binary))


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !