Asked Dec 8, 2019

Write the following in C++

Write three functions, float getNum(), float add(float, float), void outSum(float); that asks user for two numbers, finds the sum of two numbers, and displays the sum repeatly in a main program until the user enter “0” for either one of the numbers.

Expert Answer

Step 1

The C++ program and approach are given below:


  • Declaring the essential headers and the namespaces.
  • Defining the three functions “float getNum()”, “float add(float x, float y)” and “void outSum(float s)”.
  • Inside the “float getNum()” function, variable num is declared which stores the two numbers entered by the user. The getnum() function allows the user to enter two numbers. When the user enters the first number it stores in the num variable than it stores to the getnum(). And it again asks the user to enter the second number.
  • If-else statement will check whether the number is greater than zero or not.
  • Inside the “float add(float x, float y)”, sum of x and y is returned.
  • The “void outSum(float s)” function will display the sum.
  • Inside the main() function while loop will execute until the statement is true(num!=0).
  • Inside the while loop, the outSum() is called and add() function is called as the function parameter for the outSum() The getNum() function is called two times as the function parameter for add() function(as add() takes two arguments).
Step 2


//essential headers

#include <iostream>

//using namespace

using namespace std;

//getNum() to take input from user

float getNum()


    //declaring the variables

    float num;

    //prompts the user to enter the number

    cout<<"Enter a number:\n";


    //if statement to check the entered number is greater than zero

    if(num != 0)


        return num;



        exit (0);


//add() function will return the sum of two arguments

float add(float x, float y)


    //returns the sum

    return x+y;


//outSum() will displays the sum 

void outSum(float s)



Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in


Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A

Q: Compiler returning errors. Screenshot of File.txt as well as compiler errors included.  //import...

A: Given program code is working perfectly and there is no error in this code. The error is in the file...


Q: List and describe four vulnerability intelligence sources. Which seems the most effective? Why?

A: Sources of Vulnerability IntelligenceSecurity VendorsSoftware VendorsGovernment and public organizat...


Q: Given the follwing businessrulea, identify primary keys, foreign keys, attributes, cardinalities.  A...

A: Primary keys: University: U_NameSchools: S_NameDepartments: D_IDProfessors: P_IDCourses: C_ID


Q: construct a pda that accepts the language defined by the grammar s → abSb|λ.

A: Constructing PDA:There are two states q and f. q is the start state and f is the final state. Here i...


Q: What is pseudo code?

A: Pseudocode is used in algorithm and programming based fields to outlines or represents the implement...


Q: Python Coding-While Loops  (In PyCharm Community edition)   Create a while loop that SUMS up ALL the...

A: Prompt user to enter initial numberPrompt user to enter ending numberUsing while loop sums up all th...


Q: What are the three aspects of infornation security management

A: Information securities also known as infosec, it is the way to control security risk like unauthoriz...


Q: Find the range of salaries (min, max) of each department in the following table:EMP(EID, ENAME, SAL,...

A: EMP is the name of the table Employee.EMP table has four rows named EID, ENAME, SAL, and DEPT. Find ...


Q: What is the difference between using direct and indirect addressing? Give an example.

A: Click to see the answer