Calculate the sum of all integers 1 through 50 that are divisible by 3 in C#.
Variables are defined in the first row. In the loop “if statement” checks the number is divisible by 3
1 2 3 4 5 6 7 8 9 10 11 12 | static void Main(string[] args) { int sum = 0; for (int i = 1; i <= 50; i++) { if(i%3==0)sum += i; } Console.WriteLine(sum); Console.ReadKey(); } |
You can find more similar examples of programming for this programming language in the site.