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; }



LinkBack URL
About LinkBacks


