















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various c programming examples using functions. Topics include printing fibonacci series, checking factorial using pointers, swapping numbers using call by reference or pointers, calculating power of a number using a function, printing prime numbers between 1 to 100, finding the largest element in an array, reversing an array using pointers, finding the transpose of a given matrix, multiplying two matrices, implementing binary search, linear search, reversing a string, concatenating two strings, comparing two strings, creating a file, writing to a file, opening a file, and closing a file.
Typology: Lecture notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Programming using Functions Tathagata roy chowdhury^ Developed by, Dept of computer science and engg.^ Assistant Professor GECC, chaibasa
Fibonacci series using function?
#include<stdio.h> int Fibonacci(int); main() { int n, i = 0, c; scanf("%d",&n); printf("Fibonacci series \n "); for ( c = 1 ; c <= n ; c++ ) { printf("%d \n ", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
6. C program to find the largest element in an array #include<stdio.h> int main(){ int a[50],size,i,big; printf("\nEnter the size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ”, size); for(i=0;i<size;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<size;i++){ if(big<a[i]) big=a[i]; } printf("\nBiggest: %d",big); return 0; }
8.C program to find transpose of given matrix #include<stdio.h> int main(){ int a[10][10],b[10][10],i,j,k=0,m,n; printf("\nEnter the row and column of matrix"); scanf("%d %d",&m,&n); printf("\nEnter the First matrix->"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nThe matrix is\n"); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",a[i][j]); } }
for(i=0;i<m;i++) for(j=0;j<n;j++) b[i][j]=0; for(i=0;i<m;i++){ for(j=0;j<n;j++){ b[i][j]=a[j][i]; printf("\n%d",b[i][j]); } } printf("\n\nTraspose of a matrix is -> "); for(i=0;i<m;i++){ printf("\n"); for(j=0;j<m;j++){ printf("%d\t",b[i][j]); } } return 0; }
Cont…. printf("Enter the matrix 2\n"); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { scanf("%d",&b[i][j]); } } for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { c[i][j]=0; for(k=0;k<r1;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } }
Cont.. printf("Product of matrices\n"); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { printf("%d\t",c[i][j]); } printf("\n"); } } else printf("Matrix multiplication not possible"); printf("\n"); }
k=0,u=n-1; while(k<=u){ mid=(k+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else k=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; }
#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);