Integer

An Integer is a whole number that can be positive, negative, or zero. In programming, integers are used to perform arithmetic operations and control the flow of programs using loops and conditional statements. Unlike floating-point numbers, integers do not have fractional parts.

Examples:

Python:

  1. Creating Integers:

    positive_integer = 42
    negative_integer = -42
    zero = 0
    
  2. Arithmetic Operations:

    sum = 10 + 5         # Addition
    difference = 10 - 5  # Subtraction
    product = 10 * 5     # Multiplication
    quotient = 10 / 5    # Division
    remainder = 10 % 3   # Modulus (remainder)
    
    print(sum)           # Output: 15
    print(difference)    # Output: 5
    print(product)       # Output: 50
    print(quotient)      # Output: 2.0
    print(remainder)     # Output: 1
    
  3. Comparisons:

    a = 10
    b = 20
    
    print(a > b)         # Output: False
    print(a < b)         # Output: True
    print(a == b)        # Output: False
    print(a != b)        # Output: True
    
  4. Increment and Decrement:

    count = 0
    
    count += 1           # Increment by 1
    print(count)         # Output: 1
    
    count -= 1           # Decrement by 1
    print(count)         # Output: 0
    
  5. Using Integers in Loops:

    for i in range(5):
        print(i)         # Output: 0, 1, 2, 3, 4
    
    countdown = 5
    while countdown > 0:
        print(countdown) # Output: 5, 4, 3, 2, 1
        countdown -= 1
    

Integers are crucial in programming for counting, indexing arrays, controlling loops, and performing precise arithmetic operations without the rounding errors that can occur with floating-point numbers.