How to add and then multiply


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

	
 int main( void )
	{
	   
	 int n=1, n_max , sum=0; 
	   
	   printf("enter n_max");
	   scanf("%d", &n_max);
	   while ( n <= n_max ) { 
	      printf ( " +%4d",n ); 
	      sum+=n;
		  if(n%5==0)printf("\n");
	      ++n; /* increment */
	    }/* end while */
		printf("= %4d", sum);
	   
	   return 0; 
	
	}



enter n_max5
 +   1 +   2 +   3 +   4 +   5
=   15



My friend and I are just learning c++ and we are trying to modify this program to compute

+ 1*2 + 2*3 + 3*4 + 4*5 + 5 *6
+ 6*7 + ……+ n_max * (n_max +1)
= sum,

Any tips or ideas would be helpful. We have tried a few things but they all have compile errors.



//sorry guys im still learning how to use the tags//
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//#include <stdio.h> This is a C library
#include <iostream>  // THIS is a c++ library

	
 int main() {
	   
	 int n=0;
	 int nmax;
	 int sum=0;
	 int vn;
	 int sn; 
	   
	   //printf("enter nmax");   This is a C command.
	   std::cout<<"Enter n's max value.\n";
	   
	   //scanf("%d", &nmax);         "
	   std::cin>>nmax;
	   nmax=nmax+1;
	   while (n<nmax){  
          sn=n+1;
	      vn=n*sn;
	      sum = sum+vn;
	      n=n+1;
	}
	std::cout<<sum;
 }

Sorry, it is a tad sloppy. I am also new!
Last edited on
Thanks, honestly you opened my eyes. It is so easy now I feel dumb, thank you once again.
Topic archived. No new replies allowed.