0% found this document useful (0 votes)
63 views

Task 11

The document contains 4 sections that describe C programs for different tasks: 1) A function to calculate binomial coefficients (NCR value) using factorials. 2) A function to find the length of a string by iterating through the string. 3) A function to find the transpose of a matrix by swapping rows and columns. 4) A program that uses Euler's method to numerically solve ordinary differential equations by approximating the solution curve with line segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Task 11

The document contains 4 sections that describe C programs for different tasks: 1) A function to calculate binomial coefficients (NCR value) using factorials. 2) A function to find the length of a string by iterating through the string. 3) A function to find the transpose of a matrix by swapping rows and columns. 4) A program that uses Euler's method to numerically solve ordinary differential equations by approximating the solution curve with line segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

11.i Aim: Write a C function to calculate NCR value.

Description:
Following are the common definitions of Binomial Coefficients.

A binomial coefficient C(n, k) can be defined as the coefficient of Xk in the expansion of (1 +


X)n.
A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k
objects can be chosen from among n objects; more formally, the number of k-element subsets
(or k-combinations) of an n-element set.
Given two numbers N and r, The task is to find the value of NCr
Input: N = 5, r = 2
Output: 10
Explanation: The value of 5C2 is 10
Program:
#include<stdio.h>
int factorial(int n)
{
int i;
if(n == 0)
return 1;
int factorial = 1;
for(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 = 5, r = 3;
printf("%d", nCr(n, r));
return 0;
}
Output:
10
11.ii Aim: Write a C function to find the length of a string.
Description:

Program:
#include<stdio.h>
/* Function Prototype */
int mystrlen(char str[30]);
/* Main Function */
int main()
{
char str[30];
int i, len;
printf("Enter string:\n");
gets(str);
len = mystrlen(str); /* Function Call */
printf("Length of given string is: %d", len);
return 0;
}
/* Function Definition */
int mystrlen(char str[30])
{
int i, len=0;
for(i=0;str[i]!='\0';i++)
{
len++;
}
return(len);
}
Output :
Enter string:
Welcome to C
Length of given string is: 12

11.iii Aim: Write a C function to transpose of a matrix.


Description:
Given a matrix of size N X M, find the transpose of the matrix. Transpose of a matrix is
obtained by changing rows to columns and columns to rows. In other words, transpose of
A[N][M] is obtained by changing A[i][j] to A[j][i].
Program:
// C program to find
// transpose of a matrix

#include <stdio.h>
#define N 4

// This function stores transpose of A[][] in B[][]


void transpose(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
B[i][j] = A[j][i];
}
// Driver code
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };

// Note dimensions of B[][]


int B[N][N], i, j;

// Function call
transpose(A, B);

printf("Result matrix is \n");


for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}

return 0;
}
Output:
Result matrix is
1234
1234
1234
1234
11.iv Aim: Write a C function to demonstrate numerical integration of differential equations
using Euler’s method

Description:
Solving an ordinary differential equation (ODE) or initial value problem means finding a
clear expression for y in terms of a finite number of elementary functions of x. Euler’s
method is one of the simplest methods for the numerical solution of such an equation or
problem. This C program for Euler’s method considers ordinary differential equations, and
the initial values of x and y are known.
Mathematically, here, the curve of the solution is approximated by a sequence of short lines
i.e. by the tangent line in each interval. (Derivation) Using this information, the value of the
value of ‘yn’ corresponding to the value of ‘xn‘ is to determined by dividing the length (xn –
x) into n strips.
Therefore, strip width= (xn – x)/n and xn=x0+ nh.
Again, if m be the slope of the curve at point, y1= y0 + m(x0 , yo)h.
Similarly, values of all the intermediate y can be found out.
Below is a source code for Euler’s method in C to solve the ordinary differential equation
dy/dx = x+y. It asks for the value of of x0 , y0 ,xn and h. The value of slope at different
points is calculated using the function ‘fun’.
The values of y are calculated in while loop which runs till the initial value of x is not equal
to the final value. All the values of ‘y’ at corresponding ‘x’ are shown in the output screen.
dy/dx = x+y
Program:
#include<stdio.h>
float fun(float x,float y)
{
float f;
f=x+y;
return f;
}
main()
{
float a,b,x,y,h,t,k;
printf("\nEnter x0,y0,h,xn: ");
scanf("%f%f%f%f",&a,&b,&h,&t);
x=a;
y=b;
printf("\n x\t y\n");
while(x<=t)
{
k=h*fun(x,y);
y=y+k;
x=x+h;
printf("%0.3f\t%0.3f\n",x,y);
}
}

Output:

Enter x0,y0,h,xn: 0
1
0.1
1

x y
0.100 1.100
0.200 1.220
0.300 1.362
0.400 1.528
0.500 1.721
0.600 1.943
0.700 2.197
0.800 2.487
0.900 2.816
1.000 3.187

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy