Question

In Java Write a program the takes in a number and converts it to binary. Use...

In Java Write a program the takes in a number and converts it to binary. Use an OOP class approach using class and objects

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.io.*;
import java.util.Scanner;
public class Test { // Here we are taking class name as Test
public static int DecimaltoBinary(int n) { // Definition of the method DecimaltoBinary
int[] Arr=new int[10]; // Here declaring the one dimensional array called Arr
int i; // Here declaring integer varaible called i
for(i=0; n>0; i++)// Loop Iteration
{
Arr[i]=n%2; // Here dividing n value with 2 and stored in Arr[]
n=n/2; // Here dividing n value with 2 and stored in n
}
for(i=i-1 ;i>=0 ;i--)// Loop Iteration
{
System.out.print(Arr[i]); // Here priting the array values
}
return Arr[i]; //Here Returning array values to the main()
} // End of method DecimaltoBinary
public static void main(String args[])throws IOException // Start of main()
{
Test ob = new Test(); // Here creating the object for class TestLeapYear
Scanner in = new Scanner(System.in);
System.out.println("Enter any Decimal Number: "); // Here taking Input from the User
int x = in.nextInt(); // Here scanning the input
System.out.print("The Binary value of "+x+ " is : ");
ob.DecimaltoBinary(x); // Here calling the DecimaltoBinary() method using object called ob
}// End of main()
} // End of class Test

Output:

Add a comment
Know the answer?
Add Answer to:
In Java Write a program the takes in a number and converts it to binary. Use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT