# Thread: This sounds easy but have never done it

1. ## This sounds easy but have never done it

I need to combine these programs into one program that works for a positive integer "n"
The first is a sum of squares starting with 1 until "n"
The second is a sum of cubes starting with 1 until "n"
The third is a sum of fractions starting with 1/2^1 until 1/2^n
I know I need to take out my exit stuff until the last one and all I need is one set of preprocessor directives.
Code:
```#include <stdio.h>
#include <math.h>

main ()
{
/* Declare variables */
int i, n, sumsquare = 0;;
printf("Enter n: \n");
scanf("%d", & n);
/* Set for loop conditions */
for (i=1; i <= n; i++)
{
sumsquare += i*i;
}
printf ("The sum of the squares from 1..%d is %d\n", n,
sumsquare);
system("pause");
exit(0);

}
{
/* Declare and initialize variables */
int i=0, n, sumcubes=0;

printf("Enter n: \n");
scanf("%i", &n);

/* Set for loop conditions */
while (i++<n)
{
sumcubes += (i*i*i);
}
printf ("The sum of the cubes from 1..%d is %d\n", n,
sumcubes);
system("pause");
exit(0);

}
{
unsigned int i = 1, n;
float sum=0;

printf("Enter n: \n");
scanf("%u", &n);

do
{
sum += (1/(pow(2,i)));
}
while (i++ < n);

printf ("The sum of the fractional exponentials from 1 ... %u is %f\n",
n, sum);
system ("pause");
return 0;
}```

2. Press edit, and fix your tags. Then, write functins.
Code:
```int dosomething( int n )
{
...do stuff...
...return something...
}
... repeat for all functions ...

int main( void )
{
...prompt for n...

...call all of your functions, passing them n...
...store whatever they return, and print it...

return 0;
}```
Quzah.

3. This is the best I can do right now & the compiler highlights the bracket of the second program. This is actually more interesting than I thought. Thank you quzah.
I am going to apologize ahead of time for the lengthy code
Code:
``` main ()
{
/* Declare variables */
int i, n, sumsquare = 0;;
printf("Enter n: \n");
scanf("%d", & n);
/* Set for loop conditions */
for (i=1; i <= n; i++)
{
sumsquare += i*i;
}
printf ("The sum of the squares from 1..%d is %d\n", n,
sumsquare);
system("pause");

}
int main ()
{
/* Declare and initialize variables */
int i=0, n, sumcubes=0;

printf("Enter n: \n");
scanf("%i", &n);

/* Set for loop conditions */
while (i++<n)
{
sumcubes += (i*i*i);
}
printf ("The sum of the cubes from 1..%d is %d\n", n,
sumcubes);
system("pause");

}
int main ()
{
unsigned int i = 1, n;
float sum=0;

printf("Enter n: \n");
scanf("%u", &n);

do
{
sum += (1/(pow(2,i)));
}
while (i++ < n);

printf ("The sum of the fractional exponentials from 1 ... %u is %f\n",
n, sum);
system ("pause");
return 0;
}```

4. ## It's got a lot of compiling beefs with me

There are so many I am reluctant to list them.
I have eliminated main () from the others and just put one on the top or do I need to put main () above every new program?
Code:
```20 syntax error before '{' token
24 syntax error before string constant
24 conflicting types for 'printf'
24 a parameter list with an ellipsis can't match an empty parameter name list declaration
24 conflicting types for 'printf'
24 a parameter list with an ellipsis can't match an empty parameter name list declaration
24 [Warning] data definition has no type or storage class
25 syntax error before string constant
25 conflicting types for 'scanf'
25 a parameter list with an ellipsis can't match an empty parameter name list declaration
25 conflicting types for 'scanf'
25 a parameter list with an ellipsis can't match an empty parameter name list declaration
25 [Warning] data definition has no type or storage class
32 syntax error before string constant
33 [Warning] data definition has no type or storage class
34 syntax error before string constant
34 [Warning] data definition has no type or storage class
41 syntax error before string constant
41 [Warning] data definition has no type or storage class
42 syntax error before string constant
42 [Warning] data definition has no type or storage class
50 syntax error before string constant
51 [Warning] data definition has no type or storage class
52 syntax error before string constant
52 [Warning] data definition has no type or storage class```

5. The main function is only supposed to be defined once, and you've written it three times. My suggestion is to rewrite your program anew from Quzah's outline.

And this has been bugging me; it's int main(void) .

6. ## Thanks you all

I straightened it out and I appreciate you all's comments.
I want to say more but it's just stupid sentiment.
Thanks I have learned a lot.

7. > main ()
Instead of naming this main, call this something like:
Code:
```int calc_sum_squares(int n)
{
.
.
return sumsquare;
}```
>int main ()
And name this next one:
Code:
```int calc_sum_cubes(int n)
{
.
.
return sumcubes;
}```
Then in main you would have:
Code:
```    printf("Enter n: \n");
scanf("%u", &n);

printf ("The sum of the squares from 1..%d is %d\n", n,
calc_sum_squares(n));

printf ("The sum of the cubes from 1..%d is %d\n", n,
calc_sum_cubes(n));```

8. Why did you send this to me as a private message? You have already had the issue solved by quaza.

If you have a problem you send as a private message, please make sure it is unsolved in the future, then people will be able to help you better

9. ## Sorry

I am new to the forum and I was hoping for a new insight.
I did not mean to bother you.
I will not abuse the ettiquete of the forum, but please understand I am learning.