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 to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
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,...
Design a program that will ask the user to input two integer numbers and then perform...
Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it...
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...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT