/* Program to write a function to find minimum value */
#include<stdio.h> /* Main Function */ void main() { double x,y,mm; double min(double,double); /* Function Prototype Function Declaration*/ printf("Enter the value of x:"); scanf("%lf",&x); printf("Enter the value of y:"); scanf("%lf",&y); mm=min(x,y); /* Calling the function min(a,b) */ printf("The minimum of %.4lf and %.4lf is %.4lf\n",x,y,mm); } /* Function to return the minimum of two numbers */ double min(double a,double b) { if(a<b) return(a); else return(b); }
receg./* Program to find Factorial of Given No: using Recursive Function */
#include <stdio.h> /*Function Declaration with one argument and with return value */ long factorial(long); /* Main Function */ void main() { long number=0,f; printf("\nEnter an integer value: "); scanf("%ld",&number); f=factorial(number); /* Calling Function */ printf("\nThe Factorial of %ld is %ld\n",number,f); } /* Recursive factorial function */ long factorial(long N) { if(N<2) /* Terminating Condition */ return N; else return N*factorial(N-1); /* Recursive call */ }
Functions
1./*To find the sum of individual digits of a no.*/
int sum(int); #include<stdio.h> void main() { int n,res; printf("Enter a no."); scanf("%d",&n); res=sum(n); printf("\nSum of individual digits = %d\n",res); } int sum(int N) { int sumn=0,r; while(N>0) { r=N%10; sumn=sumn+r; N=N/10; } return(sumn); }
3./*To count how many times a function is called*/
int count(void); #include<stdio.h> void main() { int ct; ct=count(); ct=count(); ct=count(); ct=count(); printf("The function is called %d times\n",ct); } int count(void) { static int c=0; c++; return(c); }
4./*To find smallest of 3 nos. and to do the given*/
float min(float,float,float); #include<stdio.h> void main() { float m1,m2,m3,mini,avg; printf("Enter 3 test marks"); scanf("%f %f %f",&m1,&m2,&m3); mini=min(m1,m2,m3); avg=(m1+m2+m3-mini)/2; printf("Average of best 2 test marks out of 3 test marks = %.2f\n",avg); } float min(float a,float b,float c) { float small=a; if(b<small) small=b; if(c<small) small=c; return(small); }
5./*To apply linear search to a set of n nos.*/
void search(int a[20],int,int); #include<stdio.h> void main() { int a[20],n,i,x,pos; printf("Enter no. of nos."); scanf("%d",&n); printf("Enter array elements"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter element to be searched"); scanf("%d",&x); search(a,n,x); } void search(int a[20],int n,int x) { int i,ct=0; for(i=0;i<n;i++) { if(a[i]==x) ct++; } if(ct>0) { printf("Element appears in the array %d times",ct); for(i=0;i<n;i++) { if(a[i]==x) printf("\nPosition of array = %d",i+1); } printf("\n"); } else printf("Element does not appear in the array\n"); }
6./*To obtain trace of a matrix*/
int trace(int a[30][30],int,int); #include<stdio.h> void main() { int a[30][30],r,c,i,j,sum; printf("Enter the no. of rows and columns"); scanf("%d %d",&r,&c); if(r==c) { printf("Possible to find trace"); printf("\nEnter the matrix elements"); for(i=0;i<r;i++) { for(j=0;j<c;j++) scanf("%d",&a[i][j]); } printf("Given matrix\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } sum=trace(a,r,c); printf("\nTrace = %d\n",sum); } else printf("Not possible to find trace\n"); } int trace(int a[30][30],int r,int c) { int sum=0,i; for(i=0;i<r;i++) sum=sum+a[i][i]; return(sum); }
7.*To arrange a set of nos. depending on a parameter I*/
int fact(int); #include<stdio.h> void main() { int n,r; float res; printf("Enter n value"); scanf("%d",&n); printf("Enter r value"); scanf("%d",&r); if(r>n) printf("Not possible to eavluate nCr"); else { res=(float)fact(n)/(fact(r)*fact(n-r)); printf("\nnCr = %.2f\n",res); } } int fact(int n) { if(n==1 || n==0) return(1); else return(n*fact(n-1)); }
2./*To display the first n terms of fibonacci series*/
void fibo(int); #include<stdio.h> void main() { int n; printf("Enter the no. of terms"); scanf("%d",&n); printf("FIBONACCI SERIES\n"); fibo(n); } void fibo(int n) { static int a=0,b=1,sum=0,ct=1; printf("%d\t",sum); a=b; b=sum; sum=a+b; ct++; if(ct<=n) fibo(n); }
3./*To reverse an integer*/
int reverse(int); #include<stdio.h> void main() { int n,rev; printf("Enter a no."); scanf("%d",&n); rev=reverse(n); printf("Reversed no. = %d\n",rev); } int reverse(int n) { static int sum=0; int r; r=n%10; sum=sum*10+r; n=n/10; if(n>0) reverse(n); return(sum); } 4./*To depict Towers of Hanoi problem*/ #include <stdio.h> void towerofHanoi(int,char,char,char); void main() { int n; // Number of disks printf("Enter no. of disks"); scanf("%d",&n); towerofHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods printf("\n"); } void towerofHanoi(int n,char from_rod,char to_rod,char aux_rod) { if(n==1) { printf("\nMove disk 1 from rod %c to rod %c",from_rod,to_rod); return; } towerofHanoi(n-1,from_rod,to_rod,aux_rod); printf("\nMove disk %d from rod %c to rod %c",n,from_rod,to_rod); towerofHanoi(n-1,aux_rod,to_rod,from_rod); }
5./*To find HCF of 2 nos.*/
int HCF(int,int); #include<stdio.h> void main() { int n1,n2,hcf; printf("Enter 2 nos."); scanf("%d %d",&n1,&n2); hcf=HCF(n1,n2); printf("HCF of %d and %d = %d\n",n1,n2,hcf); } int HCF(int a,int b) { static int nr,dr,r; if(a>b) { nr=a; dr=b; } else { nr=b; dr=a; } r=nr%dr; nr=dr; dr=r; if(dr>0) HCF(nr,dr); return(nr); }