Skip to content

Adding up numbers by 6

Song Lee

Asked Sep 27, 2014, 2:23 PM EDTSolved
Share
LinkedInX
Hello C-Sharpcorner,
I want to know how to add numbers by 6, starting from #6 
(so, 6->12->18->24.. so forth until 100)

-----------------------------------------------------------------------------------------------

private void
six()
{
Console.WriteLine("the number will go up by 6");
double sixStart =6;

//Below is the nooby method that I came up with
//But I want to know if there is something better than doing this for individual numbers


Console.WriteLine("2nd # ={0}", sixStart + 6);
Console.WriteLine("3rd # ={0}", sixStart +12);

//Are there any formulas to replace this...?

}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • ✓ Accepted answer+1
    using System;

    class Program
    {
    static void Main(string[] args)
    {
    int x = 0;

    for (int i = 0; i < 16; i++)
    {
    x = x + 6;

    Console.WriteLine(x);
    }
    Console.Read();
    }
    }
    +1
  • 0
    Wow
    Thank you for the help.
    It did really save time for me :)
    0
  • 0
    In my view following is the good method because you don't need to bother to choose an appropriate number (in the above case it is 16) in the FOR loop.

    In the following program choose whatever number you like in the FOR loop, only thing is it should be bigger than the 100 here I choose 500.

    And in the IF loop make your requirement less or equal to 100 then you will obtain your required o/p.

    using System;

    class Program
    {
    static void Main(string[] args)
    {
    int x = 0;

    for (int i = 0; i < 500; i++)
    {
    x = x + 6;

    if (x <= 100)
    Console.Write(" " + x);
    else
    break;
    }
    Console.Read();
    }
    }
    // 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96
    0