You are on page 1of 8

WEEK-11

AIM:- Write a C function to calculate NCR value.


DESCRIPTION:-
nCr, is a mathematical notation representing the number of ways to choose r
items from a set of n items, where order does not matter and repetitions are not
allowed.
It is also known as the binomial coeffiecient and is represented
by the formula: nCr = n! / (r! * (n - r)!).
Where n! Represents the factorial of n, or the product of all positive integers up to n.
In this program the function calculates the factorial of a given integer n, using
a for loop that iterates from 1 o n, multiplying the variable "fact" by i at each
iteration.
ALGORITHAM:-
SETP-1. START
STEP-2. FUNCTION factorial(n)
SET fact = 1
FOR i = 1 to n
SET fact = fact * i
RETURN fact
STEP-3. FUNCTION nCr(n, r)
SET nCr = factorial(n) / (factorial(r) * factorial(n - r))
RETURN nCr
STEP-4. FUNCTION main()
DECLARE n, r, result AS INTEGER
PRINT "Enter the value of n : "
READ n FROM USER
PRINT "Enter the value of r : "
READ r FROM USER
SET result = nCr(n, r)
PRINT "nCr = ", result
RETURN 0
STEP-5. END
C-PROGRAM :

#include <stdio.h>

int factorial(int n)
{
if(n == 0)
return 1;
int factorial = 1;
for (int i = 2; i <= n; i++)
factorial = factorial * i;
return factorial;
}

int nCr(int n, int r)


{
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main()
{
int n ,r;
printf("\n Enter a Value for n=");
scanf("%d",&n);
printf("\n Enter a Value for r=");
scanf("%d",&r);
printf("nCr=%d", nCr(n, r));
}

OUTPUT:-

Enter a Value for n=6


Enter a Value for r=3
nCr=20
Write a C function to find the length of a string.

AIM:- Write a C function to find the length of a string (without using library functions)

DESCRIPTION:-
This program is designed to determine the length of a string entered by the user. It utilizes a
recursive function to traverse through the characters of the input string until it encounters the null
terminator, denoting the string's end.
The recursive function incrementally counts the characters, and upon reaching the null
terminator, it returns the count, signifying the length of the string.
The main function prompts the user to input a string, invokes the recursive function to
compute its length, and then displays the length of the entered string.
Through this process, the program showcases a recursive approach to ascertain the length of
a string in a step-by-step manner.

ALGORITHAM :-
STEP- 1 START
STEP-2 : Function stringLength(s: array of characters, i: integer) -> integer:
 Take a string s and an index i as parameters.
 If the character at index i in the string s is the null terminator '\0':
 Return i (current length of the string).
 Else:
 Recursively call stringLength with parameters s and i + 1.
STEP-3: Function main():
 Declare variables:
 s: array of characters of size 1000.
 length: integer.
 Output "Enter any string: ".
 Take input into s.
 Call stringLength(s, 0) to calculate the length of the entered string.
 Output "Length of '", s, "' = ", length.
STEP-4: STOP
C-PROGRAM:

int stringlength(char *s,int i)


{
if(s[i]=='\0')
return i;
stringlength(s,++i);

int main()
{
char s[1000];
int length;

printf("Enter any string: ");


gets(s);
length=stringlength(s,0);
printf("Length of '%s'= %d",s,length);

return 0;
}
OUTPUT:-

Enter any string: PBR VITS KAVALI


Length of ' PBR VITS KAVALI’ =15
Transpose of a matrix.

AIM:- Write a C function to transpose of a matrix.

Description :-
The transpose of a matrix involves flipping its rows with columns.
In a matrix M with dimensions m×n, the transpose MT is obtained by swapping
rows with columns resulting in a matrix of dimensions n×m.
The provided C program reads a matrix, displays it, and computes its
transpose, demonstrating the fundamental operation of transposing a matrix
efficiently within the defined size limit.
Using functions and nested loops, it iterates through the matrix elements
to calculate and display the transposed matrix.
ALGORITHAM :-
STEP-1 START
STEP-2 : Define MAX_SIZE as the maximum size for the matrix
MAX_SIZE = 10

STEP-3 // Declare global variables i and j

STEP-4: // Function to read a matrix


FUNCTION read(matrix[MAX_SIZE][MAX_SIZE], rows, cols):
FOR i = 0 TO rows:
FOR j = 0 TO cols:
READ matrix[i][j]

STEP-5: // Function to display a matrix


FUNCTION display(matrix[MAX_SIZE][MAX_SIZE], rows, cols):
FOR i = 0 TO rows:
FOR j = 0 TO cols:
PRINT matrix[i][j]

STEP-6 : // Function to calculate transpose of a matrix


FUNCTION transpose(matrix[MAX_SIZE][MAX_SIZE], trans[MAX_SIZE][MAX_SIZE], rows, cols):
FOR i = 0 TO rows:
FOR j = 0 TO cols:
trans[j][i] = matrix[i][j]

STEP-7: // Main function


FUNCTION main():
DECLARE rows, cols as integers
DECLARE matrix[MAX_SIZE][MAX_SIZE] and tmatrix[MAX_SIZE][MAX_SIZE]
READ rows, cols

STEP:-8 : // Read matrix elements


CALL read(matrix, rows, cols)
STEP-9: // Display original matrix
CALL display(matrix, rows, cols)

STEP-10: // Calculate and display transpose matrix


CALL transpose(matrix, tmatrix, rows, cols)
CALL display(tmatrix, cols, rows)

STEP-11 END.
C-PROGRAM:-
#include <stdio.h>
int i,j;
#define MAX_SIZE 10 // Maximum size for the matrix
// Function to read a matrix
void read(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols)
{
printf("Enter the elements of the matrix:\n");
for ( i = 0; i < rows; i++)
{
for ( j = 0; j < cols; j++)
{
printf("\n enter element of A[%d][%d] =",i,j);
scanf("%d", &mat[i][j]);
}
}
}

// Function to display a matrix


void display(int mat[MAX_SIZE][MAX_SIZE], int rows, int cols)
{
printf("The matrix is:\n");
for ( i = 0; i < rows; i++)
{
for ( j = 0; j < cols; j++)
{
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}

// Function to calculate transpose of a matrix


void transpose(int mat[MAX_SIZE][MAX_SIZE], int trans[MAX_SIZE][MAX_SIZE], int rows, int cols)
{
for ( i = 0; i < rows; i++)

{
for ( j = 0; j < cols; j++)
{
trans[j][i] = mat[i][j];
}
}
}
int main()
{
int rows, cols;
int matrix[MAX_SIZE][MAX_SIZE];
int tmatrix[MAX_SIZE][MAX_SIZE];

printf("\nEnter the number of rows of the matrix: ");


scanf("%d", &rows);
fflush(stdin);
printf("\nEnter the number of columns of the matrix: ");
scanf("%d", &cols);

if (rows > MAX_SIZE || cols > MAX_SIZE)


{
printf("Matrix size exceeds the limit. Please enter a smaller matrix size.\n");
return 1;
}
read(matrix, rows, cols);
printf("\n");
display(matrix, rows, cols);

transpose(matrix, tmatrix, rows, cols);

printf("\nTranspose of the matrix is:\n");


display(tmatrix, cols, rows);
return 0;
}

OUT PUT:-

You might also like