User Define Function Programs
| Program using User Define Function to find sum of three numbers. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,c,s; //Function Declaration int sum(int a,int b,int c); clrscr(); printf("Enter value of a:"); scanf("%d",&a); printf("Enter value of b:"); scanf("%d",&b); printf("Enter value of c:"); scanf("%d",&c); s=sum(a,b,c); //Function Call printf("Sum=%d",s); getch(); } //Function Definition int sum(int a, int b, int c) { return (a+b+c); } Output Enter value of a: 2 Enter value of b: 5 Enter value of c: 3 Sum = 10 |
||||||
| Program using User Define Function to find average of three numbers. | ||||||
| #include<stdio.h> #include<conio.h> void main() { float a,b,c; float ag; //Function Declaration float avg(float a,float b,float c); clrscr(); printf("Enter value of a:"); scanf("%f",&a); printf("Enter value of b:"); scanf("%f",&b); printf("Enter value of c:"); scanf("%f",&c); //Function Call ag=avg(a,b,c); printf("Average=%f",ag); getch(); } //Function Definition float avg(float a, float b, float c) { return (a+b+c)/3; } Output Enter value of a: 2 Enter value of b: 5 Enter value of c: 3 Average = 3.333333 |
||||||
| Program using User Define Function to find maximum from two numbers. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,m; //Function Declaration int max(int a,int b); clrscr(); printf("Enter value of a:"); scanf("%d",&a); printf("Enter value of b:"); scanf("%d",&b); //Function Call m=max(a,b); printf("max=%d",m); getch(); } //Function Definition int max(int a,int b) { if (a>b) return a; else return b; } Output Enter value of a: 35 Enter value of b: 45 max = 45 |
||||||
| Program using User Define Function to find maximum from three numbers. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,c,m; //Function Declaration int min(int a,int b,int c); clrscr(); printf("Enter value of a:"); scanf("%d",&a); printf("Enter value of b:"); scanf("%d",&b); printf("Enter value of c:"); scanf("%d",&c); //Function Call m=min(a,b,c); printf("Minimum=%d",m); getch(); } //Function Definition int min(int a,int b,int c) { if ((a<,b)&&(a<,c)) return a; else if((b<,a)&&(b<,c)) return b; else return c; } Output Enter value of a: 2 Enter value of b: 5 Enter value of c: 3 Minimum:2 |
||||||
| Program using User Define Function to find factorial of given number (Use Recusrsion). | ||||||
| #include<stdio.h> #include<conio.h> void main() { int n,f; //Function Declaration int factorial(int n); clrscr(); printf("Enter value of n:"); scanf("%d",&n); //Function Call f=factorial(n); printf("Factorial=%d",f); getch(); } //Function Definition int factorial(int n) { int fact; if (n==1) return 1; else fact=n*factorial(n-1); return fact; } Output Enter value of n: 4 Factorial=24 |
||||||
| Program using User Define Function to find power of given number. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int x,n,p; //Function Declaration int power(int x,int n); clrscr(); printf("Enter value of x:"); scanf("%d",&x); printf("Enter value of n:"); scanf("%d",&n); //Function Call p=power(x,n); printf("Power=%d",p); getch(); } //Function Definition int power(int x,int n) { int p=1,i; for(i=1;i<=n;i++) p=p*x; return p; } Output Enter Value of x:2 Enter Value of n:3 Power=8 |
||||||