Author: admin

  • C Program to print Odd Number Pattern

    In this tutorial, we will learn how to print odd number patterns in C. Before that, you may go through the following topic in C.

    The program takes a user input for the number of rows and prints the number pattern of the pyramid for odd numbers in C.

    C Program to print Odd Number Pattern

    #include <stdio.h>
    
    void main()
    {
      int i, j, k = 1, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = 1; j <= i; j++)
        {
          printf("%2d ", k);
          k = k + 2;
        }
    
        printf("\n");
      }
    
      getch();
    }

    Output:

    c program to print odd number pattern
  • C Program to print Floyd’s Triangle

    In this tutorial, we will write Floyd’s triangle pattern program in C. Before that, you should have knowledge on the following topic in C.


    Floyd’s Triangle in C

    #include <stdio.h>
    
    void main()
    {
      int i, j, k = 1, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      printf("Floyd's Triangle:\n");
      for (i = 1; i <= rows; i++)
      {
        for (j = 1; j <= i; j++, k++)
        {
          printf("%2d ", k);
        }
    
        printf("\n");
      }
    
      getch();
    }

    Output:

    Floyd's Triangle pattern in C

  • Hollow Triangle Star Pattern in C

    In this tutorial, we will write a C program to print a hollow triangle star pattern. Before that, you may go through the following topics in C.

    Hollow Triangle Pattern in C

    Hollow equilateral triangle pattern in C.

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = i; j < rows; j++)
        {
          printf(" ");
        }
    
        for (k = 1; k <= (2 *i - 1); k++)
        {
          if (k == 1 || i == rows || k == (2 *i - 1))
            printf("*");
          else
            printf(" ");
        }
        printf("\n");
      }
    
      return 0;
    }

    Output:


  • Hourglass Star Pattern in C

    In this tutorial, we will write a C program to print Full sandglass star patterns. Before that, you may go through the following topic in C.

    The program below takes a user input for the number of rows needed and then in decreasing order of the upper half is printed and then increasing the order, the second half sandglass is printed.

    This is a full hourglass pattern program in C or you can say full sandglass pattern program using a star.

    Full Hourglass Pattern in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      printf("Output: \n\n");
      //upper half
      for (i = 1; i <= rows; i++)
      {
        for (k = 1; k < i; k++)
          printf(" ");
    
        for (j = i; j <= rows; j++)
          printf("* ");
    
        printf("\n");
      }
    
      //lower half
      for (i = rows - 1; i >= 1; i--)
      {
        for (k = 1; k < i; k++)
          printf(" ");
    
        for (j = i; j <= rows; j++)
          printf("* ");
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    Enter the number of rows: 5
    * * * * * 
     * * * *
      * * *
       * *
        *
        *
       * * 
      * * *
     * * * * 
    * * * * *

  • Diamond Shape Star Pattern in C

    In this tutorial, we will write a program for diamond patterns in C using stars. Before that, you should have knowledge on the following topic in C.


    Diamond Shape Star Pattern in C

    Question: C Program to Print Diamond Pattern:

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 0; i <= rows - 1; i++)
      {
        for (j = rows - 1; j >= i; j--)
          printf(" ");
    
        for (k = 0; k <= i; k++)
          printf("* ");
    
        printf("\n");
      }
      for (i = 0; i <= rows - 1; i++)
      {
        for (j = -1; j <= i; j++)
          printf(" ");
    
        for (k = 0; k <= rows - 2 - i; k++)
          printf("* ");
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    diamons shape pattern in C

  • C Program to Print Pascal Triangle

    In this tutorial, we will write a program to print pascal triangle in c. Before that, you should have knowledge on the following topic in C.

    Pascal’s triangle is one of the important examples that is taught to the student. The number outside Pascal’s triangle is zero (0), which is not visible. It is a pattern where the triangle starts from 1 at the top and below is the addition of the upper level as you can see in the figure below. The addition continues until the required level is reached.

    pascal's triangle

    C Program to Print Pascal Triangle

    #include <stdio.h>
    
    int main()
    {
      int rows, i, j, numCoef = 1, gap;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = 0; i < rows; i++)
      {
        for (gap = 1; gap <= rows - i; gap++)
          printf("  ");
    
        for (j = 0; j <= i; j++)
        {
          if (j == 0 || i == 0)
            numCoef = 1;
          else
            numCoef = numCoef *(i - j + 1) / j;
    
          printf("%4d", numCoef);
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Output: Pascal’s Triangle in C.

    Enter the no. of rows: 6
                   1
                 1   1
               1   2   1
             1   3   3   1
           1   4   6   4   1
         1   5  10  10   5   1

  • Implementation of Shell Sort Algorithm in C

    In this tutorial, we will write a C program to implement shell sort. Before that, you may go through the following topics in C.

    Shell Sort Algorithm

    Shell sort is an in-place comparison-based sorting algorithm and variation of Insertion sort. It is a better version of Insertion sort in comparison-based. It can compare the elements far apart whereas Insertion compares adjacent elements.

    In shell sort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h’th element are sorted.

    Shell sort

    Implementation of Shell Sort Algorithm in C

    Question: C program to implement Shell sort using function.

    // Shell Sort in C programming
    #include <stdio.h>
    
    //function prototype
    void shellSort(int array[], int n);
    
    int main()
    {
      int arr[30], n, i;
    
      printf("Enter the number of elements: ");
      scanf("%d", &n);
    
      printf("Enter %d Elements:\n", n);
      for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
      //calling sorting function
      shellSort(arr, n);
    
      //Display the result
      printf("\nArray after sorting: \n");
      for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    
      return 0;
    }
    
    // Shell sort
    void shellSort(int arr[], int n)
    {
      //Rearrange the array elements at n/2, n/4, ..., 1 intervals 
      for (int interval = n / 2; interval > 0; interval /= 2)
      {
        for (int i = interval; i < n; i += 1)
        {
          int temp = arr[i];
          int j;
    
          for (j = i; j >= interval && arr[j - interval] > temp; j -= interval)
            arr[j] = arr[j - interval];
    
          arr[j] = temp;
        }
      }
    }

    Output:

    Enter the number of elements: 6
    Enter 6 Elements:
    55
    3
    356
    124
    54
    18

    Array after sorting:
    3 18 54 55 124 356

    Time Complexity of Shell Sort:

    • Best case:  0(nlogn)
    • Average case:  0(n(logn)^2)
    • Worst case:  0(n(logn)^2)

  • Implementation of Quick SortAlgorithm in C

    In this tutorial, we will write a C program to implement quick sort. Before that, you may go through the following topics in C.

    Quick Sort Algorithm

    Quicksort algorithm is a way of rearranging the elements in an array in ascending or descending order. Quicksort is another Divide and Conquer algorithm.

    It takes two empty arrays to hold elements, the first is less than the pivot value and the second element greater than the pivot value, and then sort the element around it or inside it. It involves Swapping and partitioning a section of the array.


    Implementation of Quick SortAlgorithm in C

    Question: C program to implement Quick sort using function.

    // implementation of quick sort in C program
    
    #include <stdio.h>
    
    // function prototype
    void quickSort(int a[], int start, int end);
    int partitionFunc(int a[], int start, int end);
    
    int main()
    {
      int arr[30], n, i;
    
      printf("Enter the number of elements: ");
      scanf("%d", &n);
    
      printf("Enter %d Elements:\n", n);
      for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
      //calling sorting function
      quickSort(arr, 0, n - 1);
    
      //Display the result
      printf("\nArray after sorting: \n");
      for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    
      return 0;
    }
    
    int partitionFunc(int a[], int start, int end)
    {
      int pivot = a[end];	// pivot element  
      int i = (start - 1);
    
      for (int j = start; j <= end - 1; j++)
      {
        // If current is smaller than the pivot  
        if (a[j] < pivot)
        {
          i++;
    
          //swap
          int t = a[i];
          a[i] = a[j];
          a[j] = t;
        }
      }
    
      //swap
      int t = a[i + 1];
      a[i + 1] = a[end];
      a[end] = t;
    
      return (i + 1);
    }
    
    //quickSort Function 
    void quickSort(int a[], int start, int end)
    {
      if (start < end)
      {
        //p_index is the partitioning index
        int p_index = partitionFunc(a, start, end);
    
        quickSort(a, start, p_index - 1);
        quickSort(a, p_index + 1, end);
      }
    }

    Output:

    Enter the number of elements: 6
    Enter 6 Elements:
    12
    65
    5
    224
    68
    45

    Array after sorting:
    5 12 45 65 68 224

    Time Complexity of Quick Sort:

    • Best case:  0(n log n)
    • Average case:  0(n log n)
    • Worst case:  0(n^2)

  • Implementation of HeapSort Algorithm in C

    In this tutorial, we will write a C program to implement heap sort. Before that, you may go through the following topics in C.

    Heap Sort Algorithm

    Heap is a tree in heap sorting that possesses some specific properties whose value should be greater than or equal to that of the children node.

    Heapsort is a comparison-based sorting algorithm and is also considered as the improved Selection sort. It is based on a Binary Heap data structure so to understand we must know two types of data structure:
    Arrays and trees

    Binary heap

    Learn more on Heap sort.


    Implementation of HeapSort Algorithm in C

    Question: C program to implement Heap sort using function.

    // implementation of heap sort in C program
    
    #include <stdio.h>
    
    //function prototype
    void heapSort(int arr[], int n);
    void heapify(int arr[], int size, int i);
    
    //main function
    int main()
    {
      int arr[30], n, i;
    
      printf("Enter the number of elements: ");
      scanf("%d", &n);
    
      printf("Enter %d Elements:\n", n);
      for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
      //calling sorting function
      heapSort(arr, n);
    
      //Display the result
      printf("\nArray after sorting: \n");
      for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    
      return 0;
    }
    
    void heapSort(int arr[], int n)
    {
      // create heap
      for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
    
      // One by one extract an element from heap
      for (int i = n - 1; i > 0; i--)
      {
        // arrA[0] being max element
        int x = arr[0];
        arr[0] = arr[i];
        arr[i] = x;
    
        // call max 
        heapify(arr, i, 0);
      }
    }
    
    void heapify(int arr[], int size, int i)
    {
      int largest = i;  // root
    
      //Indexes
      int leftChild = 2 *i + 1;
      int rightChild = 2 *i + 2;
    
      // If left child is larger than root
      if (leftChild < size && arr[leftChild] > arr[largest])
        largest = leftChild;
    
      // If right child is larger than root
      if (rightChild < size && arr[rightChild] > arr[largest])
        largest = rightChild;
    
      // If root is not largest
      if (largest != i)
      {
        int swap = arr[i];
        arr[i] = arr[largest];
        arr[largest] = swap;
    
        // Recursive call
        heapify(arr, size, largest);
      }
    }

    Output:

    Enter the number of elements: 6
    Enter 6 Elements:
    25
    98
    3
    124
    66
    9

    Array after sorting:
    3 9 25 66 98 124


  • Implement Insertion sort Program in C

    In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in C:

    Insertion Sort Algorithm

    Insertion sort is a simple sorting algorithm that sorts the elements in an array by comparing the values at index with all its prior elements. This process takes more time so it is only used for small data set.

    This sorting takes the first index element and compares it to its largest value in an array and it moves the element to its right location.


    C Program to implement Insertion sort

    Question: Insertion sort program in C using function.

    // implementation of insertion sort in C program
    
    #include <stdio.h>
    
    void insertionSort(int arr[], int n)
    {
      int i, j, key;
    
      for (i = 1; i < n; i++)
      {
        key = arr[i];
        j = i - 1;
    
        //moving the value
        while (j >= 0 && arr[j] > key)
        {
          arr[j + 1] = arr[j];
          j = j - 1;
        }
        arr[j + 1] = key;
      }
    }
    
    int main()
    {
      int arr[30], n, i;
    
      printf("Enter the number of elements: ");
      scanf("%d", &n);
    
      printf("Enter %d Elements:\n", n);
      for (i = 0; i < n; i++)
        scanf("%d", &arr[i]);
    
      //calling sorting function
      insertionSort(arr, n);
    
      //Display the result
      printf("\nArray after sorting: \n");
      for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    
      return 0;
    }

    Output:

    Enter the number of elements: 6
    Enter 6 Elements:
    55
    14
    77
    2
    115
    99

    Array after sorting:
    2 14 55 77 99 115

    Time Complexity of Heap Sort:

    • Best case:  0(n)
    • Average case:  0(n^2)
    • Worst case:  0(n^2)