Multiples - C# Programming Exercise

In this exercise, you will learn how to use the modulo operator % to find numbers between 1 and 500 that are divisible by both 3 and 5. The program will check each number in this range and display only those that are common multiples of 3 and 5. Exercises like this are great for understanding how conditions and loops work in programming, as well as being an excellent way to practice number manipulation and flow control with loops and conditionals. Practice this exercise to improve your skills in C#!

 Category

Flow Control

 Exercise

Multiples

 Objective

Write a C# program to display on the screen the numbers from 1 to 500 that are multiples of both 3 and 5. (Hint: Use the modulo operator to check for divisibility by both 3 and 5.)

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        // Using a for loop to iterate through numbers from 1 to 500
        for (int i = 1; i <= 500; i++) // Looping through numbers 1 to 500
        {
            // Checking if the current number is divisible by both 3 and 5
            if (i % 3 == 0 && i % 5 == 0) // If the remainder of i divided by 3 and 5 is zero
            {
                // Displaying the number if it is divisible by both 3 and 5
                Console.WriteLine(i); // Printing the number on the screen
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Number repeated

    This exercise teaches you how to display a number repeated a number of times specified by the user. Three loop structures will be used to perform the repetition: while, d...

  •  Password

    In this C# programming exercise, the user is asked to enter their login and password, both as integer numbers. The goal is to create a program that repeatedly ...

  •  Password V2

    This C# exercise aims to create a program that asks the user for their login and password (both must be integer numbers) and repeats the request until the ente...

  •  Many divisions

    This C# exercise aims to develop a program that asks the user for two numbers and displays the result of the division of these numbers along with the remainder of the divisi...

  •  Several multiplication tables, (use do while)

    This C# exercise aims to create a program that displays multiplication tables from 2 to 6 using nested do...while loops. A do...while loop executes a block of ...

  •  Square

    This C# exercise aims to create a program that prompts the user to enter a number and a width, then displays a square with the given width, using...