languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

commit c79e86f984cc2437a4125ac086ea9a149877ac62
parent bd329151ff09ea9a14a7d2eec3b266ee1e03cbee
Author: Evan Alba <evanalba@protonmail.com>
Date:   Sun, 25 Jun 2023 18:59:02 -0700

feat: Added C directory.

Diffstat:
Ac/README.md | 4++++
Ac/binary.c | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/c/README.md b/c/README.md @@ -0,0 +1,4 @@ +## Installation +1. Compile the `binary.c` file into an executable file. +2. Set the PATH variable in your operating system for where your `binary.c` executable file is located. +3. Execute the executable command in your command-line interface to run the binary command. diff --git a/c/binary.c b/c/binary.c @@ -0,0 +1,39 @@ +#include <math.h> +#include <stdio.h> + +// binary: Test the user on the binary powers of 2. +void binary() { + int power = 0; + int max_power = 64; + while (power <= max_power) { + printf("What is 2 to the power of %u?\n", power); + + // Make sure there is no input buffer overflow. + char line[256]; + if (fgets(line, sizeof(line), stdin)) { + + unsigned long long answer; + if (sscanf(line, "%llu", &answer) == 1) { + printf("You have entered: %llu\n", answer); + if (answer == pow(2, power)) { + printf("You are correct!\n\n"); + power++; + } else { + printf("Incorrect. The answer is %.0f." + "\n", pow(2, power)); + return; + } + } else { + printf("\nYou have entered a non-integer " + "answer. Please try again.\n"); + } + } + } + printf("\n\nCongrats! You are highly skilled in the powers of 2.\n\n"); +} + +int main(int argc, char* argv[]) { + binary(); + return 0; +} +