Question

In: Computer Science

Part (a) Repeatedly prompt for a number and sum it. When the sum just exceeds 100,...

Part (a) Repeatedly prompt for a number and sum it. When the sum just exceeds 100, stop the prompting and display the sum at the end. You must not display the sum while the user in typing in numbers. (You do not need a counter but you will need some way of terminating the loop)

Part(b)Same as the previous question but additionally displays the average of the numbers when the loop terminates. You will need a counter but not as a loop terminator.

Can you do part a and b on separate codes and both in C#. Thank you :)

Solutions

Expert Solution

a)

C# code:

using System;
class Program{
static void Main(){
        //initializing sum as 0
        int sum=0;
        //looping till sum exceeds 100
        while(sum<=100){
            //asking for number
            Console.Write("Enter number: ");
            //accepting it
            sum+=Convert.ToInt32(Console.ReadLine());
        }
        //printing sum
        Console.WriteLine("Sum: "+sum);
}
}

Screenshot:


Input and Output:

b)

C# code:

using System;
class Program{
static void Main(){
        //initializing sum as 0
        int sum=0;
        //initializing count as 0
        int count=0;
        //looping till sum exceeds 100
        while(sum<=100){
            //asking for number
            Console.Write("Enter number: ");
            //accepting it
            sum+=Convert.ToInt32(Console.ReadLine());
            //incrementing count
            count+=1;
        }
        //printing sum
        Console.WriteLine("Sum: "+sum);
        //printing average
        Console.WriteLine("Average: "+(float)sum/(float)count);
}
}

Screenshot:


Input and Output:


Related Solutions

JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
Overflow. Find out what happens when you try to add two numbers whose sum exceeds the...
Overflow. Find out what happens when you try to add two numbers whose sum exceeds the maximum integer value (just over two billion, one hundred million). Test two billion plus two billion. What result did you get? Explain why overflow happens on a computer. Explain inexact results on integer division such as: 2 / 5 = 0 and 51 / 25 = 2
Four numbers have a sum of 9900. The second exceeds the first by 1/7 of the...
Four numbers have a sum of 9900. The second exceeds the first by 1/7 of the first. The third exceeds the sum of the first two by 300. The fourth exceeds the sum of the first three by 300. Find the four numbers.
A die is continuously rolled until the total sum of all rolls exceeds 325. What is...
A die is continuously rolled until the total sum of all rolls exceeds 325. What is the probability that at least 95 rolls are necessary?
PYTHON PROGRAM Requirements: 1. Create an empty directory a variable named song_search 2. Repeatedly prompt the...
PYTHON PROGRAM Requirements: 1. Create an empty directory a variable named song_search 2. Repeatedly prompt the user to enter either the title of a song or enter nothing if they have no more songs to enter 3. For each song title entered by the user, split the song title into words by using the split function and splitting on a space. For each word from each song title, ensure the song_search dictionary has a entry with that word as the...
If the number of units produced exceeds the number of units sold, then net operating income...
If the number of units produced exceeds the number of units sold, then net operating income under absorption costing will: be equal to the net operating income under variable costing less total fixed manufacturing costs. be greater than net operating income under variable costing. be equal to the net operating income under variable costing. be equal to the net operating income under variable costing plus total fixed manufacturing costs. . A company produces a single product. Last year, fixed manufacturing...
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
In a two digit number the sum of the digits is 9. Also, when 27 is subtracted from the number the digits are reversed. Find the number?
Number 1: Explain the answers to the following: (a) If the IRR of a project exceeds...
Number 1: Explain the answers to the following: (a) If the IRR of a project exceeds the required rate of return, what do you know about the Modified Internal Rate of Return? (b). Why should funds raised to satisfy the additional funds needed be considered long term funds?
17#7 A die is continuously rolled until the total sum of all rolls exceeds 250. What...
17#7 A die is continuously rolled until the total sum of all rolls exceeds 250. What is the probability that at least 80 rolls are necessary?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT