C++ Source Code

Hello World

Program to display a line of text on terminal


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.
eg:

eg:
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.
eg:
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


Simple Interest program

The program demonstrates the use of classes in C++ using simple interest program as an example."SimpleInterest" class holds the various parameters and functions required to calculate interest.


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.



Palindrome using stack

This program takes an input string from the user and checks whether the given string is plaindrome or not.
The program uses STL library's stack class.


Reversing words in an input text stream

This program is an example to show how you can process text in C++.
This program reverses individual words in an input text stream.
eg:
Input: This is an example text
Output: sihT si na elpmaxe txet

The program uses standard template library's(STL) "stack" data structure.


Arithmetic operation on complex numbers using function overloading

The program demonstrates the implementation of complex number class and performs simple arithmetic operation using operator overloading.The program supports only floating point value for demonstration purposes.
The program also uses the math library for power function;therefore make sure to link it as well while compiling.
The command to compile this file would be of the form:
g++ complex.cpp -o complex -lm
where complex.cpp is the name of the source file.
Since we are using math library in the program we need to explicitly link it using the -l flag at the command line.


Bubble sort

Implementation of bubble sort using object orientation in C++.Sorts an array of integer elements in ascending order using the bubble sort algorithm.


String operations using object orientation in C++ and operator overloading

Implements various string operations using object orientation and operator overloading.
String class is created to implement these functions for the string operations.
The program uses the standard C++ library for these functions.The purpose of this program is just to demonstrate operator overloading to perform these string operations.
Following is the list of the operators overloaded and their effects on the string objects:
1) == : String copy
2) = : String compare
3) + : String concatenation
4) << : Displaying the string from my_string object
5) >> : Reversing the string
6) / : Finding substring


Matrix operations using templates

Basic matrix operations are implemented using function templates. Function templates make the functions, independent of any type.

In this example the add(),sub(),mul() functions are type independent. The compiler automatically generates the appropriate code by examining the function argument during compilation.

Thus we don't have to write the same code multiple times to support different types,the compiler automatically handles everything.


Stack using SLL(Singly Linked List)

The program demostrates the implementation of stack data structure using singly linked list.

Stack is a FIFO(First In First Out)data-structure. Basic stack operations like push,pop,empty are implemented using classes.


Matrix operations using templates

Basic matrix operations are implemented using function templates. Function templates make the functions, independent of any type.

In this example the add(),sub(),mul() functions are type independent. The compiler automatically generates the appropriate code by examining the function argument during compilation.

Thus we don't have to write the same code multiple times to support different types,the compiler automatically handles everything.


Database application using STL(Standard Template Library) functions

The program shows an implementation of a student database using the STL library functions.
The student class keeps track of all of a student's personal data.
For each new student, a new student object is instantiated and vectors are used to store them.
Vectors are just like arrays with contiguous memory locations and data can be accessed efficiently as in arrays.
However unlike arrays,vectors can dynamically expand in size which is automatically handeled by the storage container class.
The database program here, does not perform input validation. All inputs are assumed valid.


Binary tree creating and traversals

The program shows creation and traversal of binary tree in preorder,inorder and postorder sequence using recursive and non recursive algorithms.

Recursive procedure for traversal of binary tree is very easy to implement and is defined within the btree class itself.

For non-recursive procedures stack data structure is used to keep track of the visited nodes and path to backtrack.

Stack class takes care of stack operations used in the traversal algorithm.

Class node is created for representing the nodes in the binary tree.