---
title: "Sum and average between M and N using pointer"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-sum-average-range"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-sum-average-range
site: BCT Engineering
---

# Sum and average between M and N using pointer

Computer Programming · Chapter 9: Pointers

- Asked in: 2071 Chaitra, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-sum-average-range
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

Write program to calculate sum and average of integer numbers between M and N (where value of M and N are read from keyboard) using pointer.

## Short answer

Read M and N through pointers, loop from *pm to *pn summing values and counting them. If count>0, print sum and average (sum/count as float), else print error. Example: for M=3, N=9, sum=42, average=6.00.

## Answer

**Logic.** M and N are read through pointers (`pm = &m`, `pn = &n`). A loop runs from `*pm` to `*pn`, adding each number and counting how many there are. Average = sum / count, calculated in float. If M is greater than N, the range is empty and a message is shown. M and N are included in the sum.

```c
#include <stdio.h>

int main(void)
{
    int m, n, sum = 0, count = 0;
    int *pm = &m, *pn = &n;               /* pointers to M and N */

    printf("Enter M and N: ");
    scanf("%d %d", pm, pn);

    for (int i = *pm; i <= *pn; i++) {
        sum += i;
        count++;
    }
    if (count > 0)
        printf("Sum = %d, Average = %.2f\n", sum, (float)sum / count);
    else
        printf("M should not be greater than N\n");
    return 0;
}
```

**Sample run** (3 to 9: 3 + 4 + … + 9 = 42 over 7 numbers)

Input: `3 9`

```text
Enter M and N: Sum = 42, Average = 6.00
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How are M and N read using pointers?
   A. scanf("%d %d", pm, pn) where pm = &m and pn = &n
   B. scanf("%d %d", *pm, *pn)
   C. scanf("%d %d", &pm, &pn)
   D. scanf("%d %d", m, n)
   Answer: A. pm and pn already hold the addresses, so they are passed to scanf directly. (Key point: Reading through pointers)

2. For M = 3 and N = 9, what are the sum and count?
   A. Sum 42 over 7 numbers
   B. Sum 33 over 6 numbers
   C. Sum 42 over 6 numbers
   D. Sum 39 over 7 numbers
   Answer: A. 3 + 4 + ... + 9 = 42 and there are 7 numbers because both ends are included. (Key point: Inclusive range)

3. Why is (float)sum / count used?
   A. To get an integer average
   B. To avoid integer division truncating the average
   C. To count the numbers
   D. To make sum float
   Answer: B. Without the cast, integer division would drop the fractional part. (Key point: Average calculation)

4. What does the program do when M is greater than N?
   A. Swaps them silently
   B. Shows a message because the range is empty
   C. Prints sum 0 and average 0
   D. Crashes
   Answer: B. count stays 0, so the else branch prints a message that M should not be greater than N. (Key point: M greater than N)

## Related in Computer Programming

- [Sum and average until 0 is entered](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-average-until-zero) — Computer Programming, chapter 5
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [GOTO types; sum of series program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-goto-types-series) — Computer Programming, chapter 11
- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Algorithm for numbers divisible by 9 not 6](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgo-flowchart-divisible-9-not-6) — Computer Programming, chapter 2
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×

## Related in other subjects

- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [8086 ALP: sum of 100 even numbers](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-sum-even-numbers) — Microprocessor, chapter 3
- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
