Skip to content
Home » C Program to Find Sum and average in Range using Pointer

C Program to Find Sum and average in Range using Pointer

Learn about C Program to Find Sum and average in Range using Pointer in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to Find Sum and average in Range using Pointer

We will find the Sum and Average (in Range) from m to n here. We can either explicitly initialize m and n as m=30 and n=60, or we can accept m and n values given by the user. First, we’ll compute the sum, followed by the average. The addresses of variables m and n will be stored in variables pm and pn .

#include<stdio.h>
int main()
{
  int m, n, i;
  int *pm, *pn;
  pm= &m, pn= &n;
  double sum=0.0, avg;
  double *psum, *pavg;
  psum= &sum, pavg= &avg;
  printf("Enter range (m and n values)(m<n): ");
  scanf("%d %d", pm, pn);
  for(i=m;i<=n;i++)
  {
    *psum += i;
  }
  *pavg= (*psum) / (*pn - *pm + 1);
  printf("Sum= %.2lf, Average= %.2lf", *psum, *pavg);
  return 0;
}

Output:

Enter range (m and n values)(m<n): 30 60
Sum= 1395.00, Average= 45.00

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Check Odd-Even using Pointer
Largest among three number using pointer & function in C