Skip to main content

Posts

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; }

C Program To Implement String Operations.

The C language provides no explicit support for strings in the language itself. The string-handling functions are implemented in libraries. The string library (string.h or strings.h) has some useful functions for working with strings, like strcpy, strcat, strcmp, strlen, strcoll, etc. We will take a look at some of these string operations.  #include <stdio.h> #include<string.h> #include<ctype.h> #include<stdlib.h> int length(char str[]); void reverse(char str[]); int palindrome(char str[]); void copy(char str1[], char str2[]); int compare(char str1[], char str2[]); void concat(char str1[], char str2[]); void search(char str1[], char str2[]); void count(char str1[]); int main() { char a[100], b[100]; int result, option;

C Program To Multiply Two Matrices Using Multi-dimensional Arrays.

#include <stdio.h> int main()  { int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k; printf("Enter Rows and Columns for First Matrix:\n"); scanf("%d%d", &r1, &c1); printf("\nEnter Rows and Columns for Second Matrix:\n"); scanf("%d%d",&r2, &c2); while (c1!=r2)  { printf("\nWrong Input: Column of First Matrix is Not Equal to Row of Second Matrix.\n\n"); printf("\nEnter Rows and Columns for First Matrix:\n"); scanf("%d%d", &r1, &c1); printf("\nEnter Rows and Columns for Second Matrix:\n"); scanf("%d%d",&r2, &c2); }

C Program To Replace Lowercase Characters By Uppercase Or Vice-Versa.

This program checks each character one by one. If the character is in lowercase, then it is converted into uppercase. But if it is in uppercase then converts into lowercase. The source code is successfully compiled and run on any operating system. #include <stdio.h> #include <ctype.h> int main() { char sentence[100]; int count, ch, i; printf("Enter One or Multiple Sentence:\n\n\t"); for (i = 0; (sentence[i] = getchar()) != '\n'; i++) {         ; }

C Program To Find The Sum Of Diagonal Elements Of A Matrix.

The properties of any element Aij will be diagonal element if and only if i = j. #include<stdio.h> int main(){ int a[100][100],i,j,sum=0,m,n; printf("\nEnter the row and column of matrix: \n"); scanf("%d %d",&m,&n); printf("\nEnter the elements of matrix: \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n");