Question

Question#03: Input an integer containing only Os and is (i.e., a binary integer) and print its decimal equivalent. (Hint: U

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

THE PROGRAM IS DONE WITH JAVA AND C++

PROGRAM IN JAVA

/* import scanner class from util package */
import java.util.Scanner;
import java.lang.Math;

class bin_decimal
{

int binary_decimal(int b)
{
   
   int i=0;
   int s=0,a;
   while(b!=0)
{
       a=b%10;
       s=s+(int)(Math.pow(2,i)*a);
       b=b/10;
       System.out.println(b);
       i++;
    }  
    return s;
}
  
}
public class convert
{
public static void main(String[] args)
{
   int bin,res;
   /* sc object of scanner class */
   Scanner sc=new Scanner(System.in);
   /* ob object of bin_decimal class */
bin_decimal ob=new bin_decimal();
   System.out.println();
   System.out.print("Enter a binary number :");
   bin=sc.nextInt();
   res=ob.binary_decimal(bin);
   System.out.println("\nDecimal equivalant of binary " + bin + " is : " + res);
}
}

SCREEN SHOT

/* import scanner class from util package */ import java.util.Scanner; import java.lang.Math; class bin_decimal int binary_de

System.out.println(); System.out.print (Enter a binary number :); bin=sc.nextInt (); res=ob.binary_decimal (bin); System.ou

OUTPUT

C:\Program Files\Java\jdk1.8.0_131\bin>javac convert.java C:\Program Files\Java\jdk1.8.0_131bin>java convert Enter a binary n

PROGRAM IN C++

#include<iostream>
#include<math.h>
using namespace std;

int binary_decimal(int b)
{
   
   int i=0,s=0,a;
   while(b!=0)
{
       a=b%10;
       s=s+(pow(2,i)*a);
       b=b/10;
       i++;
    }  
    return s;
}
main()
{
   int bin,dec,i=0,s=0,a,b,res;
   cout<<"Enter a binary number :";
   cin>>bin;
   res=binary_decimal(bin);
   cout<<"\nDecimal equivalant of binary "<<bin<<" is : "<<res;
}

SCREEN SHOT

#include<iostream> #include<math.h> using namespace std; int binary_decimal(int b) int i=0,s=@,a; while(b!=0) a=b%10; S=5+(po

OUTPUT

C:\Users\cse\Documents\bin_dec.exe Enter a binary number :1100 Decimal equivalant of binary 1100 is : 12 Process exited after

Add a comment
Know the answer?
Add Answer to:
Question#03: Input an integer containing only Os and is (i.e., a "binary" integer) and print its...
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
  • C++ Convert a Number from Binary to Decimal using a stack: The language of a computer...

    C++ Convert a Number from Binary to Decimal using a stack: The language of a computer is a sequence of Os and 1s. The numbering system we use is called the decimal system, or base 10 system. The numbering system that the computer uses is called the binary system, or base 2 system. The purpose of this exercise is to write a function to convert a string representing a binary number from base 2 to base 10. To convert a...

  • The place values for the eight binary digits used in IPv4 addressing are as follows: 128,...

    The place values for the eight binary digits used in IPv4 addressing are as follows: 128, 64, 32, 16, 8, 4, 2, 1. Expand this range to include an additional four bits. Do this by recording the place values for 211, 210, 29, and 28. 1111 Express the decimal value 2001 in binary by placing 1s in the binary positions requiring the addition of the corresponding place value. Place 0s in the binary positions where the corresponding place value should...

  • The digital hardness of an integer is measured by examining its representation in binary (base 2)....

    The digital hardness of an integer is measured by examining its representation in binary (base 2). Working from the ends of the representation inward, we remove/count corresponding pairs of leading and trailing 1s until we can no longer do so. The total number of 1s (bits) removed is the original number's digital hardness value. For example, suppose that we have an integer whose binary representation is 110101010101 (the binary representation always begins with a leading 1). We remove the leading...

  • Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number....

    Write a VBA code to convert an arbitrary binary integer number to its equivalent decimal number. Specific requirements: Use InputBox function to acquire the input for an arbitrary binary integer number; Convert the binary integer to its equivalent decimal number; Return the decimal number in a message box. Submit your (VBA code) Excel screen shoot. Below functions may be useful in your VBA code: Val( ): can convert a string-type number in ( ) to its numeric value; Len( ):...

  • (a) Write a truth table. The input is 4-bit binary ABCD, A is MSB, D is...

    (a) Write a truth table. The input is 4-bit binary ABCD, A is MSB, D is LSB. The output is also represented by x. (b) Obtain an output expression in the form of a SOP. (c) Use Boolean Algebra to design a circuit consisting of only four inverters, four 3-input and gate, and one 4-input OR gate using the simplified and simplified expression obtained in (b). 4-6. The Excess-3 coding system is a four-bit digital coding system for encoding all...

  • ord Paragrapth Styles 1 Perform the following conversions Convert 51 (decimal) to binary and to hex...

    ord Paragrapth Styles 1 Perform the following conversions Convert 51 (decimal) to binary and to hex a b. Convert 0xDI (hexadecimal) to binary and to decimal c. Convert Ob11001001 (binary) to hex and to decimal 2. Find the 2's complement of the following 4 bit numbers a 1101 b 0101 3. Perform the following 4 bit unsigned operations. For each, indicate the 4-bet result and the carry bit, and indicate if the answer is correct or not a. 5+8 b....

  • TRUE FALSE: Please indicate your answer with a Tor F. 1. The binary number system has...

    TRUE FALSE: Please indicate your answer with a Tor F. 1. The binary number system has a base of 8. ANS: F 2. The decimal number 7 would be written in binary as 1011. ANS: F 3. To express a number in decimal requires fewer digits than in the binary system. ANS: 4. Numbers 8 and 9 are not used in the octal number system. ANS: 5. For a base 2 number system, the weight value associated with the 3rd...

  • Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number...

    Problem 1: regarding Binary numbers, create an algorithm (flowchart ) that reads a 4-bit binary number from the keyboard as a string and then converts it into a decimal number. For example, if the input is 1100, the output should be 12. (Hint: Break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, and b3, the decimal equivalent is 8b0+ 4b1+ 2b2+ b3.) Problem 2: Suppose...

  • 1. Translate the decimal numbers below to binary numbers. The first two are already completed as...

    1. Translate the decimal numbers below to binary numbers. The first two are already completed as examples. Decimal number: 66 Place 128 64 32 16 8 4 2 1 Digits Show the calculation: Decimal number: 105 Place 128 64 32 16 8 4 2 1 Digits Show the calculation: Decimal number: 111 Place 128 64 32 16 8 4 2 1 Digits Show the calculation: Determine the binary code for the decimal integer numbers on your worksheet. Some help in...

  • What is the largest negative signed integer (furthest away from zero in the negative direction) that...

    What is the largest negative signed integer (furthest away from zero in the negative direction) that may be stored in 4 bits? a. -24 − 1 b. -24 c. -23 − 1 d. -23 What is the largest signed integer that may be stored in 16 bits? a. 216 − 1 b. 216 c. 215 − 1 d. 215 The 16-bit two’s complement representation of -33 decimal number is _____? a.        0000 0000 0010 0001 b.        0000 0000 1101 1111 c.        1111 1111...

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