Skip to main content

Featured

C Program to Solve Two Sum Using Brute Force (With Algorithm & Output)

 Introduction The Two Sum problem is a popular coding interview question where we must find two indices of an array whose values add up to a given target. This program demonstrates a simple brute-force solution in C using nested loops and dynamic memory allocation. Problem Statement Given an integer array and a target value, return the indices of the two numbers such that they add up to the target. Each input has exactly one solution, and the same element cannot be used twice. The result should return the indices, not the values. If no solution exists, return NULL.  Algorithm / Logic Explanation Start the program. Traverse the array using a loop from index 0 to numsSize - 1 . Inside this loop, use another loop starting from i + 1 to numsSize - 1 . For every pair (i, j) , check if nums[i] + nums[j] == target . If condition becomes true: Allocate memory for 2 integers using malloc() . Store indices i and j . Set returnSize = 2 . Return the result poi...

C++ Function Overloading Example (Different Parameters)

C++ Function Overloading Example (Different Parameters)

✅ C++ Program: Function Overloading with Different Parameters

#include <iostream>
using namespace std;

class Math {
public:
    void add(int a, int b) {
        cout << "Sum of two ints: " << a + b << "\n";
    }

    void add(double a, double b) {
        cout << "Sum of two doubles: " << a + b << "\n";
    }

    void add(int a, int b, int c) {
        cout << "Sum of three ints: " << a + b + c << "\n";
    }
};

int main() {
    Math m;
    m.add(10, 20);        // calls add(int, int)
    m.add(5.5, 2.5);      // calls add(double, double)
    m.add(1, 2, 3);       // calls add(int, int, int)
}
  

๐Ÿ“˜ Explanation:

This program shows Function Overloading in C++. The same function name add() is used with:

  • add(int, int) → Adds two integers
  • add(double, double) → Adds two doubles
  • add(int, int, int) → Adds three integers
The compiler chooses the correct function based on argument type and count.

๐Ÿงพ Sample Output:

Sum of two ints: 30
Sum of two doubles: 8
Sum of three ints: 6
  

๐Ÿ”‘ Keywords:

C++ function overloading, add function in C++, compile-time polymorphism, OOP concepts, C++ examples

๐Ÿ“Œ Hashtags:

#CPlusPlus #FunctionOverloading #CppExamples #Polymorphism #OOP #Programming

๐Ÿ” Search Description:

This C++ program demonstrates function overloading with different parameter lists (integers and doubles). Example of compile-time polymorphism with sample output.

Comments

Popular Posts

๐ŸŒ™