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
Katen [24]
2 years ago
13

Write a program that reads numbers from scanf (keyboard) and then sums them, stopping when 0 has been entered. Construct three v

ersions of this program, using the while, do-while, and forloops.
Computers and Technology
1 answer:
almond37 [142]2 years ago
4 0

Answer:

For loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0;

   printf("Enter a positive integer: ");

   scanf("%d", &number);

   

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

       if(number == 0)

           break;

       sum += number;

       

       printf("Enter a positive integer: ");

       scanf("%d", &number);

   }

   printf("Sum is: %d", sum);

   return 0;

}

- - - - -

While Loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0, i = 0;

   printf("Enter a positive integer: ");

   scanf("%d", &number);

   

   while (number != 0) {

       sum += number;

       

       printf("Enter a positive integer: ");

       scanf("%d", &number);

   }

   printf("Sum is: %d", sum);

   return 0;

}

Do-while Loop Version:

#include <stdio.h>

int main()

{

   int number, sum = 0, i = 0;

   

   do {

       printf("Enter a positive integer: ");

       scanf("%d", &number);

       

       sum += number;

       

   } while (number != 0);

   printf("Sum is: %d", sum);

   return 0;

}

Explanation:

- Initialize the variables

- Ask the user for the numbers until the user enters 0

-  Calculate and print the sum

You might be interested in
You are using a device that reads the physical addresses contained in incoming data that travels along network cables. Based on
GuDViN [60]

You are using a device that reads the physical addresses contained in incoming data that travels along network cables. Based on the physical address that it reads, the device then forwards the data out one of its ports to reach the destination device. The type of device you are using is router.

<h3>What is Router?</h3>

A router is an hardware device that is used in transferring information or data from a system to another.

The data can also be transfered from one computer networks to another.

Router makes it easier for more than one device to be connected easily without difficult Internet access.

Therefore, The type of device you are using that reads incoming data that travels along network cables is router.

Learn more on router below

brainly.com/question/24812743

#SPJ1

3 0
1 year ago
Write a loop that reads c-strings from standard input where the c-string is either "land", "air", or "water". The loop terminate
Anvisha [2.4K]

Answer:

I am writing a C++ code.        

#include <iostream> // for input output functions

using namespace std; // identifies objects like cin cout

int main() { //start of main() function body

string c_string; // stores the string entered by user from land water or air

int land=0; // contains the number of times land string is read in

int air=0; //contains the number of times air string is read in

int water = 0; //contains the number of times water string is read in

cin>>c_string; //reads the string entered by user from air water or land

/*while loop continues to execute until user enters xxxxx or maximum length of a string exceeds 8 */

while(c_string!= "xxxxx" && c_string.length()<=8) {

if(c_string=="land"){ // if string entered by user is land

land = land + 1;} //counts and increments each occurrence of land string by 1

else if(c_string=="air"){// if string entered by user is air

air = air + 1;}// counts and increments each occurrence of string air by 1

else if(c_string=="water"){// if string entered by user is water

water = water + 1;}//counts and increments each occurrence of air by 1

cin>>c_string;}

/* keeps reading the string entered by user from land air or water, until the loop breaks after the user enters xxxxx or user enters a string whose length is greater than 8 */

//prints the number of times land, air and water are read in

cout << "land:"<<land;

cout << endl<<"air:"<<air;

cout << endl<< "water:"<<water; }                      

                                                                                     

Explanation:

Everything is well explained in the comments above.

The program prompts the user to input strings. These strings are either land air or water. The while loop continues to read the input strings until user enters xxxxx or the string entered by user exceeds the length 8. Both these terminating conditions are added in the while loop. After the loop terminates, the number of times land, air and water strings are read is displayed on the output screen. Any other string entered by user other than these 3 is ignored. The program along with the output is attached.

3 0
3 years ago
Fiona is creating a presentation with PowerPoint Online about how pencils are made. She would like to type an explanation about
777dan777 [17]

Answer:THE ANSWER IS A.

Explanation:I DID IT AND I GOT 100

6 0
2 years ago
Which of the following statements best describes O*NET OnLine?
irakobra [83]

The process that aids managers to better understand the processes through which they are performed most effectively by gathering and organizing detailed information about various jobs within an organization so that is called Job analysis and is performed by a job analyst in an organization. Occupational Information Network (O*Net) is a database that provides both basic and advanced job-analysis information; as such, IT can be viewed as an alternative to conducting job analysis. O*NET OnLine has detailed descriptions of the world of work for use by job seekers, workforce development and HR professionals, students, researchers, and more. It is a tool for career exploration and job analysis.

 

 

 





8 0
2 years ago
After execution of the code fragment
lesya692 [45]
Yes the output is 6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  





8 0
3 years ago
Other questions:
  • Social networking sites like Office Online, PayPal, and Dropbox are used to develop social and business contacts.
    6·2 answers
  • Which of the following best describes a proxy firewall? A. It sends traffic through another host. B. It acts as a gateway for re
    15·1 answer
  • Assume that input file references a Scanner object that was used to open a file. Which of the following while loops shows the co
    6·1 answer
  • What is the ability for a system to respond to unexpected failures or system crashes as the backup system immediately and automa
    8·1 answer
  • PLEASE HELP!!!!
    6·2 answers
  • What is the term for the era created by the digital revolution?<br>​
    13·1 answer
  • Pls help will give brainlest!!! The image shows a line graph. Which scientist is most likely to use this visual aid in a present
    15·1 answer
  • What is assembler? What is Compiler?What is interpreter?
    9·2 answers
  • What is the remainder obtained by dividing x 7 + x 5 + 1 by the generator polynomial x 3 + 1?
    9·1 answer
  • The arrangement of keys on a keyboard, QWERTY reflects the keyboard layout by ________. Group of answer choices using the letter
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!