Wednesday, March 21, 2018

C Program To Using Recursion – Merge Sort.

/* 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 recursion

merge(a,i,mid,mid+1,j);    //merging of two sorted sub-arrays

}

}

void merge(int a[],int i1,int j1,int i2,int j2)

{

int temp[50];    //array used for merging

int i,j,k;

i=i1;    //beginning of the first list

j=i2;    //beginning of the second list

k=0;

while(i<=j1 && j<=j2)    //while elements in both lists

{

if(a[i]<a[j])

temp[k++]=a[i++];

else

temp[k++]=a[j++];

}

while(i<=j1)    //copy remaining elements of the first list

temp[k++]=a[i++];

while(j<=j2)    //copy remaining elements of the second list

temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]

for(i=i1,j=0;i<=j2;i++,j++)

a[i]=temp[j];

}

Output:-

Merge Sort

Tuesday, March 20, 2018

C Program To Using Recursion – Quick Sort.

/* Prpgram for Recursion Quick Sort */



#include<stdio.h>

int a[50];

void qsort(int,int);

int split(int,int);

int main()

{

int n,i;

printf("How many elements? ");

scanf("%d",&n);

printf("Enter %d elements: \n",n);

for(i=0;i<n;i++)

scanf("%d",&a[i]);

qsort(0,n-1);

printf("The resultant array: \n");

for(i=0;i<n;i++)

printf("%5d",a[i]);

return 0;

}

void qsort(int start,int end)

{

int s;

if(start>=end)

return;

s=split(start,end);

qsort(start,s-1);

qsort(s+1,end);

}

int split(int start,int end)

{

int p=a[start];

int i=start,j=end,temp;

while(i<j)

{

while(a[i]<=p)

i++;

while(a[j]>p)

j--;

if(i<j)

temp=a[i],a[i]=a[j],a[j]=temp;

}

a[start]=a[j];

a[j]=p;

return j;

}

Output:-

C Program To Using Recursion – Quick Sort.

C Program To Using Recursion – GCD.

/* Program For GCD */

#include <stdio.h>

int hcf(int n1, int n2);

int main()

{

int n1, n2;

printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

 

printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2));

return 0;

}

 

int hcf(int n1, int n2)

{

if (n2 != 0)

return hcf(n2, n1%n2);

else

return n1;

}

Output:-

C Program To Using Recursion – GCD.

 

Monday, March 19, 2018

  Program To Generate Fibonacci Series Using Recursion.

/* C Program to print Fibonacci series using recursion */

#include <stdio.h>

#include <conio.h>

 

int fibonacci(int term);

int main(){

int terms, counter;

printf("Enter number of terms in Fibonacci series: ");

scanf("%d", &terms);

/* Nth term = (N-1)th therm + (N-2)th term; */

printf("Fibonacci series till %d terms\n", terms);

for(counter = 0; counter < terms; counter++){

printf("%d ", fibonacci(counter));

}

getch();

return 0;

}

/*

* Function to calculate Nth Fibonacci number

* fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);

*/

int fibonacci(int term){

/* Exit condition of recursion*/

if(term < 2)

return term;

return fibonacci(term - 1) + fibonacci(term - 2);

}

Output:-

  Program To Generate Fibonacci Series Using Recursion.

Sunday, March 18, 2018

Jai Lava Kusa 2017 Telugu HD Full Movies

Jhodaa Akbar 2008 full Movies

C Program  To Print Floyd’s Triangle




/* Print Floyd's Triangle*/

#include <stdio.h>

int main()

{

int rows, i, j, number= 1;

printf("Enter number of rows: ");

scanf("%d",&rows);

for(i=1; i <= rows; i++)

{

for(j=1; j <= i; ++j)

{

printf("%d ", number);

++number;

}

printf("\n");

}

return 0;

}

Output:-

C Program  To Print Floyd’s Triangle

Life Articles And News:

Life Articles And News: