Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

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

๐ŸŒ™