Question

In: Computer Science

Ask the user to input a series of numbers, write a C# program to output the...

Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.



Solutions

Expert Solution

Code:

using System;
class series
{
static void Main()
{
int n,max=0,min=0,sum=0,i,num;/*Declared variables*/

while(true)
{/*infinet while loop*/
try
{
Console.Write("Enter no of numbers you want to enter: ");
n=Convert.ToInt32(Console.ReadLine());
/*here we read the integer from the user
if he entered any charecter this loop iterate again
untill he enter an integer*/
break;
}
catch(Exception e)
{
/*if he enter other than integer we print enter only integer*/
Console.WriteLine("Enter only integers");
}
}
Console.WriteLine("Enter the numbers: ");
/*Here we ask the user to enter series of numbers*/
for(i=0;i<n;i++)
{/*This loop iterate for 0 to n-1*/
while(true)
{/*infinet while loop*/
try
{
num=Convert.ToInt32(Console.ReadLine());
/*here we read the integer from the user
if he entered any charecter this loop iterate again
untill he enter an integer*/
break;
}
catch(Exception e)
{ /*if he enter other than integer we print enter only integer*/
Console.WriteLine("Enter only integers");
}
}
if(i==0)
{/*IF the user enter for the first time we make min sum max as the enterd number*/
min=num;
max=num;
sum=num;
}
else
{
if(num>max)
{
/*If enterd number is greter than the max
then we make the entered number as max*/
max=num;
}
if(num<min)
{
/*If enterd number is less than the min
then we make the entered number as min*/
min=num;
}
sum+=num;
}
}
Console.WriteLine("Sum = "+sum);
Console.WriteLine("min = "+min);
Console.WriteLine("max = "+max);
/*Finally we print min max and sum*/
}
}

Output:

Indentation:



Related Solutions

Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Question: Conversion Program * This program will ask the user to input meters * After input,...
Question: Conversion Program * This program will ask the user to input meters * After input, a menu will be displayed: * 1. Convert to kilometers * 2. Convert to inches * 3. Convert to feet * 4. Quit * * The user will select the conversion and the converted answer will be displayed. * The program should redisplay the menu for another selection. * Quit program when user selects 4. Hi, my instructor requirement fix the bug in the...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT