Skip to content

← Back to Overview

Integer Arithmetic Operations

Concept Explanation

Integer arithmetic involves performing fundamental mathematical operations like addition, subtraction, multiplication, division, and finding the remainder (modulo) on whole numbers. Understanding these basic operations is crucial for any programming task, as they form the building blocks of more complex calculations. Special attention is often given to division, especially preventing division by zero errors.

Algorithm

  1. Start.
  2. Get two integer numbers from the user (e.g., num1, num2).
  3. Calculate the sum (sum = num1 + num2).
  4. Calculate the difference (difference = num1 - num2).
  5. Calculate the product (product = num1 * num2).
  6. Display the sum, difference, and product.
  7. Check if num2 is not zero: a. If true, calculate quotient (quotient = num1 / num2) and remainder (remainder = num1 % num2). b. Display the quotient and remainder. c. If false, display an error message for division by zero.
  8. End.

Implementations

import java.util.Scanner;

public class IntegerArithmetic {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("--- Integer Arithmetic Operations ---");

        System.out.print("Enter the first integer: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter the second integer: ");
        int num2 = scanner.nextInt();

        int sum = num1 + num2;
        int difference = num1 - num2;
        int product = num1 * num2;

        System.out.println("Results:");
        System.out.println("Sum:        " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product:    " + product);

        if (num2 != 0) {
            int quotient = num1 / num2;
            int remainder = num1 % num2;
            System.out.println("Division (Quotient): " + quotient);
            System.out.println("Remainder:           " + remainder);
        } else {
            System.out.println("Division and Remainder: Cannot divide by zero.");
        }

        scanner.close();
    }
}
print("--- Integer Arithmetic Operations ---")

num1_str = input("Enter the first integer: ")
num1 = int(num1_str)

num2_str = input("Enter the second integer: ")
num2 = int(num2_str)

sum_val = num1 + num2
difference = num1 - num2
product = num1 * num2

print("Results:")
print("Sum:       ", sum_val)
print("Difference:", difference)
print("Product:   ", product)

if num2 != 0:
    quotient = num1 // num2
    remainder = num1 % num2
    print("Division (Quotient):", quotient)
    print("Remainder:          ", remainder)
else:
    print("Division and Remainder: Cannot divide by zero.")
#include <stdio.h>

int main() {
    int num1, num2;
    int sum, difference, product, quotient, remainder;

    printf("--- Integer Arithmetic Operations ---");

    printf("Enter the first integer: ");
    scanf("%d", &num1);

    printf("Enter the second integer: ");
    scanf("%d", &num2);

    sum = num1 + num2;
    difference = num1 - num2;
    product = num1 * num2;

    printf("Results:\n");
    printf("Sum:        %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product:    %d\n", product);

    if (num2 != 0) {
        quotient = num1 / num2;
        remainder = num1 % num2;
        printf("Division (Quotient): %d\n", quotient);
        printf("Remainder:           %d\n", remainder);
    } else {
        printf("Division and Remainder: Cannot divide by zero.\n");
    }

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  num1        NUMBER := &Enter_First_Integer;
  num2        NUMBER := &Enter_Second_Integer;
  sum_val     NUMBER;
  diff_val    NUMBER;
  prod_val    NUMBER;
  ratio_val   NUMBER; -- Renamed to avoid confusion with integer division
  rem_val     NUMBER;
BEGIN
  sum_val := num1 + num2;
  diff_val := num1 - num2;
  prod_val := num1 * num2;

  DBMS_OUTPUT.PUT_LINE('Number 1: ' || num1);
  DBMS_OUTPUT.PUT_LINE('Number 2: ' || num2);
  DBMS_OUTPUT.PUT_LINE('Sum: ' || sum_val);
  DBMS_OUTPUT.PUT_LINE('Difference: ' || diff_val);
  DBMS_OUTPUT.PUT_LINE('Product: ' || prod_val);

  IF num2 != 0 THEN
    ratio_val := TRUNC(num1 / num2); -- For integer quotient
    rem_val := MOD(num1, num2);
    DBMS_OUTPUT.PUT_LINE('Division (Quotient): ' || ratio_val);
    DBMS_OUTPUT.PUT_LINE('Remainder: ' || rem_val);
  ELSE
    DBMS_OUTPUT.PUT_LINE('Division and Remainder: Cannot divide by zero.');
  END IF;
END;
/

Explanation

  • Java: Uses the Scanner class for input. Integer division (/) truncates decimals. The modulo operator (%) gives the remainder. Includes if condition to prevent division by zero.
  • Python: Uses input() for string input, int() for conversion. Supports standard +, -, * operators. // is for integer division (floor division), and % is for modulo. Includes if condition to prevent ZeroDivisionError.
  • C: Employs scanf() to read integers and printf() for output. Uses +, -, *, / (integer division), and % for modulo. It's crucial to check for division by zero to prevent runtime errors.
  • Oracle: Implemented in PL/SQL. Uses substitution variables (&Enter_First_Integer) for user input. TRUNC(num1 / num2) is used to get the integer quotient, and MOD(num1, num2) for the remainder. DBMS_OUTPUT.PUT_LINE is used for displaying results. Division by zero is handled with an IF statement.

Flowchart

graph TD
    A[Start] --> B[Get num1 int]
    B --> C[Get num2 int]
    C --> D[Calculate operations]
    D --> E[Display results]
    E --> F{num2 != 0?}
    F -- Yes --> G[Calculate division]
    G --> H[Display division]
    H --> I[End]
    F -- No --> J[Display error]
    J --> I

Practice Problems

  • Modify the program to perform arithmetic operations on three numbers.
  • Implement similar operations for decimal numbers.
  • Take two numbers and find their average.

"Pure mathematics is, in its way, the poetry of logical ideas." - Albert Einstein

Decimal Arithmetic Operations

Concept Explanation

Decimal (floating-point) arithmetic extends basic mathematical operations to numbers with fractional parts. This is essential for calculations requiring precision beyond whole numbers, commonly found in scientific, financial, or engineering applications. Like integer arithmetic, it involves addition, subtraction, multiplication, division, and remainder, but with considerations for floating-point precision and handling.

Algorithm

  1. Start.
  2. Get two decimal numbers from the user (e.g., num1, num2).
  3. Calculate the sum (sum = num1 + num2).
  4. Calculate the difference (difference = num1 - num2).
  5. Calculate the product (product = num1 * num2).
  6. Display the sum, difference, and product.
  7. Check if num2 is not zero: a. If true, calculate quotient (quotient = num1 / num2) and remainder (remainder = num1 % num2 or equivalent). b. Display the quotient and remainder. c. If false, display an error message for division by zero.
  8. End.

Implementations

import java.util.Scanner;

public class DecimalArithmetic {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("--- Decimal Arithmetic Operations ---");

        System.out.print("Enter the first decimal number: ");
        double num1 = scanner.nextDouble();

        System.out.print("Enter the second decimal number: ");
        double num2 = scanner.nextDouble();

        double sum = num1 + num2;
        double difference = num1 - num2;
        double product = num1 * num2;

        System.out.println("\nResults:");
        System.out.println("Sum:        " + sum);
        System.out.println("Difference: " + difference);
        System.out.println("Product:    " + product);

        if (num2 != 0.0) {
            double quotient = num1 / num2;
            double remainder = num1 % num2;
            System.out.println("Division (Quotient): " + quotient);
            System.out.println("Remainder:           " + remainder);
        } else {
            System.out.println("Division and Remainder: Cannot divide by zero.");
        }

        scanner.close();
    }
}
print("--- Decimal Arithmetic Operations ---")

num1_str = input("Enter the first decimal number: ")
num1 = float(num1_str)

num2_str = input("Enter the second decimal number: ")
num2 = float(num2_str)

sum_val = num1 + num2
difference = num1 - num2
product = num1 * num2

print("\nResults:")
print("Sum:       ", sum_val)
print("Difference:", difference)
print("Product:   ", product)

if num2 != 0.0:
    quotient = num1 / num2
    remainder = num1 % num2
    print("Division (Quotient):", quotient)
    print("Remainder:          ", remainder)
else:
    print("Division and Remainder: Cannot divide by zero.")
#include <stdio.h>
#include <math.h>

int main() {
    float num1, num2;
    float sum, difference, product, quotient, remainder;

    printf("--- Decimal Arithmetic Operations ---");

    printf("Enter the first decimal number: ");
    scanf("%f", &num1);

    printf("Enter the second decimal number: ");
    scanf("%f", &num2);

    sum = num1 + num2;
    difference = num1 - num2;
    product = num1 * num2;

    printf("\nResults:\n");
    printf("Sum:        %.2f\n", sum);
    printf("Difference: %.2f\n", difference);
    printf("Product:    %.2f\n", product);

    if (num2 != 0.0f) {
        quotient = num1 / num2;
        remainder = fmod(num1, num2);
        printf("Division (Quotient): %.2f\n", quotient);
        printf("Remainder:           %.2f\n", remainder);
    } else {
        printf("Division and Remainder: Cannot divide by zero.\n");
    }

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  num1        NUMBER := &Enter_First_Decimal;
  num2        NUMBER := &Enter_Second_Decimal;
  sum_val     NUMBER;
  diff_val    NUMBER;
  prod_val    NUMBER;
  ratio_val   NUMBER;
  rem_val     NUMBER;
BEGIN
  sum_val := num1 + num2;
  diff_val := num1 - num2;
  prod_val := num1 * num2;

  DBMS_OUTPUT.PUT_LINE('Number 1: ' || num1);
  DBMS_OUTPUT.PUT_LINE('Number 2: ' || num2);
  DBMS_OUTPUT.PUT_LINE('Sum: ' || sum_val);
  DBMS_OUTPUT.PUT_LINE('Difference: ' || diff_val);
  DBMS_OUTPUT.PUT_LINE('Product: ' || prod_val);

  IF num2 != 0 THEN
    ratio_val := num1 / num2;
    rem_val := MOD(num1, num2);
    DBMS_OUTPUT.PUT_LINE('Ratio (Division): ' || ratio_val);
    DBMS_OUTPUT.PUT_LINE('Remainder: ' || rem_val);
  ELSE
    DBMS_OUTPUT.PUT_LINE('Division and Remainder: Cannot divide by zero.');
  END IF;
END;
/

Explanation

  • Java: Uses double for decimal numbers and Scanner for input. Standard operators +, -, *, /, and % work directly with doubles. Includes division by zero check.
  • Python: Uses float() to convert string input to floating-point numbers. Standard operators +, -, *, /, and % handle floats. Includes division by zero check.
  • C: Employs float or double types. scanf("%f", ...) for input, printf("%.2f", ...) for formatted output. fmod() from <math.h> is used for floating-point remainder. Requires explicit if check for division by zero.
  • Oracle: Implemented in PL/SQL using the NUMBER data type, which handles both integers and decimals. Substitution variables are used for input. MOD() is used for the remainder. DBMS_OUTPUT.PUT_LINE for displaying results. Division by zero is handled.

Flowchart

graph TD
    A[Start] --> B[Get num1 decimal]
    B --> C[Get num2 decimal]
    C --> D[Calculate operations]
    D --> E[Display results]
    E --> F{num2 != 0?}
    F -- Yes --> G[Calculate division]
    G --> H[Display division]
    H --> I[End]
    F -- No --> J[Display error]
    J --> I

Practice Problems

  • Extend the program to include exponential calculations.
  • Explore precision issues with floating-point numbers in each language.
  • Implement a calculator that handles both integer and decimal inputs.