Anonymous

how can you write a c program to calculate the sum and product of five numbers?

1

1 Answers

Ray Dart Profile
Ray Dart answered

Note; This is strictly "C" - it would/should be done differently (and better) in C++ (scanf and printf suck, for a start!). There are none of the range checks in here that would be present in a "real" piece of software. Printf may misformat the final output for "product" depending upon what compiler you use. Comment in here if you need explanation. Note that Blurtit has thrown away the backslashes in front of the "n" at the end of some of the text to create a newline.

int main(int argc, char* argv[], char* envp[])

{

        int inNumbers[5];

        int total=0;

        int product=1;

        short i;

/* First get the numbers in */

        for (i=1; i<6; i++)

        {

                printf("Please give me a numbern");

                scanf("%d", &inNumbers[i]);

                printf("You entered %dn", inNumbers[i]);

        }

/* now sum them */

        for (i=1; i<6; i++)

        {

                total=total+inNumbers[i];

        }

        printf("Total is %dn",total);

/*now now produce a product */

        for (i=1; i<6; i++)

        {

                printf("Product so far is %dn",product);

                product=product*inNumbers[i];

        }

        printf("Product is %dn",product);

return 0;

}


Answer Question

Anonymous