Logical errors

These are the type of errors that occurs when incorrect logic is implemented in the code and the code produces unexpected output.

Example:

Find GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers that is the largest number that divides both of them.

For finding the GCD of two numbers we will first find the minimum of the two numbers and then find the highest common factor of that minimum which is also the factor of the other number.

Wrong Implementation of Code for the above Problem Statement:

C++
// C++ program to find GCD of two numbers
#include <iostream>
using namespace std;
// Function to return gcd of a and b
int gcd(int a, int b)
{
    int result = min(a, b); // Find Minimum of a and b
    while (result > 0) {
        if (a % result != 0 && b % result != 0) {
            break;
        }
        result--;
    }
    return result; // return gcd of a and b
}

// Driver program to test above function
int main()
{
    int a = 98, b = 56;
    cout << "GCD of " << a << " and " << b << " is "
        << gcd(a, b);
    return 0;
}
Java
public class Main {
    // Function to return gcd of a and b
    static int gcd(int a, int b) {
        int result = Math.min(a, b); // Find Minimum of a and b
        while (result > 0) {
            if (a % result != 0 || b % result != 0) { // Use || (logical OR) instead of && (logical AND)
                break;
            }
            result--;
        }
        return result; // return gcd of a and b
    }

    // Driver program to test above function
    public static void main(String[] args) {
        int a = 98, b = 56;
        System.out.println("GCD of " + a + " and " + b + " is " + gcd(a, b));
    }
}
Python3
def gcd(a, b):
    result = min(a, b)  # Find Minimum of a and b
    while result > 0:
        if a % result != 0 or b % result != 0:
            break
        result -= 1
    return result  # return gcd of a and b

# Driver program to test above function
if __name__ == "__main__":
    a, b = 98, 56
    print(f"GCD of {a} and {b} is {gcd(a, b)}")
C#
using System;

public class Program
{
    // Function to return gcd of a and b
    public static int Gcd(int a, int b)
    {
        int result = Math.Min(a, b); // Find Minimum of a and b
        while (result > 0)
        {
            if (a % result != 0 && b % result != 0)
            {
                break;
            }
            result--;
        }
        return result; // return gcd of a and b
    }

    // Driver program to test above function
    public static void Main(string[] args)
    {
        int a = 98, b = 56;
        Console.WriteLine($"GCD of {a} and {b} is {Gcd(a, b)}");
    }
}
Javascript
// Function to return gcd of a and b
function gcd(a, b) {
    let result = Math.min(a, b); // Find Minimum of a and b
    while (result > 0) {
        if (a % result !== 0 || b % result !== 0) {
            break;
        }
        result--;
    }
    return result; // return gcd of a and b
}

// Driver program to test above function
function main() {
    let a = 98, b = 56;
    console.log(`GCD of ${a} and ${b} is ${gcd(a, b)}`);
}

// Calling the main function
main();
//This code is contributed by Utkarsh.

Output
GCD of 98 and 56 is 55


Expected Output: GCD of 98 and 56 is 14

In the above code, the Output produced by the code and the expected output are different. Hence, we can say that there is a logical error in the above code. If we review the above code, we can see the condition, if (a % result != 0 && b % result != 0) is not correct. It should be if (a % result == 0 && b % result == 0).

Types of Issues and Errors in Programming/Coding

Where there is code, there will be errors

If you have ever been into programming/coding, you must have definitely come across some errors. It is very important for every programmer to be aware of such errors that occur while coding.

Table of Content

  • 1. Syntax errors: 
  • 2. Logical errors:
  • 3. Runtime errors:
  • 4. Time Limit exceeded error:

In this post, we have curated the most common types of programming errors and how you can avoid them.

Similar Reads

1. Syntax errors:

These are the type of errors that occur when code violates the rules of the programming language such as missing semicolons, brackets, or wrong indentation of the code,...

2. Logical errors:

These are the type of errors that occurs when incorrect logic is implemented in the code and the code produces unexpected output....

3. Runtime errors:

These are the errors caused by unexpected condition encountered while executing the code that prevents the code to compile. These can be null pointer references, array out-of-bound errors, etc....

4. Time Limit exceeded error:

Time Limit Exceeded error is caused when a code takes too long to execute and execution time exceeds the given time in any coding contest. TLE comes because the online judge has some restrictions that the code for the given problem must be executed within the given time limit....