C# Program to Find the Value of 1/(1+x2) using Simpson?s 1/3 Rule
Posted by Superadmin on August 15 2022 11:33:27

C# Program to Find the Value of 1/(1+x2) using Simpson’s 1/3 Rule

 

 

This C# Program Finds Value of 1/(1+x2) using Simpsons 1/3 Rule. Here Simpson’s 1/3 Rule Numerical Integration is used to estimate the value of a definite integral. It works by creating an even number of intervals and fitting a parabola in each pair of intervals.

 

Here is source code of the C# Program to Find Value of 1/(1+x2) using Simpsons 1/3 Rule. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Find Value of 1/(1+x2) using Simpsons 1/3 Rule
  3.  */
  4. using System;
  5.  
  6. class simpson
  7. {
  8.     float a, b;
  9.     int n;
  10.     public void readdata()
  11.     {
  12.         Console.WriteLine("Enter the Lower Limit Value : ");
  13.         a = Convert.ToSingle(Console.ReadLine());
  14.         Console.WriteLine("Enter the Upper Limit Value : ");
  15.         b = Convert.ToSingle(Console.ReadLine());
  16.         Console.WriteLine("Enter the Number of Intervals : ");
  17.         n = Convert.ToInt32(Console.ReadLine());
  18.     }
  19.     public void simp()
  20.     {
  21.         int i;
  22.         float x, sum = 0.0f, h;
  23.         float[] y = new float[n + 1];
  24.         h = (b - a) / n;
  25.         x = a;
  26.         for (i = 0; i <= n; i++)
  27.         {
  28.             y[i] = 1.0f / (1 + x * x);
  29.             x = x + h;
  30.         }
  31.         sum = y[0] + y[n];
  32.         for (i = 1; i < n - 1; i += 2)
  33.         {
  34.             sum += 4 * y[i]+2* y[i + 1];
  35.  
  36.         }
  37.         sum = sum * h / 3.0f;
  38.         Console.WriteLine("Integral Value : {0} ", sum);
  39.         Console.ReadLine();
  40.     }
  41.     public static void Main()
  42.     {
  43.         simpson obj = new simpson();
  44.         obj.readdata();
  45.         obj.simp();
  46.     }
  47. }

Here is the output of the C# Program:

Enter the Lower Limit Value : 1
Enter the Upper Limit Value : 10
Enter the Number of Intervals : 100
Integral Value : 0.6845199