0
Enter a number, find multiples of 3 and 5 below that num, and add them to the sum,if multiples of 3 and 5 are the same,addonce
For example, we only add 15 once to the sum, even though its multiples of 3 and 5 at same time.My problem is why does my code doesn't work? Due to character limits I will post my code in the comments.
8 Respuestas
+ 1
Show your attempt!
+ 2
Yasswecancn
I use linq and write this program
https://code.sololearn.com/cyOpGze3tfOL/?ref=app
+ 1
static void Main(string[] args)
{
int sum = 0;
int n = Convert.ToInt32(Console.ReadLine());
int a = 1;
while (a < n)
{
if (a % 3 == 0 || a % 5 == 0)
{
sum += a;
a += 1;
}
else
{
a += 1;
}
}
//
//int inita = 3;
//int initb = 5;
//while (inita < n)
//{
// Console.WriteLine(sum);
// sum += inita;
// inita += 3;
//}
//while (initb < n)
//{
// if (initb % 3 == 0)
// {
// continue;
// }
// sum += initb;
// initb += 5;
// Console.WriteLine(sum);
//}
+ 1
Character limits,sorry.The one in the comment was my code,but when you enter 100,they will give you different answers.The correct one will give you 2318,while my one got 2633,This is a easy sololearn question named multiples in code coach.
+ 1
Yasswecancn Are you sure about the answer? Can you send me the question link?
0
I wonder what are the differences between my code and the one I found from Google.
0
Well, Even though I haven't learned anything about linq yet ,but I still tried your code,when I entered 100 ,your code gives me 2418, your algorithm is interesting, but clearly it's incorrect. hossein B
0
It's a easy sololearn question in code coach, go to code coach ,and choose c# and the easy rank question ,you will find it at the bottom.
https://www.sololearn.com/coach/89?ref=app
I will set my code bits as public so you could see my code. hossein B