# Print Binary representation of a number in c

C code to print binary representation of a number :

# Check whether given number is palindrome or not

Approach :

store the given number into some other variable then reverse the number and compare it with stored no. If both the numbers are same then the given number is palindrome.

Example :

Given No             Reversed No                     Equal               Palindrome

123                           321                                       No                            No

121                            121                                      Yes                         Yes

————————————————————————————————————————————————————————–

C code to check whether given number is palindrome or not.

Code provided by Lalit Kumar

# 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

end

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.

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

# 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