Computer Science, asked by hamanidb159, 11 months ago

Write a Java program to input a no. And display it's result in its binary equivalent

Answers

Answered by jellyyoutube
7

Answer:

// Java program to convert a decimal  

// number to binary number  

import java.io.*;  

 

class GFG  

{  

   // function to convert decimal to binary  

   static void decToBinary(int n)  

   {  

       // array to store binary number  

       int[] binaryNum = new int[1000];  

   

       // counter for binary array  

       int i = 0;  

       while (n > 0)  

       {  

           // storing remainder in binary array  

           binaryNum[i] = n % 2;  

           n = n / 2;  

           i++;  

       }  

   

       // printing binary array in reverse order  

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

           System.out.print(binaryNum[j]);  

   }  

     

   // driver program  

   public static void main (String[] args)  

   {  

       int n = 17;  

       decToBinary(n);  

   }  

}  

 

Explanation:

please add this answer brianillist!

Answered by rakeshchennupati143
14

Program:

import java.util.*;

public class Main{

     public static void main(String[] args) {

           Scanner sc = new Scanner(System.in);

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

           int num = sc.nextInt();

           int temp = num;

           String binary="";

           while(num>0){

                 int rem = num % 2;

                 binary = rem + binary;

                 num = num / 2;

           }

           System.out.println("The binary of "+temp+" is "+binary);

     }

}

Output:

Enter a number : 10

The binary of 10 is 1010

Explanation:

  • first i took binary number into a num variable of in type
  • then i started loop until the given number becomes 0
  • in loop i took reminder of num % 2 which will be either 0 or 1
  • then i store it in a string variable (using arrays takes more memory so simply i used string to store)
  • then i divided num / 2 and store it in num
  • so the loop iterates until the number becomes 0
  • at last the binary will be stored in a string variable and printed out

----Hope you liked my program mark as brainliest if you liked it, it would really help me.    :)

Similar questions