Uploaded by skumarpatel025

Practical 22

advertisement
Practical – 22
Design and test at least 5 C programs using user defined functions.
(A) Write a C program using User Define Function that find the Maximum
and Minimum number from given three number. (Construct two separate
functions for Maximum and Minimum)
#include<stdio.h>
#include<conio.h>
int maximum(int,int,int);
int minimum(int,int,int);
void main()
{
int x,y,z,maxno,minno;
clrscr();
printf("Enter the value of x=");
scanf("%d",&x);
printf("Enter the value of y=");
scanf("%d",&y);
printf("Enter the value of z=");
scanf("%d",&z);
maxno=maximum(x,y,z);
minno=minimum(x,y,x);
printf("Maximum number is %d\n",maxno);
printf("Minimum Number is %d\n",minno);
getch();
}
int maximum(int a,int b,int c)
{
if(a>b && a>c)
{
return a;
}
else if(b>a && b>c)
{
return b;
}
else
{
return c;
}
}
int minimum(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 the value of x=10
Enter the value of y=20
Enter the value of z=15
Maximum number is 20
Minimum Number is 10
(B)Write a C program using User Define Function that Calculate Factorial
of given number without the use of recursion.
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n,ans;
clrscr();
printf("Enter number = ");
scanf("%d",&n);
ans=factorial(n);
printf("Factorial value of %d is %d",n,ans);
getch();
}
int factorial(int n)
{
int fact=1,i;
for(i=n;i>=1;i--)
{
fact=fact*i;
}
return fact;
}
Output
Enter number = 6
Factorial value of 6 is 720
(C) Write a C program using User Define Function that Check if a number
is Prime or not.
#include<stdio.h>
#include<conio.h>
void checkprime(int );
void main()
{
int n;
clrscr();
printf("Enter n = ");
scanf("%d",&n);
checkprime(n);
getch();
}
void checkprime(int n)
{
int i,flag=0;
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d is Prime Number",n);
}
else
{
printf("%d is non prime number",n);
}
}
Output
Enter n = 58
58 is non prime number
(D) Write a C program using User Define Function that Calculate the
Fibonacci Series based on given input.
#include<stdio.h>
#include<conio.h>
void printfibonacci(int );
void main()
{
int n;
clrscr();
printf("Enter a number to print Fibonacci series = ");
scanf("%d",&n);
printfibonacci(n);
getch();
}
void printfibonacci(int n)
{
int a=0,b=1,c=1;
while(c<=n)
{
printf("%d\t",c);
c=a+b;
a=b;
b=c;
}
}
Output
Enter a number to print Fibonacci series = 50
1
1
2
3
5
8
13
21
34
(E) Write a C program with User Define Function that accepts an array
and its size as parameters. Inside the function, each element of the array is
incremented by 5 and display the array with new incremented values in
main () function.
1) Perform given task with call by value strategy.
2) Perform same task with call by reference strategy.
#include <stdio.h>
void incrementArrayByValue(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
{
arr[i] += 5;
}
}
void incrementArrayByReference(int *arr, int size)
{
int i;
for(i = 0; i < size; i++)
{
(*arr) += 5;
arr++; // Move to the next element in the array
}
}
void main()
{
int i;
int arr1[5]; // Declare an array of the given size
int arr2[5];
clrscr();
printf("Enter elements for the array:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr1[i]);
arr2[i] = arr1[i]; // Create a copy of the array for call by reference
}
incrementArrayByValue(arr1, 5);
printf("Array after incrementing by value:\n");
for (i = 0; i < 5; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
incrementArrayByReference(arr2, 5);
printf("Array after incrementing by reference:\n");
for (i = 0; i < 5; i++)
{
printf("%d ", arr2[i]);
}
printf("\n");
getch();
}
Output
Enter elements for the array:
4
5
6
7
8
Array after incrementing by value:
9 10 11 12 13
Array after incrementing by reference:
9 10 11 12 13
Download