I was wondering why the math formula we use to find the sum of the numbers up to the entered number works in javascript but it doesn't work in c ++.
The Formula: (n/2)*(n+1)
Example:
n = 3;
(1+2+3 = 6) = (3/2 * 3+1)
#include <iostream>
using namespace std;
int sumUp(int n){
return (n/2) *(n+1);
}
int main(){
int a;
cout << "Enter the nth number for Sum." << endl;
cin >> a;
cout << sumUp(a) << endl;
system("pause");
}
- My Input --> 3
- My Output --> 4
- Expected: 6
Is there anyone who can help me?