# Thread: "is a number binary" program that uses 2 functions

1. ## "is a number binary" program that uses 2 functions

pretty new to coding and have an assignment that i need guiding with.
Here's the prompt.

I can get code that works but is not using functions and I'm struggling with how to integrate them into this. please help

Overview: 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 integer 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
• Use at least two functions to solve the problem.

Here's the code I have that is not using functions

project 2

NEEDS TO USE 2 FUNCTIONS, CURRENTLY DOES NOT

Code:
```
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main() {

printf("Hello, this program determines whether your input is or is not binary")

int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n < 0) {

printf("%d: not binary\n", n);

}
else {

int isBinary = 1;

int mod, t = n;

while (t > 0) {

mod = t % 10;

if (mod != 0 && mod != 1) {

isBinary = 0;

break;

}

t = t / 10;

}

if (isBinary) {

printf("%d: binary\n", n);

}
else {

printf("%d: not binary\n", n);

}

}

return 0;

}``` 2. I'd use a function to find whether the number is binary or not. I can't think of any serious use a second function would have in this simple situation. Take input from the user and check for errors maybe? 3. I agree with GReaper on the two functions. You can remove the t = n statement if you use t as the formal parameter and n as the actual argument to a discriminateBinary function that includes your while statement. If you have that function return isBinary, then you can put a call to that function in place of testing the isBinary variable in your if (isBinary) statement. What is at the top of your main up to the scanf could be put into a getInput function that returns n. 4. I'm struggling with trying to do so :/ 5. Post your work! Post your attempt at creating an function that return a bool and takes an int as an input parameter.

isBinary would be a good function name.

Tim S. 6. still having no luck. could someone show me this? the original code runs but now i need to integrate two functions and I'm lost 7. If you don't know what functions are, start there. We gave you ideas on what the function may do, the implementation is up to you. 8. Essentially the same code you have above, but wrapped in a function:
Code:
```#include <stdbool.h>
static bool isBinary(int);
int main(void) { ... }

bool isBinary(int t) { // what does t stand for?
bool valid = true; // until proven otherwise
while (t > 0) {
int mod = t % 10;
if (mod != 0 && mod != 1) {
valid = false;
break; }
t = t / 10; }
return valid; }``` Popular pages Recent additions binary, input, number, program, user 