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

CHECK IF SYSTEM IS LITTLE ENDIAN OR BIG ENDIAN IN C

Check System Endianness in C

✅ CHECK SYSTEM ENDIANNESS IN C

#include <stdio.h>

int main() {
    int x = 0x12AB6578;
    char *ptr = (char *)&x;

    if (*ptr == 0x78) {
        printf("System is Little Endian:\n");
    } else {
        printf("System is Big Endian:\n");
    }

    return 0;
}
    

๐Ÿง  Explanation:

This program determines the endianness of the system.

  • A hexadecimal number is stored in memory: 0x12AB6578.
  • The memory address of x is typecast to a char*, pointing to the least significant byte.
  • If the first byte contains 0x78, the system is Little Endian (least significant byte stored first).
  • Otherwise, the system is Big Endian.

๐Ÿ–ฅ️ Sample Output:

System is Little Endian:
    

๐Ÿ”‘ Keywords:

C program to check endianness, little endian vs big endian, pointer typecasting in C, memory representation in C, system architecture test.

Comments

Popular Posts

๐ŸŒ™