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

Binary to Decimal Conversion in C

Binary to Decimal Conversion in C

✅ Binary to Decimal Conversion in C

#include<stdio.h>
int main( )
{
    int num, decimal = 0, remainder, j = 1;
    printf("Enter binary number:\n");
    scanf("%d", &num);
    while(num != 0)
    {
        remainder = num % 10;
        decimal = decimal + remainder * j;
        j = j * 2;
        num = num / 10;
    }
    printf("The decimal value is %d\n", decimal);
}
  

๐Ÿ“˜ Explanation:

This program converts a binary number (input as an integer) into its decimal equivalent.
- It extracts each digit (right to left) using modulus and multiplies it by powers of 2.
- The value is added to a `decimal` variable.
- `%d` is used in printf to display the result in decimal format.

๐Ÿงพ Sample Output:

Enter binary number:
1010
The decimal value is 10
  

๐Ÿ”‘ Keywords:

Binary to Decimal, C Program for Number Conversion, Beginner C Code, scanf printf usage, while loop example, base-2 to base-10

๐Ÿ“Œ Hashtags:

#CProgramming #BinaryToDecimal #BeginnerCode #NumberConversion #whileLoop #scanf #printf #LogicInC

Comments

Popular Posts

๐ŸŒ™