0

Need Help. Why it can't Print Exact Output (in C#)?

/* Write a C# program to compute the sum of the first 500 prime numbers. Go to the editor Output: 3682913 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication8 { class Program { static void Main(string[] args) { int num, i,l; int sum = 0; l = 0; for (num = 1; ;num++ ) { int k = 0; for (i = 1; i <= num; i++) { if (num % i == 0) { k++; } } if (k == 2) { Console.WriteLine("Number is {0}", num); sum += num; l++; if (l == 500) { Console.WriteLine("Sum is {0}", sum); break; } } else { } } Console.WriteLine("Sum is {0}", sum); Console.WriteLine("Total Number is {0}", l); } } }

5th May 2018, 2:17 AM
Abir Hossain
Abir Hossain - avatar
2 odpowiedzi
0
Are you running this on SL or locally on your machine?
21st Jun 2019, 8:53 AM
SQrL
SQrL - avatar
0
This looks to cause an infinite loop for (num = 1; ;num++ ) This seems to be limiting your sum calculations if (k == 2) {
21st Jun 2019, 8:59 AM
SQrL
SQrL - avatar