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

๐ŸŒ™