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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.