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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

3rd Mar 2021, 8:37 PM
Yasswecancn
Yasswecancn - avatar
8 Answers
+ 1
Show your attempt!
3rd Mar 2021, 8:39 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
4th Mar 2021, 10:57 AM
hossein B
hossein B - avatar
+ 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); //}
3rd Mar 2021, 8:40 PM
Yasswecancn
Yasswecancn - avatar
+ 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.
3rd Mar 2021, 8:43 PM
Yasswecancn
Yasswecancn - avatar
+ 1
Yasswecancn Are you sure about the answer? Can you send me the question link?
4th Mar 2021, 7:46 PM
hossein B
hossein B - avatar
0
I wonder what are the differences between my code and the one I found from Google.
3rd Mar 2021, 8:46 PM
Yasswecancn
Yasswecancn - avatar
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
4th Mar 2021, 7:41 PM
Yasswecancn
Yasswecancn - avatar
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
4th Mar 2021, 7:53 PM
Yasswecancn
Yasswecancn - avatar