Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

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

๐ŸŒ™