Professional Documents
Culture Documents
#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 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:-
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 main()
{
char s[1000];
int length;
return 0;
}
OUTPUT:-
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-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]);
}
}
}
{
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];
OUT PUT:-