Question

Write a function that takes an integer input n as a parameter and after it calculates the sum of 1+2+3+...+n, it returns the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in C++:

#include <iostream> using namespace std; /*main function*/ int main() { /*function prototype*/ int calculateSum(int); /*varia/*function definition*/ int calculateSum(int n) { /*variables*/ int i, sum=0; /*using Loop to calculate sum*/ for(i=1;i<=n; i

Output:

C:\Users\Kumar Reddi\Desktop\trail.exe Enter an integer n: 4 The sum is: 10 Process exited after 0.7031 seconds with return v

Code in text format (See above images of code for indentation):

#include <iostream>
using namespace std;
/*main function*/
int main()
{
   /*function prototype*/
   int calculateSum(int);
   /*variables*/
   int n,sum;
   /*read integer n from the user*/
   cout<<"Enter an integer n: ";
   cin>>n;
   /*function call*/
   sum=calculateSum(n);
   /*print sum*/
   cout<<"The sum is: "<<sum;
   return 0;
}
/*function definition*/
int calculateSum(int n)
{
   /*variables*/
   int i,sum=0;
   /*using loop to calculate sum*/
   for(i=1;i<=n;i++)
       sum=sum+i;
   /*return sum*/
   return sum;
}

Add a comment
Know the answer?
Add Answer to:
Write a function that takes an integer input n as a parameter and after it calculates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT