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 Pattern Program: Hourglass Star Shape

C Pattern Program: Hourglass Star

๐Ÿ”ท C Pattern Program: Hourglass Star Shape

#include <stdio.h>

int main()
{
    int num;
    printf("Enter the number: ");
    scanf("%d", &num);

    // Upper half
    for (int i = 0; i < num; i++)
    {
        for (int j = 0; j < (num - i - 1); j++)
        {
            printf(" ");
        }
        printf("*\n");
    }

    // Lower half
    for (int i = 1; i < num; i++)
    {
        for (int j = 0; j < i; j++)
        {
            printf(" ");
        }
        printf("*\n");
    }

    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program prints a vertical hourglass-like star pattern using spaces and a single * per row.

๐Ÿ”น The first loop prints the upper half: decreasing spaces followed by a star.
๐Ÿ”น The second loop prints the lower half: increasing spaces followed by a star.
๐Ÿ”น Together, it creates a symmetrical hourglass shape centered around the vertical axis.

๐Ÿ” Sample Output:

Enter the number: 5
    *
   *
  *
 *
*
 *
  *
   *
    *
    

๐Ÿท️ Keywords:

C pattern printing, star pattern, diamond pattern, vertical hourglass, single star pattern, C loop pattern examples

Comments

Popular Posts

๐ŸŒ™