Question

C++ program by using while loop structure. Write an interactive program (C++) to prompt and read...

C++ program by using while loop structure.

Write an interactive program (C++) to prompt and read two float type numbers, Then your program should perform and print the following arithmetic: (use while loop structure to repeat program 2 times. 1) Sum 2)Difference 3) Product 4) Quotient 5) Modulus (use system defined fmod function) You must use system's defined function prototype and/or to create programmer's defined function prototype for each of the above calculation. In addition, create a function called "DISPLAYLINE " using looping concept and a single print statement to create a line of Add (#) signs.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include<iostream>

#include <math.h>

using namespace std;

void DISPLAYLINE(){

for(int i=0;i<21;i++){

cout<<" # ";

}

}

int main(){

int i=0;

while(i<2){

float a,b,sum,diff,prod,quotient,mod;

cout<<"Enter number 1: ";

cin>>a;

cout<<"Enter number 2: ";

cin>>b;

sum=a+b;

diff=a-b;

prod=a*b;

quotient=a/b;

mod=fmod(a,b);

cout<<"Sum of num1 and num2 = "<<sum<<endl;

cout<<"Diff of num1 and num2 = "<<diff<<endl;

cout<<"Product of num1 and num2 = "<<prod<<endl;

cout<<"Quotient of num1/num2 = "<<quotient<<endl;

cout<<"Mod of num1 and num2 = "<<mod<<endl;

DISPLAYLINE();

i++;

}

}

if you like the answer please provide a thumbs up.

Add a comment
Answer #2

Here's a C++ program that fulfills the requirements mentioned:

cppCopy code#include <iostream>#include <cmath>void displayLine(int n){    for (int i = 0; i < n; i++)
    {
        std::cout << "#";
    }
    std::cout << std::endl;
}float sum(float num1, float num2){    return num1 + num2;
}float difference(float num1, float num2){    return num1 - num2;
}float product(float num1, float num2){    return num1 * num2;
}float quotient(float num1, float num2){    return num1 / num2;
}float modulus(float num1, float num2){    return std::fmod(num1, num2);
}int main(){    int count = 0;    while (count < 2)
    {        float num1, num2;
        std::cout << "Enter two numbers: ";
        std::cin >> num1 >> num2;

        std::cout << "Sum: " << sum(num1, num2) << std::endl;
        std::cout << "Difference: " << difference(num1, num2) << std::endl;
        std::cout << "Product: " << product(num1, num2) << std::endl;
        std::cout << "Quotient: " << quotient(num1, num2) << std::endl;
        std::cout << "Modulus: " << modulus(num1, num2) << std::endl;        displayLine(30);

        count++;
    }    return 0;
}

In this program, we have defined functions for each arithmetic operation: sum, difference, product, quotient, and modulus. The displayLine function is responsible for printing a line of "#" signs.

The main function uses a while loop to repeat the program's execution two times. Inside the loop, it prompts the user for two float numbers, performs the arithmetic calculations using the defined functions, and prints the results. After each iteration, the displayLine function is called to print a line of "#" signs.

Note: The program assumes that the user will enter valid input (float numbers) and does not include input validation. It's always a good practice to include appropriate input validation checks in a real-world program.

answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
C++ program by using while loop structure. Write an interactive program (C++) to prompt and read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming in C: Using a nested for loop create the pattern below. Prompt the user for...

    Programming in C: Using a nested for loop create the pattern below. Prompt the user for the height (number of rows) and width (number of characters per line) for pattern A. You may use any number for height and width, just as long as the basic pattern is preserved. Call a function to print the pattern. This function receives the height and width as inputs (parameters) but does not return a value. Make use of an if structure to instruct...

  • Just do program 6 using c++ which are printf and scanf. Please don’t use cout and...

    Just do program 6 using c++ which are printf and scanf. Please don’t use cout and cin. Program 4: A slot machine has three windows. A random fruit is picked for each window from cherry apple, lemon, and orange. If all three windows match, the user wins 8 times the bet amount. If the first two windows only are cherries, the user wins 3 times the bet amount. If the first window is a cherry and the second window is...

  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • C++ Program Write a do-while loop that continues to prompt a user to enter a number...

    C++ Program Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is: Enter a number (<100): Enter a number (<100): Enter a number (<100): Your number < 100 is: 25 #include <iostream> using namespace std; int main() { int userInput; /* Your solution goes here */...

  • Write a c++ program to calculate the approximate value of pi using this series. The program...

    Write a c++ program to calculate the approximate value of pi using this series. The program takes an input n that determines the number of values we are going to use in this series. Then output the approximation of the value of pi. The more values in the series, the more accurate the data. Note 5 terms isn’t nearly enough. You will try it with numbers read in from a file. to see the accuracy increase. Use a for loop...

  • In C language using printf and scanf statements: Write a program to display a histogram based...

    In C language using printf and scanf statements: Write a program to display a histogram based on a number entered by the user. A histogram is a graphical representation of a number (in our case, using the asterisk character). On the same line after displaying the histogram, display the number. The entire program will repeat until the user enters zero or a negative number. Before the program ends, display "Bye...". The program will ask the user to enter a non-zero...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • Write a C program named space_to_line.c that features a while loop that continuously reads input from...

    Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match...

  • Write a C++ program. Using the while loop or the do – while loop write a...

    Write a C++ program. Using the while loop or the do – while loop write a program that does the following: Calculate the average of a series of homework grades (0 - 100) entered one at a time. In this case the lowest score will be dropped and the average computed with the remaining grades. For example suppose you enter the following grades: 78, 85, 81, 90, 88, 93 and 97.The average will be computed from the 6 grades 85,...

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT