Jump to content

Using sum and average in loop c#

sil130
Need help on how can i get the sum of the encoded entries. The instuctions is enter any number between 0-100. And it will stop if the user enters -1.

When I try to get the sum, it includes the -1. Example, 300,300,300,-1. The sum should be 900, but it shows 899 because it includes -1. How can i remove numbers that arent between 0 and 100 on the computation? And i also need to get the average of the numbers without -1 and other numbers exceeding 100. This is the code ive come up with. Sorry for bad english :)

 

 

 

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace SumAverageProject

{

    class Program

    {

        static void Main(string[] args)

        {

            float nSum = 0, average = 0;

            int nNum = 0;

            int nCounter = 0;

            double nLength = 0;

            while (nNum != -1)

            {

                Console.Write("Type a number :");

                nNum = Convert.ToInt16(Console.ReadLine());

 

                if (nNum <= 100)

                    nSum += nNum;

                nCounter++;

            }

            Console.WriteLine("You typed " + nCounter + "  numbers");

            {

              

 

 

            {

 

                Console.WriteLine("The sum is : {0}", +nSum);

 

            }

            Console.WriteLine("The average is: {0}", nSum / nLength);

 

 

            Console.ReadKey();

        }

 

    }

}

Link to comment
Share on other sites

Link to post
Share on other sites

 

Need help on how can i get the sum of the encoded entries. The instuctions is enter any number between 0-100. And it will stop if the user enters -1.
When I try to get the sum, it includes the -1. Example, 300,300,300,-1. The sum should be 900, but it shows 899 because it includes -1. How can i remove numbers that arent between 0 and 100 on the computation? And i also need to get the average of the numbers without -1 and other numbers exceeding 100. This is the code ive come up with. Sorry for bad english :)
 
 
 
 
 
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace SumAverageProject{    class Program    {        static void Main(string[] args)        {            float nSum = 0, average = 0;            int nNum = 0;            int nCounter = 0;            double nLength = 0;            while (nNum != -1)            {                Console.Write("Type a number :");                nNum = Convert.ToInt16(Console.ReadLine());                 if (nNum <= 100)                    nSum += nNum;                nCounter++;            }            Console.WriteLine("You typed " + nCounter + "  numbers");            {                            {                 Console.WriteLine("The sum is : {0}", +nSum);             }            Console.WriteLine("The average is: {0}", nSum / nLength);              Console.ReadKey();        }     }}
Try replacing your
 
nNum = Convert.ToInt16(Console.ReadLine());
with
nNum = int.Parse(Console.ReadLine());
 
Also, you will want to test the user's input BEFORE applying the sum.
For example:
 
while (int.Parse(Console.ReadLine() != -1){//Code goes here}

 

 

EDIT: Also, please use code tags, using the blue button in the editor that looks like this <> or [ CODE][ /CODE]. And don't forget to follow your topics @sil130

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×