Skip to main content

Posts

Showing posts from May, 2015

C Program To Implement Bubble Sort Algorithm.

Bubble sort algorithm starts by comparing the first two elements of an array and swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if the first element is greater than second then, you need to swap the elements but, if the first element is smaller than second, you mustn't swap the element.  Then, again second and third elements are compared and swapped if it is necessary and this process go on until last and second last element is compared and swapped. This completes the first step of bubble sort. If there are n elements to be sorted then, the process mentioned above should be repeated n-1 times to get required result. But, for better performance, in second step, last and second last elements are not compared as the element is automatically placed at last after first step.  In the third step, last and second last, second last and third last elements are not compared and so on. Graphical representation of bubble sort:

C Program To Implement Stack Operations.

Stack is an area of memory that holds all local variables and parameters used by any function. It also remembers the order in which functions are called so that function returns occur correctly. Stack is a LIFO data structure. Stack operations are PUSH (insert operation), POP (Delete operation), and Display (stack). Each time a function is called, its local variables and parameters are “pushed onto” the stack. When the function returns, these locals and parameters are “popped”. #include <stdio.h> #define MAXSIZE 25 struct stack { int stk[MAXSIZE]; int top; }; typedef struct stack STACK; STACK s;

C Program To Find The Nth Fibonacci Number.

In Fibonacci series, each number is the sum of the two preceding numbers. For example: 0, 1, 1, 2, 3, 5, 8, ….., ∞ (infinity). The following programs return the nth number entered by user residing in the Fibonacci series. Method 1 (Using recursion): Time Complexity: T(n) = T(n-1) + T(n-2) which is exponential, i.e. this implementation does a lot of repeated work. Extra Space: O(n) if we consider the function call stack size, otherwise O(1). #include<stdio.h> int fib(int n) { if ( n <= 1 ) return n; return fib(n-1) + fib(n-2); } int main () { int n ; printf(" Enter Number to Compute nth Fibonacci Series: "); scanf("%d",&n); printf("\n\n %dth term of Fibonacci Series is %d\n",n, fib(n)); return 0; }

C Program To Solve Tower Of Hanoi Problem Using Recursion.

The Towers of Hanoi is a mathematical game or puzzle. The objective is to move the all of the discs one at a time from an arbitrary peg to another. Putting a larger disc over a smaller one must be avoided at all times and the transfer must be made in the least possible moves, which is 2^n - 1, where n is the number of disks. For example, the minimum number of moves required to move three discs are 2^3 - 1 = 7. #include <stdio.h> void towers(int, char, char, char); int main() { int num; printf("Enter the number of disks: "); scanf("%d", &num);

C Program To Display Switch…Case Statement.

The following program asks user an arithmetic operator (+,-,*,/) and two operands and perform the corresponding calculation on the operands. # include <stdio.h> int main() { char o; float num1,num2; printf("Enter an operator: either + or - or * or /\n\n=> "); scanf("%c",&o); printf("\nEnter two numbers:\n"); scanf("%f%f",&num1,&num2);

C Program To Store Information Using Struct Data Type.

A struct is a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory.   #include <stdio.h> #include <string.h> struct Books  { char title[50]; char author[50]; char subject[100]; int book_id;   }; int main( )  { struct Books Book1; struct Books Book2;

C Program To Print Multiplication Table.

Method 1: The following program generates a multiplication table for a given integer to a given range. #include <stdio.h> int main()  { int n, range, i; printf("Enter the value of n: "); scanf("%d",&n); printf("\nEnter the range: "); scanf("%d",&range); for(i=1;i<=range;++i)  { printf("%2d X %2d = %3d\n", n, i, n*i); } return 0; }