Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

C Program to Convert Decimal to Binary

C Program to Convert Decimal to Binary

✅ C Program to Convert Decimal Number to Binary

#include <stdio.h>

int main() {
    int num, binary[32], i = 0;

    printf("Enter a decimal number: ");
    scanf("%d", &num);

    if (num == 0) {
        printf("Binary: 0\n");
        return 0;
    }

    while (num > 0) {
        binary[i] = num % 2;   // store remainder (0 or 1)
        num = num / 2;         // reduce number
        i++;
    }

    printf("Binary: ");
    for (int j = i - 1; j >= 0; j--) {  // print in reverse
        printf("%d", binary[j]);
    }
    printf("\n");

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program converts a decimal number into its binary representation:

  • We repeatedly divide the number by 2 and store the remainders (0 or 1).
  • These remainders are stored in an array.
  • Finally, we print the array in reverse order to get the correct binary format.
  • If the number is 0, the output is directly 0.

๐Ÿงพ Sample Output:

Enter a decimal number: 10
Binary: 1010

Enter a decimal number: 25
Binary: 11001
  

๐Ÿ”‘ Keywords:

C program decimal to binary, decimal to binary conversion, division by 2 method in C, binary number C program, beginner C examples

๐Ÿ“Œ Hashtags:

#CProgramming #BinaryConversion #DecimalToBinary #CExamples #CodingForBeginners

๐Ÿ” Search Description:

This C program converts a decimal number into binary using division by 2 method. It stores remainders in an array and prints them in reverse order with example outputs.

Comments

Popular Posts

๐ŸŒ™