Multiples of 3 and 5 without using % operator
Write a short program that prints each number from 1 to n on a new line.
- For each multiple of 3, print “Multiple of 3” instead of the number.
- For each multiple of 5, print “Multiple of 5” instead of the number.
- For numbers which are multiples of both 3 and 5, print “Multiple of 3. Multiple of 5.” instead of the number.
Examples:
Input : 15
Output : 1
2
Multiple of 3.
4
Multiple of 5.
Multiple of 3.
7
8
Multiple of 3.
Multiple of 5.
11
Multiple of 3.
13
14
Multiple of 3. Multiple of 5.
The idea is iterate from 1 to n and keep track of multiples of 3 and 5 by adding 3 and 5 to current multiple. If current number matches with a multiple, we update our output accordingly.
C++
#include <iostream>
using namespace std;
void findMultiples( int n)
{
int a = 3;
int b = 5;
for ( int i = 1; i <= n; i++)
{
string s = "" ;
if (i == a)
{
a = a + 3;
s = s + "Multiple of 3. " ;
}
if (i == b)
{
b = b + 5;
s = s + "Multiple of 5." ;
}
if (s == "" )
cout << (i) << endl;
else
cout << (s) << endl;
}
}
int main()
{
findMultiples(20);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findMultiples( int n)
{
int a = 3 ;
int b = 5 ;
for ( int i= 1 ; i<=n; i++)
{
String s = "" ;
if (i==a)
{
a = a + 3 ;
s = s + "Multiple of 3. " ;
}
if (i==b)
{
b = b+ 5 ;
s = s + "Multiple of 5." ;
}
if (s == "" )
System.out.println(i);
else System.out.println(s);
}
}
public static void main (String[] args)
{
findMultiples( 20 );
}
}
|
C#
using System;
public class GFG {
static void findMultiples( int n)
{
int a = 3;
int b = 5;
for ( int i = 1; i <= n; i++)
{
String s = "" ;
if (i == a)
{
a = a + 3;
s = s + "Multiple of 3. " ;
}
if (i == b)
{
b = b + 5;
s = s + "Multiple of 5." ;
}
if (s == "" )
Console.WriteLine(i);
else
Console.WriteLine(s);
}
}
public static void Main ()
{
findMultiples(20);
}
}
|
PHP
<?php
function findMultiples( $n )
{
$a = 3;
$b = 5;
for ( $i = 1; $i <= $n ; $i ++)
{
$s = "" ;
if ( $i == $a )
{
$a = $a + 3;
$s = $s . "Multiple of 3. " ;
}
if ( $i == $b )
{
$b = $b + 5;
$s = $s . "Multiple of 5." ;
}
if ( $s == "" )
echo ( $i ). "\n" ;
else
echo ( $s ). "\n" ;
}
}
findMultiples(20);
|
Javascript
<script>
function findMultiples(n)
{
let a = 3;
let b = 5;
for (let i=1; i<=n; i++)
{
let s = "" ;
if (i==a)
{
a = a + 3;
s = s + "Multiple of 3. " ;
}
if (i==b)
{
b = b+5;
s = s + "Multiple of 5." ;
}
if (s == "" ) {
document.write(i);
document.write( "<br />" );
}
else {
document.write(s);
document.write( "<br />" );
}
}
}
findMultiples(20);
</script>
|
Python3
def findMultiples(n):
a = 3
b = 5
for i in range ( 1 ,n + 1 ):
s = ""
if (i = = a):
a = a + 3
s = s + "Multiple of 3. "
if (i = = b):
b = b + 5
s = s + "Multiple of 5."
if (s = = ""):
print (i)
else :
print (s)
if __name__ = = '__main__' :
findMultiples( 20 )
|
Output:
1
2
Multiple of 3.
4
Multiple of 5.
Multiple of 3.
7
8
Multiple of 3.
Multiple of 5.
11
Multiple of 3.
13
14
Multiple of 3. Multiple of 5.
16
17
Multiple of 3.
19
Multiple of 5.
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...