Create the logic for a program that continuously prompts a user for a numeric value until the user enters 0. The application passes the value in turn to a method that computes the sum of all the whole numbers from 1 up to and including the entered number, and to a method that computes the product of all the whole numbers up to and including the entered number.

Respuesta :

Answer:

// here is code in C++.

#include <bits/stdc++.h>

using namespace std;

// function to compute sum and product

void sum_prod(int input)

{

   int sum=0;

   long long product=1;

   // compute sum of all from 1 to input number

   for(int i=1;i<=input;i++)

   {

       sum=sum+i;

   }

   // print sum

   cout<<"sum of all from 1 to "<<input<<" is:"<<sum<<endl;

   // compute product of all from 1 to input number

   for(int i=1;i<=input;i++)

   {

       product=product*i;

   }

   // print product

   cout<<"product of all from 1 to "<<input<<" is:"<<product<<endl;;

}

// driver function

int main()

{

   int n;

   cout<<"enter the number (0 to stop):";

   // read the input number

   cin>>n;

   // repeat until the user enter 0

   while(n)

   {

   // call the function to compute sum and product

      sum_prod(n);

      cout<<"enter the number (0 to stop):";

      cin>>n;

   }

return 0;

}

Explanation:

Read a number from user. Then call the function to calculate the sum and product of all number from 1 to input. In the function sum_prod(),create two variables "sum" and "product" to calculate the sum and product. Then print the output.This function will be called until user enter 0 an input.

Output:

enter the number (0 to stop):5

sum of all from 1 to 5 is:15

product of all from 1 to 5 is:120

enter the number (0 to stop):10

sum of all from 1 to 10 is:55

product of all from 1 to 10 is:3628800

enter the number (0 to stop):0

ACCESS MORE
EDU ACCESS