Category Archives: C Code

C codes.

Print given square matrix in spiral order

Print givensquare matrix in spiral order :

Complexity : O(n^2)

Approach :

Step1 : write a 4 for loop for printing boundry

step 2: Apply the step1 for inner martrix by modifying starting point and ending point of matrix

step 3: Aplly step2 untill the matrix size becomes 1 or 0


Input :


1     2     3    4

12  13  14  5

11   16 15   6

10   9     8    7


Output :


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16


C code to print given square matrix in spiral order :


find pair of numbers in array that add to given sum

Find pair of numbers in array that add to given sum :

Complexity: O(n logn)

Approach :

step 1: Sort an array using efficient sorting method

step 2: use two variable i & j , initialize i as starting position of array (i=0) and j as last position of array (j=n-1). Where n is number of element in array.

step 3:  if(A[i]+A[j]<x) i++

else if(A[i]+A[j]>x) j–

else (A[i]+A[j]==x) print A[i] , A[j] and  i++

step 4: Repeat step 3 untill i<j


Code to Find pair of numbers in array that add to given sum :


Counting sort

Counting Sort :

n is  number of values in array , k is the range of values (max – min )

Compexity : O(n+k)

Counting sort is a sorting technique to sort an array in linear time. It uses an extra space for counting occurrence  of each object in array. This technique is efficient when range of data is not much larger than number of inputs to be sorted.



Kth smallest element in Array

C code to find out Kth smallest element in array .

Approach 1:

Sort the array using any sorting method and return the value of kth index.

Approach 2:

Using partition function of quick sort which return the index of the pivot value.

comparing the returning value from partition function with k.



C program to calculate size of binary tree

Problem statement :

Given a Binary tree write a c program to calculate size of binary tree

Explanation :

Size of Binary tree is defined as total no of nodes in binary tree.

Solution :

Applying recursive approach

if root is NULL then size is zero

else size of left subtree ,right subtree and +1 for the root node.


Click here to get binary search tree implementation code.

Find max depth of Binary tree

Problem Statement :

Given a binary tree find the max depth of Binary Tree.

Solution :

Height of binary tree defined as max path length from root to leaf node. if root  is NULL then height is defined as zero. Here we can apply recursive approach as maximum of height of left subtree and right subtree with +1 value of root node .


Click here to get binary search tree implementation code.


create csv file in c

A .CSV  Comma Separated Values is a file which contains tabular data in form of text. We can separate each cell in table by representing comma (,) in .csv format.

Here is the c program to represent 2D array into csv format.

This code creates a csv file  and stores info of 2D array which contains marks of 3 subjects (Physics , chemistry , maths ) along with student id.

C code to create csv file in c




C code to draw indian flag

Indian flag drawing code :
indianflag in c

C Code to draw indian flag


Iterative postorder traversal in BST code in c

As far we have seen iterative inorder traversal in BST using stack. Most traversal are solved using stack in BST. Here iterative postorder traversal in BST also implemented using 2 stacks.

Approach :

Two stacks S1 & S2 

Stack2 (S2) is used only for output purpose only

step 1: push the root in  Stack S1

step2: pop the element from s1  and mark as current node then push into S2

step 3: if popped element has left element then push it into s1

step 4: if popped element has right element then push it into s1

step 5: Follow from step2 to step4 untill stack s1 is emply.

Step 6: Pop out and print the all element from s2


Function for iterative postorder traversal :


Complete C implementation with BST implementation and iterative postorder traversal  function