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.)
Example C# Exercise
Show C# Code
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
}
}
}
}
Output
15
30
45
60
75
90
105
120
135
150
165
180
195
210
225
240
255
270
285
300
315
330
345
360
375
390
405
420
435
450
465
480
495
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#.
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...
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 ...
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...
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...
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 ...
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...