Blog

  • 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)

  • C Program for implementation of Selection sort

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

    Selection Sort Algorithm

    The selection sort is a simple sorting algorithm which is an in-place comparison-based algorithm. It has two parts where the left end has a sorted part and the right end has an unsorted part.

    In the beginning, the left end is empty and the right end has all the elements then the smallest element from the right end is brought to the left end and swapped with the leftmost element. This process continues until the element from the right end is brought to the left by swapping.

    Although, it may cause a problem with the large inputs.


    Implementation of Selection sort in C

    Question: Selection sort program in C using function.

    // implementation of selection sort in C program
    #include <stdio.h>
    
    void selectionSort(int arr[], int n)
    {
      int i, j, min, temp;
    
      for (i = 0; i < n - 1; i++)
      {
        // finding the minimum
        min = i;
        for (j = i + 1; j < n; j++)
          if (arr[j] < arr[min])
            min = j;
    
        // Swapping
        temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
      }
    }
    
    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
      selectionSort(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:
    25
    12
    88
    5
    76
    50

    Array after sorting:
    5 12 25 50 76 88

    Time Complexity of Selection Sort:

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

  • C Program to Delete a Particular element from an Array

    In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have knowledge on the following C topics:

    Example:

    Input
    Input array elements: 11 22 30 42 50
    Specify the value to delete: 22

    Output
    Array elements: 11 30 42 50

    Explanation: Here instead of a position of an element, the value of the element is specified to delete. Similarly, the program traverse until the element is found and then shifts all the elements from index + 1 by 1 position to the left.

    And if the value is not that is the entered value does not match with any element present in an array, in that case, the program will display the message “Element Not Valid”.


    C Program to Delete a Particular element from an Array

    #include <stdio.h>
    
    int main()
    {
       int arr[50], element, i, size, pos;
       int element_found = 0;
    
       printf("Enter the size of an array: ");
       scanf("%d", &size);
    
       printf("Enter %d elements\n", size);
    
       for (i = 0; i < size; i++)
          scanf("%d", &arr[i]);
    
       printf("\nArray before Deletion:\n");
       for (i = 0; i < size; i++)
          printf("%d ", arr[i]);
    
       printf("\n\nEnter the element to be deleted: ");
       scanf("%d", &element);
    
       // check the element to be deleted is in array or not
       for (i = 0; i < size; i++)
       {
          if (arr[i] == element)
          {
             element_found = 1;
             pos = i;
             break;	// terminate the loop
          }
       }
    
       if (element_found == 1)  // for element exists
       {
          for (i = pos; i < size - 1; i++)
             arr[i] = arr[i + 1];
       }
       else	//for element does not exists
          printf("\nElement %d is not present in an Array. Try Again!", element);
    
       printf("\nArray after Deletion:\n");
       for (i = 0; i < size - 1; i++)
          printf("%d  ", arr[i]);
    
       return 0;
    }

    Output:

    Enter the size of an array: 5
    Enter 5 elements
    10
    20
    30
    40
    50
    
    Array before Deletion:
    10 20 30 40 50 
    
    Enter the element to be deleted: 30
    
    Array after Deletion:
    10 20 40 50

  • C Program to sort a String in Ascending order

    In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following topics in C


    C Program to sort a String in Ascending order

    #include <stdio.h>
    #include <string.h>
    #define MAX_size 100	//maxiimum size of the string
    
    void main()
    {
      char str[MAX_size], ch;
      int i, j, length;
    
      printf("Enter the string: ");
      fgets(str, sizeof str, stdin);
    
      length = strlen(str);
    
      for (i = 1; i < length; i++)
      {
        for (j = 0; j < length - i; j++)
        {
          if (str[j] > str[j + 1])
          {
            ch = str[j];
            str[j] = str[j + 1];
            str[j + 1] = ch;
          }
        }
      }
    
      printf("\nAfter sorting, the string appears: %s", str);
    }

    Output:

    Enter the string: programs

    After sorting, the string appears:
    agmoprrs


  • C Program to find Maximum occurring Character in a String

    In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through the following topics in C


    Find maximum occurring character in a string in C

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_size 100	//MAX string size
    #define MAX_char 255	//MAX character number
    
    void main()
    {
      char str[MAX_size];
      int freq[MAX_char];
      int i = 0, max;
      int ascii;
    
      printf("Enter the string : ");
      fgets(str, sizeof str, stdin);
    
      for (i = 0; i < MAX_char; i++)
      {
        freq[i] = 0;
      }
    
      //calculating the frequency
      i = 0;
      while (str[i] != '\0')
      {
        ascii = (int) str[i];
        freq[ascii] += 1;
    
        i++;
      }
    
      max = 0;
      for (i = 0; i < MAX_char; i++)
      {
        if (i != 32)
        {
          if (freq[i] > freq[max])
            max = i;
        }
      }
    
      printf("\nCharacter with highest frequency is: '%c'", max);
      printf("\nAnd the number of times, it appears is: '%d'", freq[max]);
    }

    Output:

    Enter the string : Welcome to simple2code

    Character with highest frequency is: 'e'
    And the number of times, it appears is: '4'