Count all the multiples of 3 and 5 in the initial n natural numbers
Here given code implementation process.
// C program for
// Count all the multiples of 3 and 5 in the initial n natural numbers
#include <stdio.h>
void multiplesOf3and5(int n)
{
int result = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
printf("\n Given n : %d", n);
// Display calculated result
printf("\n Result : %d", result);
}
int main(int argc, char const *argv[])
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
multiplesOf3and5(21);
return 0;
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
/*
Java Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
public class Multiple
{
public void multiplesOf3and5(int n)
{
int result = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
System.out.print("\n Given n : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
}
public static void main(String[] args)
{
Multiple task = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple
{
public: void multiplesOf3and5(int n)
{
int result = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
cout << "\n Given n : " << n;
// Display calculated result
cout << "\n Result : " << result;
}
};
int main()
{
Multiple *task = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task->multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task->multiplesOf3and5(21);
return 0;
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
// Include namespace system
using System;
/*
Csharp Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
public class Multiple
{
public void multiplesOf3and5(int n)
{
int result = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
Console.Write("\n Given n : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
Multiple task = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
package main
import "fmt"
/*
Go Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
type Multiple struct {}
func getMultiple() * Multiple {
var me *Multiple = &Multiple {}
return me
}
func(this Multiple) multiplesOf3and5(n int) {
var result int = 0
if n > 0 {
result = ((n / 3 + n / 5) - n / 15)
}
// Display given number
fmt.Print("\n Given n : ", n)
// Display calculated result
fmt.Print("\n Result : ", result)
}
func main() {
var task * Multiple = getMultiple()
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10)
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21)
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
<?php
/*
Php Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple
{
public function multiplesOf3and5($n)
{
$result = 0;
if ($n > 0)
{
$result = (((int)($n / 3) +
(int)($n / 5)) -
(int)($n / 15));
}
// Display given number
echo("\n Given n : ".$n);
// Display calculated result
echo("\n Result : ".$result);
}
}
function main()
{
$task = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
$task->multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
$task->multiplesOf3and5(21);
}
main();
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
/*
Node JS Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple
{
multiplesOf3and5(n)
{
var result = 0;
if (n > 0)
{
result = ((parseInt(n / 3) +
parseInt(n / 5)) -
parseInt(n / 15));
}
// Display given number
process.stdout.write("\n Given n : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
}
}
function main()
{
var task = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
main();
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
# Python 3 Program for
# Count all the multiples of 3 and 5 in the initial n natural numbers
class Multiple :
def multiplesOf3and5(self, n) :
result = 0
if (n > 0) :
result = ((int(n / 3) +
int(n / 5)) -
int(n / 15))
# Display given number
print("\n Given n : ", n, end = "")
# Display calculated result
print("\n Result : ", result, end = "")
def main() :
task = Multiple()
# Test A
# n = 10
# Range : [1,2,3,4,5,6,7,8,9,10]
# Multiples [3,5,6,9,10]
# Result = 5
task.multiplesOf3and5(10)
# Test B
# n = 21
# Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
# Multiples [3,5,6,9,10,12,15,18,20,21]
# Result = 10
task.multiplesOf3and5(21)
if __name__ == "__main__": main()
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
# Ruby Program for
# Count all the multiples of 3 and 5 in the initial n natural numbers
class Multiple
def multiplesOf3and5(n)
result = 0
if (n > 0)
result = ((n / 3 + n / 5) - n / 15)
end
# Display given number
print("\n Given n : ", n)
# Display calculated result
print("\n Result : ", result)
end
end
def main()
task = Multiple.new()
# Test A
# n = 10
# Range : [1,2,3,4,5,6,7,8,9,10]
# Multiples [3,5,6,9,10]
# Result = 5
task.multiplesOf3and5(10)
# Test B
# n = 21
# Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
# Multiples [3,5,6,9,10,12,15,18,20,21]
# Result = 10
task.multiplesOf3and5(21)
end
main()
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
/*
Scala Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple()
{
def multiplesOf3and5(n: Int): Unit = {
var result: Int = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
print("\n Given n : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiple = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
/*
Swift 4 Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple
{
func multiplesOf3and5(_ n: Int)
{
var result: Int = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
print("\n Given n : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
}
}
func main()
{
let task: Multiple = Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
main();
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
/*
Kotlin Program for
Count all the multiples of 3 and 5 in the initial n natural numbers
*/
class Multiple
{
fun multiplesOf3and5(n: Int): Unit
{
var result: Int = 0;
if (n > 0)
{
result = ((n / 3 + n / 5) - n / 15);
}
// Display given number
print("\n Given n : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiple = Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3,5,6,9,10]
// Result = 5
task.multiplesOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples [3,5,6,9,10,12,15,18,20,21]
// Result = 10
task.multiplesOf3and5(21);
}
Output
Given n : 10
Result : 5
Given n : 21
Result : 10
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment