decimal to binary

Java Program

java program decimal To Binary Conversion

class DecimalToBinary {

       public static void main(String[] args) {

              Scanner sc = new Scanner(System.in);

              System.out.println(“Eneter Value”);

              int n = sc.nextInt();

              String rem = “”;

              System.out.println(“Eneter Value is :” + n);

              while (n > 0) {

                     rem = n % 2 + rem;

                     n = n / 2;

              }

              System.out.println(“Binary value is ” + rem);

       }}

======================== java program decimal To Binary Conversion===============

class BinaryToDecimal {

       public static void main(String[] args) {

              Scanner sc = new Scanner(System.in);

              System.out.println(“Eneter Value”);

              int n = sc.nextInt();

              int decimal = 0;

              int cnt = 0;

              System.out.println(“Eneter Value is :” + n);

              while (true) {

                     if (n == 0) {

                           break;

                     } else {

                           int temp = n % 10;

                           decimal = (int) (decimal + temp * Math.pow(2, cnt));

                           n = n / 10;

                           cnt++;

                     }

              }

              System.out.println(“Decimal value is :” + decimal);

       }

}

java program decimal To Binary Conversion…

About the author

ganesh

View all posts
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments