Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

23104B0007 PRABHU KRISHNA EXTC B

ASSIGNMENT 4
Q.1 Develop user defined function to find permutation and combination of two
numbers

#include <stdio.h>
int factorial(int);
int permutation(int, int);
int combination(int, int);
void main()
{
int n, r;
printf("Enter the value of n and then of r):
");
scanf("%d %d", &n, &r);
int nPr = permutation(n, r);
int nCr = combination(n, r);
printf("Permutation (nPr) = %d\n", nPr);
printf("Combination (nCr) = %d\n", nCr);
}
int factorial(int n)
{
if (n <= 1)
{
return 1;
}
return n * factorial(n - 1);
}
int permutation(int n, int r)
{
if (n < r)
{
return 0;
}
return factorial(n) / (factorial(n - r));
}
int combination(int n, int r)
{
if (n < r)
{
return 0;
}
return factorial(n) / (factorial(r) *
factorial(n - r));
}
Q.2 What is parameter passing in user defined functions? Why
parameter passing is required? What are different types of
parameter passing? (5 marks)
It is a mechanism in which data is transferred from the calling code to
the function. This parameters allow you to pass information to a
function so that the function can perform specific tasks with that
data.

why it is required
Functions can easily access and manipulate data that is outside their
scope by receiving it as parameters.
It makes functions reusable.
They allow data sharing between different parts of a program.
Functions can hide the details of their mechanism and expose a
simpler interface to the caller, enhancing code readability and
maintainability.
Different Types of Parameter Passing:
Pass by Value
Pass by Reference
Pass by Pointer
Pass by Name
Pass by Keyword
Q.4 Explain the different storage classes in C programming (Auto, Extern, Static &
Register)

1.Auto (auto):
Variables declared as auto are created inside a function and are, by
default, local to that function.
They have automatic storage duration, which means they are created
when the function is called and destroyed when the function exits.
Default value garbage
2.Extern (extern):
Variables declared as extern are typically used to declare global
variables.
They have a global scope and can be accessed across multiple source
files.
The variable is allocated storage when it's defined (usually in another
source file) and can be used anywhere in the program.
Default value zero
3.Static (static):
For local variables within functions, they retain their values between
function calls.
Static variables are initialized only once, and their values persist
across function calls.
Default value zero
4.Register (register):
The register keyword is a hint to the compiler to store the variable in
a CPU register for faster access.
It's an optimization hint and may or may not be honored by the
compiler. Default value garbage
Q.3 Develop user defined function to generate factorial of a given number
using recursion.
#include<stdio.h>
long factorial(int);
void main ()
{
int num, result;
printf("enter any number: ");
scanf("%d",&num);
result=factorial(num);
printf("\nthe factorial of given number is:
%d",result);
}
long factorial(int x)
{
if (x == 0 || x == 1)
{
return 1;
} else
{
return x * factorial(x - 1);
}
}

You might also like