Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why the output is wrong?

–2 votes
asked May 7, 2020 by meghna kattekola (130 points)
#include<math.h>
float f(float x)
{
    return 1/(1+x*x);
}
int main()
{
  float a,b,h,x,sum=0.0;    
  int i,n;
  printf("Enter lower limit and upper limit:\n");
  scanf ("%f%f",&a,&b);
  printf("Enter number of intervals:\n");
  scanf("%d",&n);
  h=(b-a)/n;
  for(i=1;i<=n-1;i++)
    {
      x=a+(i*h);
      if(i%2==0)
      sum=sum+2*f(x);
      else
        sum=sum+4*f(x);
    }
  sum=(h/3)*(sum+f(a)+f(b));
  printf("The value of integral:%f\n",sum);
  return 0;
 }                               //????//

4 Answers

0 votes
answered May 7, 2020 by Abhinav Challa (140 points)

add this line at the top of the code

#include<stdio.h> 

0 votes
answered May 7, 2020 by Raj (150 points)
You have asked a wrong question
0 votes
answered May 8, 2020 by Abhishek Nalinisathiamurthi (190 points)

#include<stdio.h> // <--  a small error
#include<math.h>
float f(float x)
{
    return 1/(1+x*x);
}
int main()
{
  float a,b,h,x,sum=0.0;    
  int i,n;
  printf("Enter lower limit and upper limit:\n");
  scanf ("%f%f",&a,&b);
  printf("Enter number of intervals:\n");
  scanf("%d",&n);
  h=(b-a)/n;
  for(i=1;i<=n-1;i++)
    {
      x=a+(i*h);
      if(i%2==0)
      sum=sum+2*f(x);
      else
        sum=sum+4*f(x);
    }
  sum=(h/3)*(sum+f(a)+f(b));
  printf("The value of integral:%f\n",sum);
  return 0;
 }                               //????//

0 votes
answered May 12, 2020 by LiOS (6,420 points)
#include<stdio.h>
#include<math.h>
float f(float x)
{
    return 1/(1+x*x);
}
int main()
{
  float a,b,h,x,sum=0.0;    
  int i,n;
  printf("Enter lower limit and upper limit:\n");
  scanf ("%f%f",&a,&b);
  printf("Enter number of intervals:\n");
  scanf("%d",&n);
  h=(b-a)/n;
  for(i=1;i<=n-1;i++)
    {
      x=a+(i*h);
      if(i%2==0)
      sum=sum+2*f(x);
      else
        sum=sum+4*f(x);
    }
  sum=(h/3)*(sum+f(a)+f(b));
  printf("The value of integral:%f\n",sum);
  return 0;
 }
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...