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++) { ...

C Program to Find Remainder Without Using % Operator

C Program to Find Remainder Without Using % Operator

✅ C Program to Find Remainder Without Using % Operator

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

    int sign = 1;
    if (a < 0) { a = -a; sign = -sign; } // handle negative dividend
    if (b < 0) { b = -b; }               // divisor just made positive

    while (a >= b) {
        a -= b;   // keep subtracting divisor from dividend
    }

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

๐Ÿ“˜ Explanation:

This program calculates the remainder without using the modulus (%) operator. It repeatedly subtracts the divisor from the dividend until the remainder is smaller than the divisor.

  • Handles negative dividends by tracking the sign.
  • a -= b; keeps subtracting divisor until remainder is less.
  • Final result is adjusted using sign * a.

๐Ÿงพ Sample Output:

Enter two numbers (a % b): 17 5
Remainder is: 2

Enter two numbers (a % b): -17 5
Remainder is: -2
  

๐Ÿ”‘ Keywords:

C program remainder without %, modulus without operator, remainder using subtraction, arithmetic operators in C, tricky C programs

๐Ÿ“Œ Hashtags:

#CProgramming #Modulo #InterviewPrep #LearnC #BitwiseTricks

๐Ÿ” Search Description:

Learn how to find remainder in C without using modulus (%) operator. Uses repeated subtraction and handles negative numbers. Includes explanation and sample output.

Comments

Popular Posts

๐ŸŒ™