Answered by:
simple console application

Hey guys,
Why doesn't this show the result in the console?
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ConsoleApplication1
{
class
Program
{
static void Main(string
[] args)
{
int
number;
int
sum = 0;
for
(number = 1; number < 1001; number++)
{
if
(number % 3 == 0)
{
sum = sum + number;
//sum = number++;
}
else if
(number % 5 == 0)
{
sum = sum + number;
}
else if
((number % 3 == 0) && (number % 5 == 0))
{
sum = sum + number;
}
}
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is "
, sum.ToString());
Console
.ReadLine();
}
}
}
Kind regards, TheOtherSid :)
Question
Answers

You missed the placeholder for 'sum'. Try modifying the Console.WriteLine as:
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString());
Secondly, your logic has some errors too. If you were looking for numbers which are divisible by both 3 AND 5. It should be:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int number; int sum = 0; for (number = 1; number < 1001; number++) { if ((number % 3 == 0) && (number % 5 == 0)) { sum = sum + number; } } Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString()); Console.ReadLine(); } } }
Unless you were looking for numbers which are divisible by either 3 OR 5. The if condition should change as:if ((number % 3 == 0)  (number % 5 == 0))
Please "Mark Answered", if it solves your question.
Nadeesha Cabral Marked as answer by TheOtherSid Wednesday, July 27, 2011 2:54 AM
All replies


Open project properties. In 'Application' tab, select 'Console Application' from 'Output Type' combo box.
Hey dude,
That didn't solve the problem.
I think it's got something to do with the logic or something.What I'm trying to do is print out the sum of all the multiples of 3 or 5 below 1000.
Hope this helps.
Cheers.
Kind regards, TheOtherSid :) 
You missed the placeholder for 'sum'. Try modifying the Console.WriteLine as:
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString());
Secondly, your logic has some errors too. If you were looking for numbers which are divisible by both 3 AND 5. It should be:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int number; int sum = 0; for (number = 1; number < 1001; number++) { if ((number % 3 == 0) && (number % 5 == 0)) { sum = sum + number; } } Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString()); Console.ReadLine(); } } }
Unless you were looking for numbers which are divisible by either 3 OR 5. The if condition should change as:if ((number % 3 == 0)  (number % 5 == 0))
Please "Mark Answered", if it solves your question.
Nadeesha Cabral Marked as answer by TheOtherSid Wednesday, July 27, 2011 2:54 AM

You missed the placeholder for 'sum'. Try modifying the Console.WriteLine as:
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString());
Hey dude,
I was missing the placeholder.
Thanks for the help. Much appreciated.
The logic is fine.
What I wanted to do was print the sum of numbers which are divisible by 3 and/or 5.
Ideally, from 1  20, the numbers would be:
3, 5, 6, 9, 10, 12, 15, 18, 20The sum of these is: 78
Please find code attached.
Having said that....is there a way that the code can be made smaller and simpler to understand?
class Program
{
static void Main(string[] args)
{
int number;
int sum = 0;
for (number = 1; number < 20; number++)
{
if (number % 3 == 0)
sum = sum + number;else if (number % 5 == 0)
sum = sum + number;else if ((number % 3 == 0) && (number % 5 == 0))
sum = sum + number;
}
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString());
Console.ReadLine();
}
}
Kind regards, TheOtherSid :) 
Short and simple.
Beautiful, isn't it? ;)static void Main(string[] args)
{
int number;
int sum = 0;
for (number = 1; number < 10; number++)
{
if ((number % 3 == 0)(number % 5 == 0)  ((number % 3 == 0) && (number % 5 == 0)))
sum = sum + number;
}
Console.WriteLine("The sum of all numbers divisible by both 3 and 5, from 1 to 1000 is {0}", sum.ToString());
Console.ReadLine();
}Thanks for all the help, guys.
Cheers
Kind regards, TheOtherSid :)