Common Running Times

Linear Time An algorithm that runs in O(n), or linear, time has a very natural property: Its running time is at most a constant factor times the size of the input. One basic way to get an algorithm with this running time is to process the input in a single pass, spending a constant amount... Continue Reading →

Programming Principles

•Problem specification –our approach must be to determine overall goals, but precise ones, and then slowly divide the work into smaller problems until they become of manageable size. •Program design –Each part of a large program must be well organized, clearly written, and thoroughly understood, •Choice of  data structures –How they are arranged in relation... Continue Reading →

Doubly Linked List

Doubly linked list is a type of linked list in which each node apart from storing its data has two links. The first link points to the previous node in the list and the second link points to the next node in the list. The first node of the list has its previous link pointing to... Continue Reading →

Linear Linked List

Representation of Linear linked list Suppose we want to store the list of integer numbers, then the linear linked list can be represented in memory with the following declarations.                         typedef struct nodetype                         {                                     int info;                                     struct nodetype *next;                         }node;                         node *head; The above declaration define a new data type,... Continue Reading →

Transpose of a matrix in C

#include <stdio.h> int main() { int matrix[10][10], transpose[10][10], row, col, i, j; printf("Enter rows and columns: "); scanf("%d %d", &row, &col); printf("\nEnter matrix elements:\n"); for (i = 0; i < row; ++i) for (j = 0; j < col; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &matrix[i][j]); } printf("\nEntered... Continue Reading →

Matrix Addition in C

#include <stdio.h> int main() { int row, col, mat1[100][100], mat2[100][100], matsum[100][100], i, j; printf("Enter the number of rows: "); scanf("%d", &row); printf("Enter the number of columns"); scanf("%d", &col); printf("\nEnter elements of 1st matrix:\n"); for (i = 0; i < row; ++i) for (j = 0; j < col; ++j) { printf("Element a%d%d: ", i +... Continue Reading →

Hosoya’s Triangle or Fibonacci Triangle in C

The Hosoya's triangle (originally Fibonacci triangle) is a triangular arrangement of numbers as it is shown in pascal triangle based on the Fibonacci series. #include<stdio.h> #include<stdlib.h> int main(){ int a=0,b=1,i,c,n,j; system("cls"); printf("Enter the number of lines you want:"); scanf("%d",&n); for(i=1;i<=n;i++) { a=0; b=1; printf("%d\t",b); for(j=1;j<i;j++) { c=a+b; printf("%d\t",c); a=b; b=c; } printf("\n"); } return 0; }

Triangular Number Pattern in C

#include<stdio.h> #include<stdlib.h> int main(){ int i,j,k,l,n; system("cls"); printf("enter the range="); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n-i;j++) { printf(" "); } for(k=1;k<=i;k++) { printf("%d",k); } for(l=i-1;l>=1;l--) { printf("%d",l); } printf("\n"); } return 0; }

Matrix Multiplication in C

#include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter number of rows and columns of matrix 1 \n"); scanf("%d%d", &m, &n); printf("Enter elements of first matrix\n"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d",... Continue Reading →

Automorphic number in C

An Automorphic number is a number whose square ends with the same digits as the original. example: 5, 6 etc. #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int num, square, temp, last; int n =0; printf("Enter a number \n"); scanf("%d",&num); square = num*num; temp = num; while(temp>0){ n++; temp = temp/10; } int den =... Continue Reading →

C program to swap 2 numbers without using 3rd variable

It can be done using two methods: By using + and -By using * and / Method 1: By using + and - #include<stdio.h> int main() { int num1, num2; printf("Enter first number: ") ; scanf("%d",&num1); printf("\nEnter second number: ") ; scanf("%d",&num2); printf("\n Before swapping \n number_1= %d \n number_2 = %d",num1,num2); num1=num1+num2; num2=num1-num2; num1=num1-num2;... Continue Reading →

To check for Armstrong number in C

Armstrong number is a number which is equal to the sum of cubes of its digits. e.g. 0, 1, 153 etc are the Armstrong numbers. Demonstration: 153=(1*1*1)+(5*5*5)+(3*3*3) cube of 1=1 cube of 5=125 cube of 3=27 1+125+27=153 #include<stdio.h> int main() { int n,q,sum=0,temp; printf("Enter a number="); scanf("%d",&n); temp=n; while(n>0) { q=n%10; sum=sum+(q*q*q); n=n/10; } if(temp==sum) printf("The given... Continue Reading →

Fibonacci Series

Fibonacci series is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. e.g. 0, 1, 1, 2, 3, 5, 8, etc. The Rule is to write a Fibonacci series is: xn = xn−1 + xn−2 where: xn is term number "n"xn−1 is the previous term (n−1)xn−2 is the term before that (n−2)... Continue Reading →

Binomial Coefficient

Binomial coefficient is a positive integer that occur as coefficient in the Binomial Theorem. It can be defined as the coefficient of the monomial Xk in the expansion of (1 + X)n . This coefficient also occurs in the formula given below #include <stdio.h> int Coeff(int n, int k) { if (k > n) return 0; if (k... Continue Reading →

Factorial of a Big Number

#include<stdio.h> int main() { int arr[200],n,count,temp,i; arr[0]=1; count=0; printf("Enter the number whose factorial you want to find: "); scanf("%d",&n); for(; n>=2; n--) { temp=0; for(i=0; i<=count; i++) { temp=(arr[i]*n)+temp; arr[i]=temp%10; temp=temp/10; } while(temp>0) { arr[++count]=temp%10; temp=temp/10; } } for(i=count; i>=0; i--) printf("%d",arr[i]); return 0; }

HCF and LCM

HCF can be defined as the Highest Common Factor. It means the greatest which can exaclty divide the given numbers without the remainder. It is also known as Greatest Common Divisor (GCD). Program to find the HCF of 2 numbers. #include <stdio.h> int main() { int num1, num2, i, hcf; printf("Enter two numbers whose HCF... Continue Reading →

Quick Sort

#include<stdio.h> void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition(int arr[], int p , int q) { int x= arr[p]; int i= p; for(int j=p+1;j<=q;j++) { if(arr[j]<x) { i++; swap(&arr[i],&arr[j]); } } swap(&arr[p],&arr[i]); return i; } void QuickSort(int arr[],int p, int q) { if(p<q) {... Continue Reading →

Merge Sort

#include<stdio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("\nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right... Continue Reading →

Generating Random numbers in a given range.

#include <stdio.h> #include <stdlib.h> int main() { int num, randomnum; printf("Enter the number of random numbers you want to enter: \n"); scanf("%d",&num); printf("%d Random no. b/w to 10-99: \n",num); while (num--) { randomnum=rand()%90+10; printf("%d\n",randomnum); } getch(); return 0; }

Insertion sort

This program also calculates the time taken by the insertion sort function to run using time() functions. In this program the user generates random numbers using the rand() function in an array and then applies insertion sort to it. Large input is taken in order to get some value of the time taken by the... Continue Reading →

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started