Vous êtes sur la page 1sur 19

1. C code to print Fibonacci series by recursion #include<stdio.

h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d %d ",0,1); printFibonacci(n); return 0; } void printFibonacci(int n){ static long int first=0,second=1,sum; if(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); printFibonacci(n-1); } } Sample output: Enter the range of the Fibonacci series: 10 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

2. Factorial program by recursion in c

#include<stdio.h> int fact(int); int main(){ int num,f; printf("\nEnter a number: "); scanf("%d",&num); f=fact(num); printf("\nFactorial of %d is: %d",num,f); return 0; } int fact(int n){ if(n==1) return 1; else return(n*fact(n-1)); }

3.C code to solve tower of hanoi recursively #include < stdio.h > int N=3,N1=1,N2=2,N3=3; void hanoi (int n, int current, int destination, int medium) { if(n!=0) { hanoi(n-1, current, medium, destination); printf( "move disk %c from %i to %i\n", (char)(n+'A'-1), current, destination); hanoi(n-1, medium, destination, current); } } int main (void) { hanoi(N, N1, N3, N2); return 0;

4.Find gcd of a number using recursion in c program #include<stdio.h> int main(){ int n1,n2,gcd; printf("\nEnter two numbers: "); scanf("%d %d",&n1,&n2); gcd=findgcd(n1,n2); printf("\nGCD of %d and %d is: %d",n1,n2,gcd); return 0; } int findgcd(int x,int y){ while(x!=y){ if(x>y) return findgcd(x-y,y); else return findgcd(x,y-x); } return x; } 5.Find power program of a number using recursion using c

#include<stdio.h> int main(){ int pow,num; long int res; long int power(int,int); printf("\nEnter a number: ");

scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); res=power(num,pow); printf("\n%d to the power %d is: %ld",num,pow,res); return 0; } int i=1; long int sum=1; long int power(int num,int pow){ if(i<=pow){ sum=sum*num; power(num,pow-1); } else return sum; }

6.C code to reverse a string by recursion: #include<stdio.h> #define MAX 100 char* getReverse(char[]); int main(){ char str[MAX],*rev; printf("Enter any string: "); scanf("%s",str); rev = getReverse(str); printf("Reversed string is: %s",rev); return 0; } char* getReverse(char str[]){ static int i=0;

static char rev[MAX]; if(*str){ getReverse(str+1); rev[i++] = *str; } return rev; } Sample output: Enter any string: mona Reversed string is: anom

6.A program to swap two elements without using third variable. #include <stdio.h> void main() { int a,b; printf("enter number1: ie a"); scanf("%d",a); printf("enter number2:ie b "); scanf("%d",b); printf(value of a and b before swapping is a=%d,b=%d"a,b); a=a+b; b=a-b; a=a-b; printf(value of a and b after swapping is a=%d,b=%d"a,b); }

7.REMOVE DUPLICATE ELEMENTS IN AN ARRAY USING C PROGRAM


#include<stdio.h> int main(){ int arr[50]; int *p; int i,j,k,size,n; printf("\nEnter size of the array: "); scanf("%d",&n); printf("\nEnter %d elements into the array: ",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); size=n; p=arr; for(i=0;i<size;i++){ for(j=0;j<size;j++){ if(i==j){ continue; } else if(*(p+i)==*(p+j)){ k=j; size--; while(k < size){ *(p+k)=*(p+k+1); k++; } j=0; } } } printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++){ printf(" %d",arr[i]); } return 0; }

8.a program to search an element using Linear Search

#include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("The number is not in the list"); else printf("The number is found"); return 0; } Sample output: Enter the size of an array: 5 Enter the elements of the array: 4 6 8 0 3 Enter the number to be search: 0 The number is found

9.BUBBLE SORT USING C PROGRAM


#include<stdio.h> int main(){

int s,temp,i,j,a[20]; printf("Enter total numbers of elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting algorithm for(i=s-2;i>=0;i--){ for(j=0;j<=i;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

Output: Enter total numbers of elements: 5 Enter 5 elements: 6 2 0 11 9 After sorting: 0 2 6 9 11

10.BINARY SEARCH USING C PROGRAM


#include<stdio.h> int main(){

int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; while(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; } Sample output: Enter the size of an array: 5 Enter the elements in ascending order: 4 7 8 11 21 Enter the number to be search: 11 The number is found.

11.SELECTION SORT USING C PROGRAM

#include<stdio.h> int main(){ int s,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=0;i<s;i++){ for(j=i+1;j<s;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("After sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } Output: Enter total elements: 5 Enter 5 elements: 4 5 0 21 7 The array after sorting is: 0 4 5 7 21

12.C PROGRAM FOR INSERTION SORT

#include<stdio.h> int main(){ int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); for(i=1;i<s;i++){ temp=a[i]; j=i-1; while((temp<a[j])&&(j>=0)){ a[j+1]=a[j]; j=j-1; } a[j+1]=temp; } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; } Output: Enter total elements: 5 Enter 5 elements: 3 7 9 0 2 After sorting: 0 2 3 7 9

13.Write a program to insert a new element in the given sorted array at proper place. #include<stdio.h> #include<conio.h>

Void main() { Int x [10]; Int I,j,n,m,temp,key,pos; Clrscr(); Printf (Enter how many elements\n); Scanf(%d,&n); Printf (Enter the elements\n); For (i=0; i<n; i++) { Scanf(%d, &x[i]); } Printf (Input array elements are \n); For (i=0; i<n; i++) { Printf (%d\n, x[i]); } For (i=0; i<n; i++) { For (j=i+1; j<n; j++) { If (x[i] > x[j])

{ Temp=x[i]; X[i]=x[j]; X[j]=temp; } } } Printf (Sorted list is \n); For (i=0; i<n; i++) { Printf (%d\n,x[i]); } Printf (enter the elements to be inserted\n); Scanf(%d,&key); For (i=0; i<n; i++) { If (key < x[i]) { Pos = i; Break; } }

} M=n-pos+1; For (i=0; i=m; i++) { X[n-i+2]=x[n-i+1]; } X[pos]=key; Printf(Final list is \n); For (i=0; i<n+1; i++) { Printf (%d\n,x[i]); } }
14.a program to delete an element from the kth position of the given unsorted array.
#include<stdio.h> #include<conio.h>
int main() { int i,j,item,n,a[10],u,k; printf("enter the limit\n"); scanf("%d",&n); if(n>=10) { printf("Array exceed limit .correct the limit value below 10 \n"); } else { printf("enter the inserted element"); scanf("%d",&item);

printf("enter the position\n"); scanf("%d",&k); if(k<=n||k<=10) { printf("enter the terms\n"); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("inserted list\n"); for(i=1;i<=n;i++) { printf("%d\n",a[i]); } printf("inser list\n"); j=n; while(j>=k) { a[j+1] =a[j]; j=j-1; } a[k]=item; for(i=1;i<=n+1;i++) { printf("%d\n",a[i]); } } else { printf("postion given in below limit not equal to \n"); } } getch(); return 0; }

15.C program to delete an element from an array


#include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]); printf("Enter the location where you wish to delete element\n");

scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.\n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array is\n"); for( c = 0 ; c < n - 1 ; c++ ) printf("%d\n", array[c]); } return 0; }

16.C program to merge two arrays


#include <stdio.h> void merge(int [], int, int [], int, int []); int main() { int a[100], b[100], m, n, c, sorted[200]; printf("Input number of elements in first array\n"); scanf("%d", &m); printf("Input %d integers\n", m); for (c = 0; c < m; c++) { scanf("%d", &a[c]); } printf("Input number of elements in second array\n"); scanf("%d", &n); printf("Input %d integers\n", n); for (c = 0; c < n; c++) { scanf("%d", &b[c]); } merge(a, m, b, n, sorted); printf("Sorted array:\n"); for (c = 0; c < m + n; c++) { printf("%d\n", sorted[c]); } return 0; } void merge(int a[], int m, int b[], int n, int sorted[]) {

int i, j, k; j = k = 0; for (i = 0; i < m + n;) { if (j < m && k < n) { if (a[j] < b[k]) { sorted[i] = a[j]; j++; } else { sorted[i] = b[k]; k++; } i++; } else if (j == m) { for (; i < m + n;) { sorted[i] = b[k]; k++; i++; } } else { for (; i < m + n;) { sorted[i] = a[j]; j++; i++; } } } }

17.C program for addition of two matrices using arrays source code. Matrix addition in c language:
C program for addition of two matrices

C code:
#include<stdio.h> int main(){ int a[3][3],b[3][3],c[3][3],i,j; printf("Enter the First matrix->"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<3;i++)

for(j=0;j<3;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",a[i][j]); } printf("\nThe Second matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",b[i][j]); } for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("\nThe Addition of two matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",c[i][j]); } return 0; } 18.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 the number of rows and columns of first matrix\n"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d", &p, &q);

if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.\n"); else { printf("Enter the elements of second matrix\n"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = { for ( d { for ( { sum } 0 ; c < m ; c++ ) = 0 ; d < q ; d++ ) k = 0 ; k < p ; k++ ) = sum + first[c][k]*second[k][d];

multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-\n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%d\t", multiply[c][d]); printf("\n"); } } return 0; }