Please excuse - and do not hesitate to point out - any violation against etiquette that I might be committing here… I am new here.

I started to learn C a few months ago as a hobby as part of a bigger project, namely to learn about computers in general. I have had so much fun reading Code - The Hidden Language of Computer Hardware and Software by Charles Petzold. But that’s another story…

I was about to buy a few new SSDs and needed to do some budgeting. Instead of using my phone’s calculator, I decided to try to write a calculating program in C, because I hadn’t touched programming for some weeks or months because life and I wanted to see if my knowledge had matured some.

The goal was to have it do the four standard arithmetics and also save the last result in a variable, which I just called “memory” for lack of bette phrasing on my part. Maybe next week I’ll figure out how to make it possible to use the value saved in memory instead of having to type a number.

I welcome any constructive criticism on how and why this code is up to code or not(sorry…), if it can be improved and how or even if it’s just garbage and why that is. I am just proud that it worked without gcc throwing any errors.

#include <stdio.h>

int main(void) {

        int num1 = 0;
        int num2 = 0;
        int choice = 0;
        int memory = 0;

        printf("Welcome to the Calculator of the century!\n\n");

        while (1) {
                printf("What would you like to do?\n\n");
                printf("(1) Add two numbers\n(2) Subtract two numbers\n(3) Multiply two numbers\n(4) Divide two numbers\n(5) Show memory\n(6) Exit\n\n");
                printf("Enter 1, 2, 3, 4, 5 or 6: ");
                scanf("%d", &choice);

                if (choice >= 6 || choice < 1) break;

                if (choice == 5) {
                        printf("\n%d in memory.\n\n", memory);
                } else if (choice < 5 || choice > 0) {
                        printf("\nEnter the first number: ");
                        scanf("%d", &num1);
                        printf("Enter the second number: ");
                        scanf("%d", &num2);
                }

                if (choice == 1) {
                        printf("\nThe sum of %d and %d is %d\n\n", num1, num2, num1 + num2);
                        memory = num1 + num2;
                } else if (choice == 2) {
                        printf("\nThe difference of %d and %d is %d\n\n", num1, num2, num1 - num2);
                        memory = num1 - num2;
                } else if (choice == 3) {
                        printf("\nThe product of %d and %d is %d\n\n", num1, num2, num1 * num2);
                        memory = num1 * num2;
                } else if (choice == 4) {
                        printf("\nThe quotient of %d and %d is %d\n\n", num1, num2, num1 / num2);
                        memory = num1 / num2;
                }
        }

        printf("\nWe hope to see you soon again!\n");
        return 0;
}
  • vrek@programming.dev
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 days ago

    Also look up the modulo operator. It’s similar to integer division but ONLY returns the remainder. It’s useful if you want to have something with multiple division steps for example.

    38 hours is how many days? 38 / 24 = 1 with your current setup

    38 / 24 = 1.583333333 if you use floats

    38 % 24 = 14… % is the modulo operator

    Combine your setup and the modulo you could output something like “there is 1 day and 14 hours over course of 38 hours” which is much more human usable since most people don’t know how long 0.583333333 is but know 14 hours.

    Or doing money… I’ll leave this up to you as a mental exercise for you but say you want to give someone $46.28 in cash. How many and what type of bills and coins would you give? Hint while 4628 pennies would be true it’s not the answer you want.