Question

In: Computer Science

Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0...

Write a program that reads numbers from scanf1 (keyboard) and then sums them, stopping when 0 has been entered. Construct three versions of this program, using the while, do-while, and for loops.

Solutions

Expert Solution

Code using for loop:

#include<stdio.h>
int main()
{
   int i,k=1,sum=0;
   for(i=0;k!=0;i++)
   {
       printf("Enter your input: ");
       scanf("%d",&k);
       sum=sum+k;
   }
   printf("%d",sum);
}

Indentation:

Output;

Code using while loop:

#include<stdio.h>
int main()
{
   int i,k=1,sum=0;
   while(k)
   {
       printf("Enter your number:");
       scanf("%d",&k);
       sum=sum+k;
   }
   printf("Sum is : %d", sum);
}

Indentation:

Output:

Code using do-while loop:

#include<stdio.h>
int main()
{
   int i,k,sum=0;
   do
   {
       printf("Enter your number:");
       scanf("%d",&k);
       sum=sum+k;
   }while(k);
   printf("Sum is : %d", sum);
}

Indentation:

Output:


Related Solutions

Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients...
Write a program that reads three double numbers from the keyboard representing, respectively, the three coefficients a, b, and c of a quadratic equation. Solve the equation using the following formulas: x1 = ( - b + square root (b2 – 4ac)) / (2a), x2 = ( - b + square root (b2 – 4ac)) / (2a) Run your program on the following sample values: a=1.0, b=3.0,c=2.0 a=0.5, b=0.5,c=0.125 a=1.0, b=3.0,c=10.0
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an...
Write a program that reads three values from the keyboard representing, respectively, an investors name, an investment amount, and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate) . Your program should calculate and output (in currency notation) the future value of the investment in 10, 2 20 an 30 years using the following formula:   Future value =investment(1+interest rate)year Example of a test run: Enter...
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print...
(C++) Write a nested loop that reads an integer n (n>0) from the keyboard and print out "n" lines as follows: 0 0 1 0 1 2 0 1 2 3 … 0 1 2 3 … n-1
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT