C# program to print all the numbers divisible by 3 and 5 for a given number

To print numbers divisible by both 3 and 5, we use the modulus operator % with the logical AND operator &&. A number is divisible by both 3 and 5 if the remainder is zero when divided by each of these numbers.

Syntax

Following is the syntax to check if a number is divisible by both 3 and 5 −

if (num % 3 == 0 && num % 5 == 0) {
   // number is divisible by both 3 and 5
}

Alternatively, since a number divisible by both 3 and 5 is divisible by their least common multiple (15), we can use −

if (num % 15 == 0) {
   // number is divisible by both 3 and 5
}

Using Modulus Operator with AND Logic

Example

using System;

class Program {
   static void Main() {
      int num = 15;
      
      Console.WriteLine("Number: " + num);
      
      // checking if the number is divisible by 3 and 5
      if (num % 3 == 0 && num % 5 == 0) {
         Console.WriteLine("Divisible by 3 and 5");
      } else {
         Console.WriteLine("Not divisible by 3 and 5");
      }
      
      // Test with another number
      num = 20;
      Console.WriteLine("\nNumber: " + num);
      if (num % 3 == 0 && num % 5 == 0) {
         Console.WriteLine("Divisible by 3 and 5");
      } else {
         Console.WriteLine("Not divisible by 3 and 5");
      }
   }
}

The output of the above code is −

Number: 15
Divisible by 3 and 5

Number: 20
Not divisible by 3 and 5

Finding All Numbers Divisible by 3 and 5 in a Range

Example

using System;

class Program {
   static void Main() {
      int limit = 50;
      
      Console.WriteLine("Numbers divisible by both 3 and 5 up to " + limit + ":");
      
      for (int i = 1; i <= limit; i++) {
         if (i % 3 == 0 && i % 5 == 0) {
            Console.Write(i + " ");
         }
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Numbers divisible by both 3 and 5 up to 50:
15 30 45

Using LCM Method

Since numbers divisible by both 3 and 5 are also divisible by 15 (LCM of 3 and 5), we can simplify the condition −

Example

using System;

class Program {
   static void Main() {
      int limit = 100;
      
      Console.WriteLine("Numbers divisible by both 3 and 5 up to " + limit + " (using LCM method):");
      
      for (int i = 15; i <= limit; i += 15) {
         Console.Write(i + " ");
      }
      Console.WriteLine();
      
      // Alternative approach using modulus
      Console.WriteLine("\nUsing modulus with 15:");
      for (int i = 1; i <= limit; i++) {
         if (i % 15 == 0) {
            Console.Write(i + " ");
         }
      }
      Console.WriteLine();
   }
}

The output of the above code is −

Numbers divisible by both 3 and 5 up to 100 (using LCM method):
15 30 45 60 75 90

Using modulus with 15:
15 30 45 60 75 90

Conclusion

To find numbers divisible by both 3 and 5, use num % 3 == 0 && num % 5 == 0 or the more efficient num % 15 == 0. The latter approach leverages the fact that numbers divisible by both 3 and 5 are divisible by their least common multiple, which is 15.

Updated on: 2026-03-17T07:04:35+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements