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...

Subtract Two Numbers Without Minus Operator in C

C Program to Subtract Two Numbers Without Using Minus Operator

✅ C Program to Subtract Two Numbers Without Using Minus Operator

#include <stdio.h>
int main() {
    int a, b;
    printf("Enter two numbers (a - b):\n");
    scanf("%d %d", &a, &b);

    while (b != 0) {
        int borrow = (~a) & b;   // borrow calculation
        a = a ^ b;               // subtraction using XOR
        b = borrow << 1;         // shift borrow to left
    }

    printf("Difference is: %d\n", a);
    return 0;
}
  

๐Ÿ“˜ Explanation:

This program performs subtraction without using the minus (-) operator. Instead, it uses bitwise operators:

  • borrow = (~a) & b → Finds the borrow bits.
  • a = a ^ b → Performs subtraction without borrow.
  • b = borrow << 1 → Shifts borrow to the correct place.
  • The loop continues until no borrow is left.

๐Ÿงพ Sample Output:

Enter two numbers (a - b):
15 7
Difference is: 8
  

๐Ÿ”‘ Keywords:

C program subtraction without minus, bitwise subtraction in C, subtraction without arithmetic operator, coding interview bitwise questions

๐Ÿ“Œ Hashtags:

#CProgramming #BitwiseOperators #Subtraction #InterviewPrep #LearnC

๐Ÿ” Search Description:

Learn how to subtract two numbers in C without using minus operator. Uses XOR, AND, NOT, and shift operations. Explained with code and output.

Comments

Popular Posts

๐ŸŒ™