Sunday, July 30, 2017

Write a function named "enough" that takes one integer argument, call it "goal" and returns as its value the smallest positive integer n for which 1+2+3+. . . +n is at least equal to goal .

/*
For more exercises and lessons, visit http://shegertech.blogspot.com/
*/

#include <iostream>
#include <cstdlib>
using namespace std;

int enough (int goal)
{
int n = 1, sum = 1;
while (sum < goal)
sum += ++n;
return n;
}

int main(){
int n2=5;
cout<<enough(n2)<<endl;
system("pause");
return EXIT_SUCCESS;
}

Share: