C: Basic Arithmetic Operators.


 https://github.com/pereiradaniel/beginning_c/blob/master/ch2/arithm.c

// arithm.c
// Daniele Grech Pereira, 02 November 2022

// Use basic arithmetic operators.

#include <stdio.h>
int main (int argc, char* argv[])
{

    // Declare and initialize vars:
    int add[2] = {0}, mult[2] = {0}, div[2] = {0};
    double div2[2] = {0};

    printf("All vars have been declared and initialized:\n");
    printf("add1: %d, add2: %d, mult1: %d, mult2: %d, div1: %d, div2: %d, div3: %.2lf, div4: %.2lf.\n", add[0], add[1], mult[0], mult[1], div[0], div[1], div2[0], div2[1]);

    printf("Enter first whole number to add: \n");
    scanf(" %d", &add[0]);
    printf("Enter second whole number to add: \n");
    scanf(" %d", &add[1]);
    printf("add1 + add2 = ?\n%d + %d = %d\n", add[0], add[1], add[0]+add[1]);

    printf("Enter first whole number to multiply: \n");
    scanf(" %d", &mult[0]);
    printf("Enter second whole number to multiply: \n");
    scanf(" %d", &mult[1]);
    printf("mult1 * mult2 = ?\n%d * %d = %d\n", mult[0], mult[1], mult[0]*mult[1]);

    printf("Enter first whole number to divide: \n");
    scanf(" %d", &div[0]);
    printf("Enter second whole number to divide: \n");
    scanf(" %d", &div[1]);
    printf("div1 / div2 = ?\n%d / %d = %d\n", div[0], div[1], div[0]/div[1]);
    printf("div1 %% div2 = ?\n%d %% %d = %d\n", div[0], div[1], div[0]%div[1]);

    printf("Enter first decimal number to divide: \n");
    scanf(" %lf", &div2[0]);
    printf("Enter second decimal number to divide: \n");
    scanf(" %lf", &div2[1]);
    printf("div3 / div4 = ?\n%.2lf / %.2lf = %.2lf\n", div2[0], div2[0], div2[0]/div2[1]);

    return 0;
}

// Arithmetic Operators
// https://devdocs.io/c/language/operator_arithmetic
//
// Binary additive arithmetic operator:         lhs + rhs
// Binary multiplicative arithmetic operator:   lhs * rhs
// Binary division arithmetic operator:         lhs / rhs
// Binary remainder arithmetic operator:        lhs % rhs
https://github.com/pereiradaniel/beginning_c/blob/master/ch2/arithm.txt
==94== Memcheck, a memory error detector
==94== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.   
==94== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==94== Command: ./arithm
==94== 
All vars have been declared and initialized:
add1: 0, add2: 0, mult1: 0, mult2: 0, div1: 0, div2: 0, div3: 0.00, div4: 0.00.
Enter first whole number to add: 
1
Enter second whole number to add: 
2
add1 + add2 = ?
1 + 2 = 3
Enter first whole number to multiply:
5
Enter second whole number to multiply:
4
mult1 * mult2 = ?
5 * 4 = 20
Enter first whole number to divide:
55
Enter second whole number to divide:
5
div1 / div2 = ?
55 / 5 = 11
div1 % div2 = ?
55 % 5 = 0
Enter first decimal number to divide:
105.5
Enter second decimal number to divide:
23.1
div3 / div4 = ?
105.50 / 105.50 = 4.57
==94==
==94== HEAP SUMMARY:
==94==     in use at exit: 0 bytes in 0 blocks
==94==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==94==
==94== All heap blocks were freed -- no leaks are possible
==94==
==94== For lists of detected and suppressed errors, rerun with: -s
==94== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Popular posts from this blog

C programming and relevancy

Shakespeare AI: My lady is more beauteous than a rose.

C: Temperature Conversion With Main Repeat