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

๐ŸŒ™