Here are your problems:
1 2 3
|
n++;
sum=n*(n+1)/2;
n--;
| |
I'm not sure what are you doing here, but you're doing it in a for loop, so sum gets recalculated every time. To find the sum of numbers in range [num1; num2] you could either use for loop
for(n = num1; n <= num2; n++) sum+=n;
or simply use a formula
sum = (num2 + num1)*(num2 - num1)/2;
or something like that...
Also
avg=sum/num2;
is not a way to get the average. You could either use
avg = sum/(num2-num1);
or
avg = (num1+num2)/2;
.
The other flaws:
You are not using function sum(int num1), so why did you declare it?
n!=0 on line 11 is put where declarations should be, it never evaluated and in generally not needed here.
n++ and n-- on lines 13, 15 are odd. You know you could have written sum=(n+1)*(n+2)/2 instead, and the result would have been the same...
Lastly, note that your code and the code that I posted is only going to work if num2 > num1. You may want to swap them otherwise.