C Source Code

Hello World

Program to display a line of text on terminal


Do while loop

The program shows an example using do...while loop

A do...while loop executes a set of instructions in a loop untill a given condition is satisfied. The program break out of he loop as soon as the condition fails.

do{

//do something

}while(condition is true)
;


Odd/Even Program in C

Odd/Even determination for a given number.


This is a very basic program that shows, how to determine whether the given number is even or odd in C language. The program simply prompts the user for a number, and displays whether the given number is odd or even.

The program uses a basic mathematical property, that every even number is completely divisible by 2. If a number is not completely divisible by 2,it is an odd number. Here we've used the modulo operator (%) to find the remainder after dividing the number by 2.


It's a very simple program, but using the modulo(%) trick, is very helpful in various, not so obvious problems.


Addition of two numbers

Take two numbers from the user (integer values) and display their sum


Verification of a given number if prime or not

Accepts a value from user and checks whether the given number is a prime number or not using function.



Printing a pyramid using asterisks

The program draws a pyramid on the console.
The program prompts the user for height and then draws the pyramid of the required height on the terminal.


Fibonacci Series without recursion

Program in C to calculate the series upto the N'th fibonacci number. The program demonstrates a fast and efficient implementation(for small purposes), for calculating fibonacci series.

The program also demonstrates the use of memoization technique to calculate fibonacci series in almost no time. Memoization helps reduce redundant computation by storing the previously calculated results in memory temporarily,so that they can be used later to speed up the total computation. This program does not use recursion.

In this example we've used a "long long int" type array to store the fibonacci series.You can get fibonacci series correct upto 92'nd fibonacci number,after which the overflow occurs as the size of the numbers exceed the limit which "long long int" data type can hold can hold.


Menu driven program for basic arithmetic operations

This program demonstrates simple arithmetic operations on given numbers using menu system.The main objective of this program is to demonstrate the program flow control by user selected options from the given menu. The program uses switch structure to implement this feature


Linear search using arrays

Linear search in C using arrays. The program shows an implementation of linear search algorithm in C language on integer array.
The program initially takes n number of integer elements from the user and stores them in an array. It then asks the user for an element to search, and then performs linear search on the input data given by the user.
Algorithm: Linear search is one of the simplest search algrithms.In linear search, we look at each element one after the other in the array untill the key to be searched is found or we reach the end of the array.
If the key to be searched is found before reaching the end or at the end, we immediately return with success.However if we reach the end of the array and still couldn't locate the key we return with failure.


Finding length of given string(without using string library)

Calcualates length of the given string using user defined function.The string is taken as character array. The user is first prompted for input string and its length is calculated using self defined function and displayed


String operations using Built in functions

Implements basic string operations like calulating length,concatenation,copy etc using standard library string functions.The objective of this code is just to demonstrate the use of the standard library functions. No error checking has been performed while taking user input


Calculation of factorial

The program takes an integer input from the user and calculates the factorial of that number. The factorial() function expects only integer inputs.No error checking is performed and all inputs are assumed to be valid.


Basic Set Operations using integer arrays

Demonstrates basic set operations using integer arrays. The program prompts the user for number of elements in each set(only two sets are considered for this program) and then takes the input. No error checking is done while taking input. Assumes all inputs are valid.The program provides a menu to the user to perform various operations on sets.The arrays are passed to different functions using pointers


Basic Matrix operations using 2D integer arrays

Implements basic matrix operations like addition,subtraction,multiplication transpose and saddle point using 2D integer arrays.The program prompts the user for matrix dimensions(assumes 10X10 to be max size for this program) and takes the inputs from the user. No error checking is performed while taking user input. Assumes all inputs all valid


Finding the maximum and minimum in an array

The program demonstrates, how to find the maximum and minimum number in an integer array. The array contents are taken from the user.The array of appropriate size is created in the beginning by taking the size from the user.


Program for bubble sort using dynamic array

Implements bubble sort on integer array,by dynamically increasing the size of the array as the user gives input.

To stop entering the input values the user is asked to give a sentinel value. After the data is entered the program sorts the given array using bubble sort and displays the result.


Selection sort,Insertion sort,Bubble sort algorithms

This program demonstrates the implementation of various sorting algorithms for sorting integer arrays.
The program takes an integer array from the user as input. The user can enter the numbers in any random order.After taking the inputs, the user is given a menu control, and any of the above mentioned sorting algorithms(i.e, bubble sort, selection sort,insertion sort)can be used to sort the given list.

Sorting algorithms have different time and space complexities, and depending upon the requirement, the appropriate sorting algorithm is used in a particular situation.
The purpose of this program was just to demonstrate how these different sorting algorithms can be implemented in C language.


Polynomial addition using arrays in C

Polynomial addition in C using arrays
This program shows the implementation of polynomial addition using arrays.
The program expects the user to enter the polynomial with integer exponent term while it supports floating value for coefficient terms.The program takes the values for the polynomial terms in the descending order,starting from the highest degree terms in the beginning and lowest degree terms in the end.

The coefficients and exponent of the polynomial are stored in a structure. An array of such structure is used to represent a polynomial.
The program takes two polynomials as input and gives out the sum of the two polynomials.

Note: Since we are using the header file,don't forget to link the math library while compiling this code with gcc.
gcc filename.c -lm


Polynomial addition using arrays in C

Polynomial addition in C using arrays
This program shows the implementation of polynomial addition using arrays.
The program expects the user to enter the polynomial with integer exponent term while it supports floating value for coefficient terms.The program takes the values for the polynomial terms in the descending order,starting from the highest degree terms in the beginning and lowest degree terms in the end.

The coefficients and exponent of the polynomial are stored in a structure. An array of such structure is used to represent a polynomial.
The program takes two polynomials as input and gives out the sum of the two polynomials.

Note: Since we are using the header file,don't forget to link the math library while compiling this code with gcc.
gcc filename.c -lm


Sum of Array using Pointer

In this program the sum of array is calculated using pointers.


Simple Interest

In this program the simple interest is calculated by using formulae (Principal * Rate of Interest * Time)/100.


Table of value n

This program prints the table of the given value n from n * 1 = n to n * 10 = 10n.


Largest Number in a Array

In this we have to find the largest element in the array.
For this we have first considered that first element is larger,then we have iterated through the array from 1 to n position to check whether a larger element exists or not.


Reverse a number

In this code a given number is reversed.
For eg. Given number is 1234 then output will be 4321.


LCM of 2 number

In this code a the lcm of given 2 number is calculated.
For this we have started looping from 1 to a*b where a and b are two number.
For eg. Given number is 2 and 3 then output will be 6.


Factorial of a number

In this code factorial of given number is found.
For eg. Given number is 5 then output will be 120.


Addition of n number

In this code the addition of given array is calculated.