C# Program to Find the Sum of All the Multiples of 3 and 5
Posted by Superadmin on August 10 2022 14:27:26

C# Program to Find the Sum of All the Multiples of 3 and 5

 

 

This is a C# Program to print the sum of all the multiples of 3 and 5.

Problem Description

This C# Program Prints the Sum of all the Multiples of 3 and 5.

Problem Solution

Here the multiples of 3 and 5 are found and the sum of all the multiples are calculated and are displayed.

Program/Source Code

Here is source code of the C# Program to Print the Sum of all the Multiples of 3 and 5. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *C# Program to Print the Sum of all the Multiples of 3 and 5
 */
using System;
class program
{
    public static void Main()
    {
        int a, b, i, Sum = 0;
        for (i = 1; i < 100; i++)
        {
            a = i % 3;
            b = i % 5;
            if (a == 0 || b == 0)
            {
                Console.Write("{0}\t", i);
                Sum = Sum + i;
            }
        }
        Console.WriteLine("\nThe Sum of all the Multiples of 3 or 5 Below 100 : {0}", 
                          Sum);
        Console.Read();
    }
}
Program Explanation

In this C# program, using for loop compute the multiples of 3 and 5 from 1 to 100. Initialize the value of ‘i’ variable as 1 and check the condition that the value of ‘i’ variable is less than 100. If the condition is true then execute the statement. Compute the modulus of the value of ‘i’ variable by 3 and the modulus of the value of ‘i’ variable by 5.

 

If condition statement is used to check the value of ‘a’ variable or the value of ‘b’ variable is equal to 0 using Logical OR Operator. If the condition is true, then execute the statement and compute the sum of all the multiples and print the values.

Runtime Test Cases
 
3  5  6  9  10  12  15  18  20  21  24  25  27  30  33  35  36  
39  40  42  45  48 50  51  54  55  57  60  63  65  66  69  70  72  
75  78  80  81  84  85  87  90  93  95  96  99
The Sum of all the Multiples of 3 or 5 Below 100 : 2318