Count all x divisible number in a range

Here given code implementation process.

// C program
// Count all x divisible number in a range
#include <stdio.h>

// Count the divisible number in the given range
void count_divisible(int start, int last, int x)
{
	if (start > last)
	{
		//Change sequence
		count_divisible(last, start, x);
		return;
	}
	else
	{
		int num = start;
		int counter = 0;
		printf("\n Divisible by %d in range of [%d-%d] are : [", x, start, last);
		while (num <= last)
		{
			if (num % x == 0)
			{
				// When x, is divisible by num
				printf(" %d ", num);
				//When get divisible number then increase counter value
				counter++;
			}
			if (counter > 0)
			{
				//visit to next divisible number
				num += x;
			}
			else
			{
				num++;
			}
		}
		// Display calculated result
		printf("]\n Result : %d\n", counter);
	}
}
int main()
{
	//Test case
	int x = 4;
	count_divisible(2, 38, x);
	x = 3;
	count_divisible(1, 10, x);
	x = 13;
	count_divisible(21, 33, x);
	x = 7;
	count_divisible(7, 29, x);
	x = 2;
	count_divisible(0, 0, x);
	x = 6;
	count_divisible(50, 120, x);
	return 0;
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Java program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	public void count_divisible(int start, int last, int x)
	{
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			int num = start;
			int counter = 0;
			System.out.print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					System.out.print(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			System.out.print("]\n Result : " + counter + "\n");
		}
	}
	public static void main(String[] args)
	{
		DivisibleNumbers obj = new DivisibleNumbers();
		//Test case
		int x = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
//Include header file
#include <iostream>
using namespace std;

/*
  C++ program 
  Count all x divisible number in a range
*/

class DivisibleNumbers
{
	public:
		// Count the divisible number in the given range
		void count_divisible(int start, int last, int x)
		{
			if (start > last)
			{
				//Change sequence
				this->count_divisible(last, start, x);
				return;
			}
			else
			{
				int num = start;
				int counter = 0;
				cout << "\n Divisible by " << x << " in range of [" << start << "-" << last << "] are : [";
				while (num <= last)
				{
					if (num % x == 0)
					{
						// When x, is divisible by num
						cout << " " << num << " ";
						//When get divisible number then increase counter value
						counter++;
					}
					if (counter > 0)
					{
						//visit to next divisible number
						num += x;
					}
					else
					{
						num++;
					}
				}
				// Display calculated result
				cout << "]\n Result : " << counter << "\n";
			}
		}
};
int main()
{
	DivisibleNumbers obj = DivisibleNumbers();
	//Test case
	int x = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
	return 0;
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
//Include namespace system
using System;
/* 
  C# program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	public void count_divisible(int start, int last, int x)
	{
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			int num = start;
			int counter = 0;
			Console.Write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					Console.Write(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			Console.Write("]\n Result : " + counter + "\n");
		}
	}
	public static void Main(String[] args)
	{
		DivisibleNumbers obj = new DivisibleNumbers();
		//Test case
		int x = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
<?php

/* 
  Php program 
  Count all x divisible number in a range
*/

class DivisibleNumbers
{
	// Count the divisible number in the given range
	public	function count_divisible($start, $last, $x)
	{
		if ($start > $last)
		{
			//Change sequence
			$this->count_divisible($last, $start, $x);
			return;
		}
		else
		{
			$num = $start;
			$counter = 0;
			echo "\n Divisible by ". $x ." in range of [". $start ."-". $last ."] are : [";
			while ($num <= $last)
			{
				if ($num % $x == 0)
				{
					// When x, is divisible by num
					echo " ". $num ." ";
					//When get divisible number then increase counter value
					$counter++;
				}
				if ($counter > 0)
				{
					//visit to next divisible number
					$num += $x;
				}
				else
				{
					$num++;
				}
			}
			// Display calculated result
			echo "]\n Result : ". $counter ."\n";
		}
	}
}

function main()
{
	$obj = new DivisibleNumbers();
	//Test case
	$x = 4;
	$obj->count_divisible(2, 38, $x);
	$x = 3;
	$obj->count_divisible(1, 10, $x);
	$x = 13;
	$obj->count_divisible(21, 33, $x);
	$x = 7;
	$obj->count_divisible(7, 29, $x);
	$x = 2;
	$obj->count_divisible(0, 0, $x);
	$x = 6;
	$obj->count_divisible(50, 120, $x);
}
main();

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Node Js program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	count_divisible(start, last, x)
	{
		if (start > last)
		{
			//Change sequence
			this.count_divisible(last, start, x);
			return;
		}
		else
		{
			var num = start;
			var counter = 0;
			process.stdout.write("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					process.stdout.write(" " + num + " ");
					//When get divisible number then increase counter value
					counter++;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num++;
				}
			}
			// Display calculated result
			process.stdout.write("]\n Result : " + counter + "\n");
		}
	}
}

function main()
{
	var obj = new DivisibleNumbers();
	//Test case
	var x = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
}
main();

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
#   Python 3 program 
#   Count all x divisible number in a range

class DivisibleNumbers :
	#  Count the divisible number in the given range
	def count_divisible(self, start, last, x) :
		if (start > last) :
			# Change sequence
			self.count_divisible(last, start, x)
			return
		else :
			num = start
			counter = 0
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [", end = "")
			while (num <= last) :
				if (num % x == 0) :
					#  When x, is divisible by num
					print(" ", num ," ", end = "")
					# When get divisible number then increase counter value
					counter += 1
				
				if (counter > 0) :
					# visit to next divisible number
					num += x
				else :
					num += 1
				
			
			#  Display calculated result
			print("]\n Result : ", counter ,"\n", end = "")
		
	

def main() :
	obj = DivisibleNumbers()
	# Test case
	x = 4
	obj.count_divisible(2, 38, x)
	x = 3
	obj.count_divisible(1, 10, x)
	x = 13
	obj.count_divisible(21, 33, x)
	x = 7
	obj.count_divisible(7, 29, x)
	x = 2
	obj.count_divisible(0, 0, x)
	x = 6
	obj.count_divisible(50, 120, x)

if __name__ == "__main__": main()

Output

 Divisible by  4  in range of [ 2 - 38 ] are : [  4    8    12    16    20    24    28    32    36  ]
 Result :  9

 Divisible by  3  in range of [ 1 - 10 ] are : [  3    6    9  ]
 Result :  3

 Divisible by  13  in range of [ 21 - 33 ] are : [  26  ]
 Result :  1

 Divisible by  7  in range of [ 7 - 29 ] are : [  7    14    21    28  ]
 Result :  4

 Divisible by  2  in range of [ 0 - 0 ] are : [  0  ]
 Result :  1

 Divisible by  6  in range of [ 50 - 120 ] are : [  54    60    66    72    78    84    90    96    102    108    114    120  ]
 Result :  12
#   Ruby program 
#   Count all x divisible number in a range

class DivisibleNumbers 
	#  Count the divisible number in the given range
	def count_divisible(start, last, x) 
		if (start > last) 
			# Change sequence
			self.count_divisible(last, start, x)
			return
		else 
			num = start
			counter = 0
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"] are : [")
			while (num <= last) 
				if (num % x == 0) 
					#  When x, is divisible by num
					print(" ", num ," ")
					# When get divisible number then increase counter value
					counter += 1
				end

				if (counter > 0) 
					# visit to next divisible number
					num += x
				else 
					num += 1
				end

			end

			#  Display calculated result
			print("]\n Result : ", counter ,"\n")
		end

	end

end

def main() 
	obj = DivisibleNumbers.new()
	# Test case
	x = 4
	obj.count_divisible(2, 38, x)
	x = 3
	obj.count_divisible(1, 10, x)
	x = 13
	obj.count_divisible(21, 33, x)
	x = 7
	obj.count_divisible(7, 29, x)
	x = 2
	obj.count_divisible(0, 0, x)
	x = 6
	obj.count_divisible(50, 120, x)
end

main()

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Scala program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	def count_divisible(start: Int, last: Int, x: Int): Unit = {
		if (start > last)
		{
			//Change sequence
			count_divisible(last, start, x);
			return;
		}
		else
		{
			var num: Int = start;
			var counter: Int = 0;
			print("\n Divisible by " + x + " in range of [" + start + "-" + last + "] are : [");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					print(" " + num + " ");
					//When get divisible number then increase counter value
					counter += 1;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num += 1;
				}
			}
			// Display calculated result
			print("]\n Result : " + counter + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: DivisibleNumbers = new DivisibleNumbers();
		//Test case
		var x: Int = 4;
		obj.count_divisible(2, 38, x);
		x = 3;
		obj.count_divisible(1, 10, x);
		x = 13;
		obj.count_divisible(21, 33, x);
		x = 7;
		obj.count_divisible(7, 29, x);
		x = 2;
		obj.count_divisible(0, 0, x);
		x = 6;
		obj.count_divisible(50, 120, x);
	}
}

Output

 Divisible by 4 in range of [2-38] are : [ 4  8  12  16  20  24  28  32  36 ]
 Result : 9

 Divisible by 3 in range of [1-10] are : [ 3  6  9 ]
 Result : 3

 Divisible by 13 in range of [21-33] are : [ 26 ]
 Result : 1

 Divisible by 7 in range of [7-29] are : [ 7  14  21  28 ]
 Result : 4

 Divisible by 2 in range of [0-0] are : [ 0 ]
 Result : 1

 Divisible by 6 in range of [50-120] are : [ 54  60  66  72  78  84  90  96  102  108  114  120 ]
 Result : 12
/* 
  Swift 4 program 
  Count all x divisible number in a range
*/
class DivisibleNumbers
{
	// Count the divisible number in the given range
	func count_divisible(_ start: Int, _ last: Int, _ x: Int)
	{
		if (start > last)
		{
			//Change sequence
			self.count_divisible(last, start, x);
			return;
		}
		else
		{
			var num: Int = start;
			var counter: Int = 0;
			print("\n Divisible by ", x ," in range of [", start ,"-", last ,"]are : [", terminator: "");
			while (num <= last)
			{
				if (num % x == 0)
				{
					// When x, is divisible by num
					print(" ", num ," ", terminator: "");
					//When get divisible number then increase counter value
					counter += 1;
				}
				if (counter > 0)
				{
					//visit to next divisible number
					num += x;
				}
				else
				{
					num += 1;
				}
			}
			// Display calculated result
			print("]\n Result : ", counter ,"\n", terminator: "");
		}
	}
}
func main()
{
	let obj: DivisibleNumbers = DivisibleNumbers();
	//Test case
	var x: Int = 4;
	obj.count_divisible(2, 38, x);
	x = 3;
	obj.count_divisible(1, 10, x);
	x = 13;
	obj.count_divisible(21, 33, x);
	x = 7;
	obj.count_divisible(7, 29, x);
	x = 2;
	obj.count_divisible(0, 0, x);
	x = 6;
	obj.count_divisible(50, 120, x);
}
main();

Output

 Divisible by  4  in range of [ 2 - 38 ]are : [  4    8    12    16    20    24    28    32    36  ]
 Result :  9

 Divisible by  3  in range of [ 1 - 10 ]are : [  3    6    9  ]
 Result :  3

 Divisible by  13  in range of [ 21 - 33 ]are : [  26  ]
 Result :  1

 Divisible by  7  in range of [ 7 - 29 ]are : [  7    14    21    28  ]
 Result :  4

 Divisible by  2  in range of [ 0 - 0 ]are : [  0  ]
 Result :  1

 Divisible by  6  in range of [ 50 - 120 ]are : [  54    60    66    72    78    84    90    96    102    108    114    120  ]
 Result :  12


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







© 2021, kalkicode.com, All rights reserved