!
Hi- this program is supposed to generate 100 random numbers between 100 and 1000, print out a message every 10th number printed to screen, and sum the total of the numbers. It does generate "random" numbers, but the message is not showing up and the numbers are not totalled at the end. Someone please help!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*This program finds 100 3 digit numbers between 100 and 1000, prints the 
 * "Cha, Cha, Cha!" after every 10th number, and outputs the sum of the numbers
 */

namespace MikeVertreeseRandom
{
    class RandomNumbers    //using the random class for number generation
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            int number = r.Next(100, 1000); //r.Next() finds the next random # bet 100 and 1000
            int numberTotal = number;  //declaring the variable "numberTotal" for the sum 
            numberTotal += number;                //the sum increases by new number with each pass
            int i = 1;            //i is the index counter
            for (i = 1; i <= 100; i++)  //the program will run through 100 iterations
                Console.WriteLine(r.Next(100, 1000)); //program prints the next random #
                numberTotal += number; //need to keep a running sum of the numbers found
                if ((i % 10) == 0)    //every 10th iteration, do something
            {
                    Console.WriteLine("Cha, Cha, Cha!"); //prints this message ea 10th number
                }
             Console.WriteLine("The sum is: ", +numberTotal);
             Console.ReadLine();
            }
        }
    }

Your total won't match the numbers generated because you declare number to be equal to r.Next on line 17 and then add it to numberTotal on line 18. At line 23 you add number to numberTotal again (inside the loop so it does this 100 times) but you aren't changing the value of number in the loop (i.e. you aren't setting it to the next r value) so you are adding the SAME number to numberTotal 100 times.

Thanks, hericles- that makes sense, but I still don't see why there is no output for the sum value once the loop is ended. I did add the missing curly braces in the for statement and now get my message output every 10th number. Still working on the output of sum...

Change the output statement to

Console.WriteLine("The sum is: " + numberTotal);

or

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

Hope this helps

Edited 4 Years Ago by samsylvestertty

Hi...
I have edited your code and made some changes.
Hope now it meet your need..
here comes the code...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*This program finds 100 3 digit numbers between 100 and 1000, prints the 
 * "Cha, Cha, Cha!" after every 10th number, and outputs the sum of the numbers
 */
namespace MikeVertreeseRandom
{
    class RandomNumbers    //using the random class for number generation
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            int number = r.Next(100, 1000); //r.Next() finds the next random # bet 100 and 1000
            int numberTotal = number;  //declaring the variable "numberTotal" for the sum 
            numberTotal += number;                //the sum increases by new number with each pass
            int i = 1;            //i is the index counter
            for (i = 1; i <= 100; i++)  //the program will run through 100 iterations
            {
                Console.WriteLine(r.Next(100, 1000)); //program prints the next random #
                numberTotal += number; //need to keep a running sum of the numbers found
                if ((i % 10) == 0)    //every 10th iteration, do something
                {
                    Console.WriteLine("Cha, Cha, Cha!"); //prints this message ea 10th number
                }
            }
            Console.WriteLine("The sum is: {0}", numberTotal);
            Console.ReadLine();
        }
    }
}

HTCc, you left in the initial flaw I mentioned in my first post, namely that the next r value isn't being added to numberTotal each loop. Lines 16 and 17 are not needed (beyond declaring numberTotal) and give an incorrect result as they add number to numberTotal twice for no reason.
It also looks like the only thing you changed was the edit already mentioned by Sam. Not being picky, but your posts should add something not mirror what previous posters have said.

the line 23 has the magiclogic,
add "numberTotal += r.Next(100, 1000);" on line 23. your problem is solved.
it worked for me.

Random r = new Random();
             //r.Next() finds the next random # bet 100 and 1000
            int numberTotal = 0; //declaring the variable "numberTotal" for the sum
            int i = 1; //i is the index counter
            for (i = 1; i <= 100; i++) //the program will run through 100 iterations
            {
                int number = r.Next(100, 1000);
                Console.WriteLine(number.ToString()); //program prints the next random #
                //numberTotal += number; //need to keep a running sum of the numbers found
                numberTotal += number;
                if ((i % 10) == 0) //every 10th iteration, do something
                {
                    Console.WriteLine("Cha, Cha, Cha!"); //prints this message ea 10th number
                }
            }
            Console.WriteLine("The sum is: {0}", numberTotal);
            Console.ReadLine();

This will solve your problem.

This article has been dead for over six months. Start a new discussion instead.