Ask your own question, for FREE!
Computer Science 9 Online
OpenStudy (anonymous):

My friend and I need help with our project. The directions are "Write a C# program to count the number of negative numbers in a sequence of numbers the user inputs. In this solution, before reading the numbers, ask the user to input how many numbers will be input. For Example: For input 32, 76, -12, 49, -11, and -3 the output should be that there are three negative numbers. So far we have Console.Write("How many numbers will you be inputting?: "); int num = int.Parse(Console.ReadLine()); for (int i = 0; i < num; i++) { Console.Write("PLease enter a number: "); int x = int.Parse(C

OpenStudy (anonymous):

Here it is in Python. Considering what you have already, you should be able to adapt this into C#. Basically you just need to initialize a counter outside the for to 0 and increment it if inputs are less than 0. Then display result when for loop terminates. >>> def count_negative(): num = int(input("How many?: ")) negs = 0 for i in range(num): nums = int(input("Enter number: ")) if nums < 0: negs += 1 print(str(negs) + " negs entered.") >>> count_negative() How many?: 6 Enter number: 32 Enter number: 76 Enter number: -12 Enter number: 49 Enter number: -11 Enter number: -3 3 negs entered.

OpenStudy (anonymous):

Here is the solution you are looking for: Console.Write("How many numbers will you be inputting?: "); int num = int.Parse(Console.ReadLine()); int count = 0; for (int i = 0; i < num; i++) { Console.Write ("PLease enter a number: "); int x = int.Parse (Console.ReadLine ()); if (x < 0) { count++; } } Console.WriteLine ("There are {0} negative numbers.", count);

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!