In: Computer Science
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 :)
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: