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

Multiply Two Numbers Without Using * Operator in C

C Program to Multiply Two Numbers Without Using * Operator

✅ C Program to Multiply Two Numbers Without Using * Operator

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

    int x = a, y = b;
    while (y > 0) {
        if (y & 1) {        // if last bit of y is 1
            result += x;    // add current value of x
        }
        x <<= 1;            // multiply x by 2
        y >>= 1;            // divide y by 2
    }

    printf("Product is: %d\n", result);
    return 0;
}
  

πŸ“˜ Explanation:

This program performs multiplication without using the multiplication (*) operator. It applies the Russian Peasant Multiplication Algorithm:

  • if (y & 1) → If last bit of multiplier is 1, add current value of multiplicand.
  • x <<= 1 → Left shift doubles the multiplicand.
  • y >>= 1 → Right shift halves the multiplier.
  • Loop continues until multiplier becomes 0.

🧾 Sample Output:

Enter two numbers (a * b):
6 7
Product is: 42
  

πŸ”‘ Keywords:

C program multiplication without *, bitwise multiplication in C, multiply without arithmetic operator, coding interview bitwise questions

πŸ“Œ Hashtags:

#CProgramming #BitwiseOperators #Multiplication #InterviewPrep #LearnC

πŸ” Search Description:

Learn how to multiply two numbers in C without using the multiplication operator (*). Uses bitwise shift and addition. Explained with code and output.

Multiply Two Numbers Without Using * Operator in C

✅ Multiply Two Numbers Without Using * Operator in C

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

    int positive = 1;

    // Handle negative values
    if (b < 0) {
        b = -b;
        positive = -1;
    }

    for (int i = 1; i <= b; i++) {
        mul = mul + a;
    }

    mul = positive * mul;  // Adjust sign

    printf("Multiplication of given numbers is: %d\n", mul);
    return 0;
}
  

πŸ“˜ Explanation:

This program multiplies two numbers without using the * operator. It performs multiplication using repeated addition logic. Here's how it works:

  • If the second number is negative, we make it positive and remember the sign.
  • We add a to the result b times using a loop.
  • Finally, we apply the sign to get the correct result.

πŸ–₯️ Sample Output:

Enter any two numbers:
5 -4
Multiplication of given numbers is: -20
  

πŸ”‘ Keywords:

multiply without star, no multiplication operator, C multiplication with addition, how to multiply in C, multiplication tricks in C, logic building programs

πŸ“Œ Hashtags:

#CProgramming #MultiplyWithoutOperator #BitwiseLogic #NoMultiplication #BeginnerC #OperatorTricks #LogicalCPrograms

πŸ” Search Description:

This C program multiplies two integers without using the multiplication operator (*). It uses a loop with repeated addition, includes sign adjustment for negative numbers, and is useful for learning low-level logic implementation.

Comments

Popular Posts

πŸŒ™