Functional Programming: Fibonacci, Factorial, Swapping, Power, Primes, Array Max, Reverse/, Lecture notes of C programming

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

2018/2019

Uploaded on 02/28/2019

Tatha_cse
Tatha_cse 🇮🇳

2 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming
using Functions
Developed by,
Tathagata roy chowdhury
Assistant Professor
Dept of computer science and e ngg.
GECC, chaibasa
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Functional Programming: Fibonacci, Factorial, Swapping, Power, Primes, Array Max, Reverse/ and more Lecture notes C programming in PDF only on Docsity!

Programming using Functions Tathagata roy chowdhury^ Developed by, Dept of computer science and engg.^ Assistant Professor GECC, chaibasa

1. Write a program to print

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

  1. Swap two number using call by reference or pointer. #include <stdio.h> void swaping(int *x, int *y); int main() { int n1, n2; printf("Enter first number (n1) : "); scanf("%d", & n1); printf("Enter second number (n2) : "); scanf("%d", & n2); printf("\nBefore swapping values:"); printf(“ n1=%d n2=%d",n1,n2); swaping( & n1, & n2); printf("\nAfter swapping values:"); printf(“ n1=%d n2=%d",n1,n2); getch(); return 0 ; } void swaping(int *x, int *y) { int z; z = *x; *x = *y; *y = z; }
  1. Calculate the power of a number using function. #include<stdio.h> #include<conio.h> long power(int x , int y) { int i; long int k=1; for(i=1;i<=y;i++) k=k*x; return k; } void main() { int x , y ; long int c; c=power(x , y ); printf("Power is: %ld",c); getch(); }

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

  1. Write a program to reverse an array using pointer. #include<stdio.h> #include<conio.h> #define MAX 30 void main() { int size, i, arr[MAX]; int *ptr; clrscr(); ptr = &arr[0]; printf("\nEnter the size of array : "); scanf("%d", &size); printf("\nEnter %d integers into array: ", size);

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

Cont…

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

Cont…

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

10. Program to implement

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

  1. Write a program to reverse a string #include<stdio.h> #include<string.h> int main(){ char str[50]; char *rev; printf("Enter any string : "); scanf("%s",str); rev = strrev(str); printf("Reverse string is : %s",rev); return 0; }
  1. Write a program to concatenate two strings #include <stdio.h> #include <string.h> int main() { char a[ 1000 ], b[ 1000 ]; printf("Enter the first string \n "); gets(a); printf("Enter the second string \n "); gets(b); strcat(a,b); printf("String obtained on concatenation is %s \n ",a); return 0 ; }