answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
xz_007
2 months ago
13

Write a do-while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The user shoul

d be asked if he or she wishes to per- form the operation again. If so, the loop should repeat; otherwise it should terminate.
Computers and Technology
1 answer:
zubka84 [1K]2 months ago
5 0

The do-while loop for this task is presented below.

do

{

// request the user to input two numbers

cout<<"Please input two numbers to add."<<endl;

cin>>num1;

cin>>num2;

sum = num1 + num2;

// display the sum

cout<<"The sum of the two numbers is "<<sum<<endl;

// ask the user if they want to continue

cout<<"Would you like to keep going (y/n)?"<<endl;

cin>>choice;

}while(choice!= 'n');

The variables for the two numbers and their sum are defined as float for compatibility with both integers and decimals.

float num1, num2, sum;

The choice variable is char since it only needs to hold a single character input from the user, either 'y' or 'n'.

char choice;

The complete program is structured as follows:

#include <iostream>

using namespace std;

int main() {

float num1, num2, sum;

char choice;

do

{

// request the user to input two numbers

cout<<"Please input two numbers to add."<<endl;

cin>>num1;

cin>>num2;

sum = num1 + num2;

// display the result

cout<<"The sum of the two numbers is "<<sum<<endl;

// query the user on continuing

cout<<"Would you like to keep going (y/n)?"<<endl;

cin>>choice;

}while(choice!= 'n');

cout<<"Exiting..."<<endl;

}

You might be interested in
Sean is white hat hacker, who is demonstrating a network level session hijack attacks and as part of it, he has injected malicio
amid [951]

Answer:

The type of session hijacking attack that Sean is illustrating is Blind Hijacking, option C.

Explanation:

This is because he is inserting harmful data or commands into the compromised communications within the TCP session, regardless of the source-routing being turned off. He is capable of sending the data or commands, but does not have the ability to view the responses.

3 0
1 month ago
In mathematics, the factorial of a positive integer n, denoted as n! , is the product of all positive integers less than or equa
oksian1 [950]
public static int factorial(int n) { if (n >= 1 && n <=12) { if (n == 1) return 1; else return n * factorial(n - 1); } else return -1; } Explanation: The factorial method takes a single integer n as input. It checks if n is within the range of 1 to 12. If it is, it further checks if n equals 1. If it is indeed 1, it returns 1 as the factorial. Otherwise, it recursively calls itself with n decreased by 1, multiplying the result by n. If n is outside this range, the method returns -1 indicating the input is invalid.
6 0
1 month ago
Read 2 more answers
To encourage good grades, Hermosa High School has decided to award each student a bookstore credit that is 10 times the student’
amid [951]

Answer:

// here's the Java code.

import java.util.*;

// definition of the class

class BookstoreCredit

{

/* method that prints a message including the name and grade point */

   public static void fun(String name,double grade)

   {

// multiplying grade by 10

       grade=grade*10;

// display the message

       System.out.println(name+"\'s average grade is $"+grade);

   }

// main method for the class

public static void main (String[] args) throws java.lang.Exception

{

   try{

    // scanner instance to read input

       Scanner s=new Scanner(System.in);

        // declaring variables

      String s_name;

      double grade;

      System.out.print("Please enter the name:");

      s_name=s.nextLine();

      System.out.print("Please enter the grade point:");

      grade=s.nextDouble();

   }catch(Exception ex){

       return;}

}

}

Explanation:

Define a string variable named s_name and a double variable named grade. Obtain the name and grade from the user, then invoke the fun() method with these parameters, which will multiply the grade by 10 and output a message that includes the student's name along with their average grade.

Output:

Please enter the name:john

Please enter the grade point:3.2

john's average grade is $32.0

7 0
1 month ago
Life changing technology is easy to fall in love with. Describe a feature of a product that did it for you and highlight its ben
Amiraneli [1052]

An example of transformative technology that significantly affected my life is Artificial Intelligence.

Whether through Google Assistant, Amazon Alexa, or an AI-powered online medical consultant, this technology offers dependable and valuable assistance.

It helps manage daily reminders amidst a busy schedule or provides medical advice for a sick family member without needing a hospital visit, and often offers useful suggestions.

This technology has altered our lives in various ways, including my own.

5 0
2 months ago
Write an expression to print each price in stock_prices. Sample output with inputs: 34.62 76.30 85.05
8_murik_8 [964]

Answer:

Below is the Python code:

stock_prices = input().split() #this takes input and separates it into a list

for price in stock_prices: #this loops through each stock price

   print("$",price) #this outputs each stock price prefixed by a dollar sign

Explanation:

The logic behind the program is clearly outlined in the attached comments. To illustrate the program's workings, let's consider an example:

Imagine the user inputs the stock_prices values of

34.62 76.30 85.05

The input() method captures user input, while the split() method divides the input string, providing a list of strings as:

['34.62', '76.30', '85.05']

Following that, the loop statement for price in stock_prices: iterates through each item in the list, and print("$",price) displays each value from the list on the output screen with a dollar sign, as follows:

$ 34.62                                                                                                            

$ 76.30                                                                                                            

$ 85.05    

4 0
2 months ago
Other questions:
  • Your computer science teacher asks you to sample a black and white image that is 4" x 6". How would you sample the image to prov
    8·1 answer
  • Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
    12·1 answer
  • Create a conditional expression that evaluates to string "negative" if user_val is less than 0, and "non-negative" otherwise.
    6·1 answer
  • Describe a situation involving making a copy of a computer program or an entertainment file of some sort for which you think it
    7·1 answer
  • A wireless network was recently installed in a coffee shop and customer mobile devices are not receiving network configuration i
    12·1 answer
  • Tanya wants to include a video with all its controls on her web page. The dimensions of the video are as follows:
    6·2 answers
  • Suppose you have a certain amount of money in a savings account that earns compound monthly interest, and you want to calculate
    10·2 answers
  • Which of the following attributes of a website indicates a more reliable source for information?
    15·1 answer
  • a.Write a Python function sum_1k(M) that returns the sumí‘ í‘ = ∑1푘푘푀푀푘푘=1b.Compute s for M = 3 by hand and write
    15·1 answer
  • Write a program to read-in a sequence of integers from the keyboard using scanf(). Your program will determine (a) the largest i
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!