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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.