Task 11
Task 11
Description:
Following are the common definitions of Binomial Coefficients.
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
#include <stdio.h>
#define N 4
// Function call
transpose(A, B);
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