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: Print Binary Representation of a Number

Binary Representation of Number in C

✅ C Program: Binary Representation of a Number

#include<stdio.h>
int main( )
{
    unsigned int num;
    printf("Enter the number:\n");
    scanf("%u", &num);

    for(int i = 31; i >= 0; i--)
    {
        if(num & (1 << i))
            printf("1");
        else
            printf("0");
    }
    printf("\n");
}
  

๐Ÿ“˜ Explanation:

This program prints the binary representation of an unsigned integer using bitwise operators.

  • The user inputs a number.
  • The loop checks all 32 bits (from MSB to LSB).
  • If the bit is set, it prints 1; otherwise, 0.
  • This uses the expression (num & (1 << i)) to test each bit.

๐Ÿงพ Sample Output:

Enter the number:
5
00000000000000000000000000000101
  

๐Ÿ”– Keywords:

C Program, Binary in C, Bitwise Operator in C, Binary Print, 32-bit Output, C Interview Questions, Unsigned Integer Handling

๐Ÿ“Œ Hashtags:

#CProgramming #BitwiseOperators #BinaryOutput #InterviewPreparation #AdSenseReady #CodingBlog

Comments

Popular Posts

๐ŸŒ™