## Queues and its implementation in C

A Queue is a First In First Out (FIFO) which is an ordered collection of items which are deleted at the front end and inserted at the rear end. A simple queue is represented as below –         Simple Sequential Queue A Queue in C may be defined as an array as…

## Game Trees

Game Tree is one of the applications of Binary Trees. For example the one implemented for the Tic-Tac-Toe Game. X | X | X O | O |     O |   | The game is between two players who makes moves ‘X’ and ‘O’ on a board of 3×3 positions. The players who wins is…

## Representing Lists As Binary Trees in C

Case 1: A list could be represented as a Binary Tree with all the list elements along with their information at the leaf nodes. For example, the linked list ->[A| ]->[B| ]->[C| ]-> ………[I| ] could be represented as following Binary Tree – 5 / \ 2 2 / \ / \ 1 2 1…

## HUFFMAN Algorithm and its implementation in C

Huffman trees are used as message encoding and message decoding trees which is the basic construct of compression and de. The specialty of Huffman tree is that – it helps to reduce the bit codes of each symbol in the message according to their relative frequency of occurrence. the same tree constructed could be used for…

## Threaded Binary Tree and its implementation in C

Threaded Binary Trees are those in which the NULL right pointers of all nodes are THREADED to its immediate inorder successor in the Binary Tree. Such pointers or THREADS are also called RIGHT IN threads as they are always threaded to right. Example Structure of a Threaded Binary Tree A / \ B C / \…

## Traversing a Binary Tree in C

There are generally 3 traversal strategies for Binary Trees – INORDER, PREORDER and POSTORDER. For eg:, the inorder, preorder and postorder traversal of an expression tree gives the infix, prefix and postfix forms of the expression. Such a traversal could be used to evaluate the value of expression. Example 1 : Consider the following Binary Tree –…

## C program implementing Binary Tree Search to find duplicates of a file

A Binary Search is such a Binary Tree in which the info field of any of the nodes to the left of a particular node nd will be < that of nd and of those to the right of nd will be >= nd. The in-order traversal of such a tree will give the sorted form…

## Binary Tree representation and Primitive Operations on a Binary Tree in C

Binary Trees are one of the core data structures used in any programming language to implement complex systems. C programming language offers various binary tree operations that could be used by the developer. Binary Trees can be dynamically represented in C as shown below – struct nodetype{ int info; struct nodetype * left; struct nodetype * right;…

## C program to print sum up-to each individual preceding number until the entered number

Here is a C program which accepts an integer and prints out sum from 1 to 1, 1 to 2, 1 to 3 …… 1 up-to that number – main(){ int n,i,s=0; printf(“\nEnter any integer>>”); scanf(“%d”,&n); for(i = 1; i

## C program for Hailstones series

Hailstones is a sequence of numbers that could be generated from any positive number by performing a specific operation on it until it reaches 1. The fact is that any positive number will reach 1 if we perform this operation. If the number is even, we divide it by 2, otherwise we multiply the number…

## C program to generate random numbers in any range

Here is a C program to generate random numbers between any range of two digit numbers – #include #include main(){ int s, a, b, n, ran; srand(time(0)); // this is to set the seed to current time so we get different random numbers in each try printf(“\nEnter lower limit>>”); scanf(“%d”,&a); printf(“\nEnter upper limit>>”); scanf(“%d”,&b); printf(“\nEnter…

## for loops in C

The ‘for’  loop is another important looping structure which could replace ‘while’  and ‘do .. while’ loops. It has a special feature that the initialization, test condition and the increment or decrements of test variable could be placed in a single line following the ‘for’ keyword as shown below – for (initialization; test condition; change…

## C program to test for a prime number

Below is a C program to test for a prime number – main(){ float n, count = 0; i = 3; a; printf(“\nEnter any number of your choice>>”); scanf(“%f”, &n); if(n == 0 || n == 1 || n == 2) printf(“\n%d is not a prime number\n”, n); while(i > 2 & i < n){...

## C program to implement Russian Peasant

Here is a C program that I wrote years back to implement Russian Peasant method of an interesting multiplication technique – main(){ int a, b, sum = 0; printf(“\nEnter the two digits to be multiplied separated by space>> “); scanf(“%d %d”, &a, &b); printf(“%d “,a); printf(” %d\n”,b); do{ if(a % 2) sum += b; else…

## C program to find numbers whose sum of cubes of individual numbers is that number

Here is an implementation of while loop in C to find numbers whose sum of cubes of individual numbers is that number. You can go a step further and make it so that you accept value of i and n. main(){ int i = 1, a, b, c, n = 1000; printf(“\n Enter a number…

## C program to find circumference and area of a circle from given radius

Here is a simple C program to find circumference and area of a circle from given radius – main(){ float r, pi = 3.1416, c, area; printf(“\nEnter value of radius>> “); scanf(“%f”, &r); while (r > = 0.0){ c = 2*pi*r; area = pi*r*; printf(“\nCircumference = %f”, c); printf(“\nArea = %f”, area); scanf(“%f”, &r) } }

## While Loop in C

Like do-while loop, While loop is the most commonly used looping construct to repeat a process or calculation for a set or collection of inputs or storage based on a condition. It is a looping technique when you want to execute the required process based on a condition for the entire input collection. Below is…

## C program to find the sum of squares of first n natural numbers

Here is a simple C program to find the sum of squares of first n natural numbers using do-while looping construct – main(){ int n, sum =0; printf(“\nInput any integer of your choice>>”); scanf(“%d”,&n); nsave = n; do{ sum += n*n; n–; } while(n > 0); printf(“Sum of squares upto %d = %d \n”, nsave,…

## Do – While Loops in C

Looping constructs is an important programming language technique to repeat a process or calculation for a set or collection of inputs or storage based on a condition. Do – While is a looping technique when you want to execute the required process for at least once and then check the condition for the rest of the…

## Sample C program showing if – else constructs

Below are simple C programs for beginners to learn if-else decision construct. Example 1: Program to check if a number is odd or even main (){ int n; printf(“Please enter any number>> “); scanf(“%d”, &n); if(n % 2 == 0) printf(“\n%d is EVEN \n”, n); else printf(“\n%d is ODD \n”, n); } Example 2: Program…

## C Program to check for leap year

Here is a C program to check for leap year – main () { int year; printf(“Please enter any year as 4 digits>> “); scanf(“%d”, &year); if(year % 4 ==0 && year % 100 !=0 || year % 400 == 0) printf(“%d is a leap year \n”, year); else printf(“%d is not a leap year…

## Making decision in C program

Decision making is an important aspect of programming language that enables the programmer to arrive at a conclusion based on certain data processing, comparisons and conditional situations. In most programming language there are inbuilt functions and language constructs that enable implicit decision making. In other situations, it offers a combination of language constructs such as comparison…