1answer.
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
Serggg [28]
2 years ago
12

Write a while loop that prints userNum divided by 4 (integer division) until reaching 2. Follow each number by a space. Example

output for userNum = 160:
40 10 2

Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 160, then with userNum = 8, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks".

Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Programend never reached." The system doesn't print the test case that caused the reported message.

#include
using namespace std;

int main() {
int userNum;

cin >> userNum;

/* Your solution goes here */

cout << endl;

return 0;
}
Computers and Technology
1 answer:
Slav-nsk [51]2 years ago
4 0

Answer / Explanation:

#include <iostream>  

using namespace std;

int main()  

{    

 int userNum = 0;  

  userNum = 20;    

  cout << userNum << " ";

  while (userNum > 1)    

  {

     userNum = userNum/2;

     cout << userNum << " ";    

  }    

  cout << endl;  

  return 0;  

}

However, we should note that the above codes divides properly but when it gets to 0, it will always give output as 0 instead of terminating the program.

Hence to make it terminate, we include:

while (userNum > 1)    

{

  cout << userNum << " ";    

  userNum = userNum/2;

}    

The above code alternatively should be replaced with int userNum = 0;  .

Also, for the sake of industry best standard and the general principle, we can say:

The general principle is:

while ( <conditional> )

{

  // Use the data

  // Change the data as the last operation in the loop.

}

A for loop provides natural placeholders for these.

for ( <initialize data>; <conditional>; <update data for next iteration> )

{

  // Use the data

}  

If you were to switch to using a for loop, which I recommend, your code would be:

for ( userNum = 20; userNum > 0; userNum /= 2 )

{

  cout << userNum << " ";

You might be interested in
Given the following structure and variable definitions, struct customer { char lastName[ 15 ]; char firstName[ 15 ]; unsigned in
garri49 [273]

Answer:

see explaination

Explanation:

a)

customerRecord.lastName

b)

customerPtr->lastName or (*customerPtr).lastName

c)

customerRecord.firstName

d)

customerPtr->firstName or (*customerPtr).firstName

e)

customerRecord.customerNumber

f)

customerPtr->customerNumber or (*customerPtr).customerNumber

g)

customerRecord.personal.phoneNumber

h)

customerPtr->personal.phoneNumber or (*customerPtr).personal.phoneNumber

i)

customerRecord.personal.address

j)

customerPtr->personal.address or (*customerPtr).personal.address

k)

customerRecord.personal.city

l)

customerPtr->personal.city or (*customerPtr).personal.city

m)

customerRecord.personal.state

n)

customerPtr->personal.state or (*customerPtr).personal.state

o)

customerRecord.personal.zipCode

p)

customerPtr->personal.zipCode or (*customerPtr).personal.zipCode

6 0
2 years ago
Supports traditional transactional processing for day-to-day front-office operations or systems that deal directly with the cust
Jet001 [13]

Answer:

Operational CRM.

Explanation:

Operational CRM can be described as most of the programs that allow a company to taking care of the desires of the consumers. The structure links and maintains the selling, marketing, and customer support activities of a corporation, thus providing a structure that requires customer service.

So, the following answer is correct according to the statement.

7 0
3 years ago
It is usually simple to delete old posts and online conversations if they make you look bad.
denis23 [38]

Answer:

not usually..sorry :(

Explanation:

7 0
3 years ago
Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. T
hammer [34]

Answer:

In C++:

#include<iostream>

#include<vector>

using namespace std;

int main() {

int len;

cout<<"Length: ";  cin>>len;

string inpt;

vector<string> vect;

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

   cin>>inpt;

   vect.push_back(inpt); }

char ch;

cout<<"Input char: ";  cin>>ch;  

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

   size_t found = vect.at(i).find(ch);  

       if (found != string::npos){

           cout<<vect.at(i)<<" ";

           i++;

       }

}  

return 0;

}

Explanation:

This declares the length of vector as integer

int len;

This prompts the user for length

cout<<"Length: ";  cin>>len;

This declares input as string

string inpt;

This declares string vector

vector<string> vect;

The following iteration gets input into the vector

<em>for(int i =0;i<len;i++){ </em>

<em>    cin>>inpt; </em>

<em>    vect.push_back(inpt); } </em>

This declares ch as character

char ch;

This prompts the user for character

cout<<"Input char: ";  cin>>ch;  

The following iterates through the vector

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

This checks if vector element contains the character

   size_t found = vect.at(i).find(ch);  

If found:

       if (found != string::npos){

Print out the vector element

           cout<<vect.at(i)<<" ";

And move to the next vector element

           i++;

       }

}  

4 0
3 years ago
Can you structure this code in if-statements?
Naddika [18.5K]
Not sure what quizScore1-5 is, but here it is. Also, I'm not entirely sure what language you're working with here, but null can't be returned as an integer. If it were a string, for example, it'd be possible as string is an object reference type.

public int getData(int dataNumber)
{
    if (dataNumber == 1)
        return quizScore1;
    else if (dataNumber == 2)
        return quizScore2;
    else if (dataNumber == 3)
        return quizScore3;
    else if (dataNumber == 4)
        return quizScore4;
    else if (dataNumber == 5)
        return quizScore5;
    else
        return 0;
}
3 0
2 years ago
Other questions:
  • Using a loop, write a program that reads in exactly five integers and outputs the sum.
    11·1 answer
  • What is qwerty and why is it on the keyboard?
    15·2 answers
  • (TCO C) When a remote user attempts to dial in to the network, the network access server (NAS) queries the TACACS+ server. If th
    14·1 answer
  • 7.) Title text boxes on every slide must be the same format. <br> A. True <br> B. False
    15·2 answers
  • ...................You like swamps
    13·2 answers
  • create a function that has an argument is the triple jump distance. It returns the estimate of vertical jump height. The world r
    8·1 answer
  • 4.12 LAB: Using math methods Given three floating-point numbers x, y, and z, output x to the power of z, x to the power of (y to
    10·1 answer
  • What is wrong with the following code?
    11·1 answer
  • Un producto tecnológico puede ser tangible o intangible?
    6·1 answer
  • What are different social phenomenas are you aware of that used part of a social movement, change, or cause
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!