Question

In C:Write a program that can determine whether a user input is a binary number or...

In C:Write a program that can determine whether a user input is a binary number or not. (1, 2, 3, 8, 13) Write a program that accomplishes all of the following:

Greets the user and informs them that the program will determine whether an input from the user is a valid binary number or not (e.g., consisting of only 0’s and/or 1’s).

Accept an integer input from the user. Assume that the user provides a valid numeric input that will fit in the standard int type. However, check to make sure the input was a positive number - for our purposes, any negative integer input is not a binary number.

Use a loop to check all of the digits of the given input to determine that they are either 0 or 1 (Hint: You may want to use the modulus and division operators to process the digits one-by-one).

If all digits were either 0 or 1, print a message to the user stating that the input was a valid binary number. Otherwise, print a message stating that the input was not a valid binary number.

Test your program thoroughly. Make sure to at least the following inputs and verify the results:

0: binary

1: binary

01: binary

21001: not binary

-1011: not binary

123: not binary

10101: binary

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

In the following C program, we ask the user for an integer num, then check if its negative or not. If it is then we set a flag to 1. Then using a while loop that runs until the num is 0, inside the loop we retrieve the unit place digit of num using mod operation. If the digit is anything other than a 0 or a 1, it sets the flag as well. Then we divide the num by 10 for the next unit place digit.

Finally, we check if the flag is 1 or not, if it is then number is not binary otherwise it is.

CODE:

#include<stdio.h>

int main()
{
   int num;
   int flag=0;
   int rem;
   printf("***Check weather the input is binary or not***");
   printf("\nEnter the number: ");
   scanf("%d", &num);  
   if(num < 0 )   //if negative
       flag =1;   //set the flag
   while(num!=0)   //loop until num is 0
   {
       rem = num % 10;   //reteieve the unit place digit
       if(rem != 0 && rem != 1)   //check if not binary
           flag = 1;   //set the flag
       num = num/10;   //divide num by 10
   }
  
   if(flag == 1)   //if flag was set
       printf("Not binary");
   else   //otherwise
       printf("binary");
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In C:Write a program that can determine whether a user input is a binary number or...
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
  • Flowchart and pseudocode for a program that takes a user input consisting of an integer number...

    Flowchart and pseudocode for a program that takes a user input consisting of an integer number between 0 and 99 and outputs its conversion as binary. For example, if a user enters 25, the output should be 11001. The program should also validate the input and continuously ask for a new input if the number is not within the appropriate range of values. Additional instructions: check that the user enters a number between 0 and 99.

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display...

    Summary: Write a C program that prompts the user to enter 2 positive integer numbers, display the numbers in 3 formats. Then check, whether the larger of the 2 is evenly divisible by the smaller. Detail: Write a complete C program, more complex than the typical "hello world" program. Prompt the user to enter 2 integer numbers that are not negative. After either entry, display that number again. Print the smaller in hexadecimal, in decimal, and in octal format. Include...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • using assembly 8086, Convert a binary string to decimal. Accept buffered user input in the form...

    using assembly 8086, Convert a binary string to decimal. Accept buffered user input in the form of a 16-bit binary string. Convert to signed integer and print to the screen. If the user enters less than 16 bits, or input illegal character return an error message and prompt for input again. Sample Execution: Enter a 16-bit binary number: 100111100101110 The decimal signed integer equivalent is -24996 Enter a binary number: 11001011100 Error! Please enter exactly 16-bits: 100111100101110 Enter a 16-bit...

  • JAVA: Write a program that inputs a string that represents a binary number. The string can...

    JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

  • Write a program that (in the main program) prompts the user for the (integer) model number...

    Write a program that (in the main program) prompts the user for the (integer) model number of their car. The main function should then call the function check defective which should • take in the model number of the car • check if the model number is 119, 179, 221, 780, or anything between 189 and 195, inclusive, which indicates the car is defective • print a message indicating whether or not the car is defective Note: this is should...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • C++ programming write a program that asks a user to enter a random IP number (IP...

    C++ programming write a program that asks a user to enter a random IP number (IP number is nothing but 4 numbers with dots in between and each number is between 0 and 255) (example: 1.2.3.4 or 255.255.255.255) The program you will write (Write the program with string function) checks the entered number for 1) making sure each part is between 0 and 255 (if it's 256 or more, it's going to print a message saying one of the values...

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