Find the sum of all integer between 1 and N that are divisible by 3.
Variables are defined in the first row. In the loop “if statement” checks the number is divisible by 3
In the last line we print the result of processing on the screen.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
static void Main(string[] args) { int sum = 0; Console.Write("Number : "); int n = Convert.ToInt32(Console.ReadLine()); for (int i = 1; i < n; i++) { if(i%3==0) { sum += i; } } Console.WriteLine("Sum : "+sum); Console.ReadKey(); } |
Output: