109k views
0 votes
Write a function that receives two numbers, m and n and calculates and displays the sum of the integers from m to n. For example, if the arguments are m = 3 and n = 7, the function should calculate 3 + 4 + 5 + 6 + 7

1 Answer

4 votes

Answer:

The program to this question as follows:

Program:

//header file

#include <stdio.h> //defining header file

void inc(int m,int n) //defining method inc

{

int sum, i; //declaring variable

sum=m; //holding value of m variable

for(i=++m;i<=n;i++) //loop for calculate number between given range

{

sum=sum+i; //adding value

}

printf("sum of the integer is : %d",sum);//print value

}

int main() //defining method

{

int m,n; //defining integer variable

printf("Enter m value: "); //message

scanf("%d",&m); //input value by user in variable

printf("Enter n value: "); //message

scanf("%d",&n);//input value by user in variable

inc(m,n); //calling method

return 0;

}

Output:

Enter m value: 3

Enter n value: 7

sum of the integer is :25

Explanation:

In the above code, an "inc" function is declared, that accepts integer parameters that are "m and n", inside the method two integer variable "sum and i" is declared, which is used in the loop.

  • The loop uses the user parameter to count value and uses the sum variable to calculate there sum.
  • In the main method, two integer variable n and m are declared, which take value from the user end, and at the last, we call the inc method, that prints sum value.
User Metabolic
by
4.5k points