Posted on by Kalkicode
Code Mathematics

Check divisibility by 7 without modulus operator

This problem is solved in two ways, one basic method uses addition and subtraction operations.

// C program for 
// Check divisibility by 7 without modulus operator
#include <stdio.h>

// This is check that given number is divisible by 7 or not
void divisibleBy7(int number)
{
	int num = number;
	// Execute the loop until, 
	// When number is outside the range of (-7 to 7)
	while (num <= -7 || num >= 7)
	{
		if (num <= -7)
		{
			// When number is negative and less than -6
			num += 7;
		}
		else
		{
			// Reduce the number by -7
			num -= 7;
		}
	}
	if (num == 0)
	{
		// When it's divisible by 7
		printf(" Number %d is divisible by 7\n", number);
	}
	else
	{
		// When it's not divisible by 7
		printf(" Number %d is not divisible by 7\n", number);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Case
	divisibleBy7(-21);
	divisibleBy7(-26);
	divisibleBy7(21);
	divisibleBy7(326);
	divisibleBy7(7315);
	return 0;
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Java Program for
  Check divisibility by 7 without modulus operator
*/
public class Divisibility
{
	// This is check that given number is divisible by 7 or not
	public void divisibleBy7(int number)
	{
		int num = number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			System.out.println(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			System.out.println(" Number " + number + " is not divisible by 7");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Case
		task.divisibleBy7(-21);
		task.divisibleBy7(-26);
		task.divisibleBy7(21);
		task.divisibleBy7(326);
		task.divisibleBy7(7315);
	}
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Check divisibility by 7 without modulus operator
*/

class Divisibility
{
	public:
		// This is check that given number is divisible by 7 or not
		void divisibleBy7(int number)
		{
			int num = number;
			// Execute the loop until, 
			// When number is outside the range of (-7 to 7)
			while (num <= -7 || num >= 7)
			{
				if (num <= -7)
				{
					// When number is negative and less than -6
					num += 7;
				}
				else
				{
					// Reduce the number by -7
					num -= 7;
				}
			}
			if (num == 0)
			{
				// When it's divisible by 7
				cout << " Number " << number << " is divisible by 7" << endl;
			}
			else
			{
				// When it's not divisible by 7
				cout << " Number " << number << " is not divisible by 7" << endl;
			}
		}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test Case
	task->divisibleBy7(-21);
	task->divisibleBy7(-26);
	task->divisibleBy7(21);
	task->divisibleBy7(326);
	task->divisibleBy7(7315);
	return 0;
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
// Include namespace system
using System;
/*
  Csharp Program for
  Check divisibility by 7 without modulus operator
*/
public class Divisibility
{
	// This is check that given number is divisible by 7 or not
	public void divisibleBy7(int number)
	{
		int num = number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			Console.WriteLine(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			Console.WriteLine(" Number " + number + " is not divisible by 7");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Case
		task.divisibleBy7(-21);
		task.divisibleBy7(-26);
		task.divisibleBy7(21);
		task.divisibleBy7(326);
		task.divisibleBy7(7315);
	}
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
<?php
/*
  Php Program for
  Check divisibility by 7 without modulus operator
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	public	function divisibleBy7($number)
	{
		$num = $number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while ($num <= -7 || $num >= 7)
		{
			if ($num <= -7)
			{
				// When number is negative and less than -6
				$num += 7;
			}
			else
			{
				// Reduce the number by -7
				$num -= 7;
			}
		}
		if ($num == 0)
		{
			// When it's divisible by 7
			echo " Number ".$number.
			" is divisible by 7".
			"\n";
		}
		else
		{
			// When it's not divisible by 7
			echo " Number ".$number.
			" is not divisible by 7".
			"\n";
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test Case
	$task->divisibleBy7(-21);
	$task->divisibleBy7(-26);
	$task->divisibleBy7(21);
	$task->divisibleBy7(326);
	$task->divisibleBy7(7315);
}
main();

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Node JS Program for
  Check divisibility by 7 without modulus operator
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	divisibleBy7(number)
	{
		var num = number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			console.log(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			console.log(" Number " + number + " is not divisible by 7");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}
main();

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
#  Python 3 Program for
#  Check divisibility by 7 without modulus operator
class Divisibility :
	#  This is check that given number is divisible by 7 or not
	def divisibleBy7(self, number) :
		num = number
		#  Execute the loop until, 
		#  When number is outside the range of (-7 to 7)
		while (num <= -7 or num >= 7) :
			if (num <= -7) :
				#  When number is negative and less than -6
				num += 7
			else :
				#  Reduce the number by -7
				num -= 7
			
		
		if (num == 0) :
			#  When it's divisible by 7
			print(" Number ", number ," is divisible by 7")
		else :
			#  When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7")
		
	

def main() :
	task = Divisibility()
	#  Test Case
	task.divisibleBy7(-21)
	task.divisibleBy7(-26)
	task.divisibleBy7(21)
	task.divisibleBy7(326)
	task.divisibleBy7(7315)

if __name__ == "__main__": main()

input

 Number  -21  is divisible by 7
 Number  -26  is not divisible by 7
 Number  21  is divisible by 7
 Number  326  is not divisible by 7
 Number  7315  is divisible by 7
#  Ruby Program for
#  Check divisibility by 7 without modulus operator
class Divisibility 
	#  This is check that given number is divisible by 7 or not
	def divisibleBy7(number) 
		num = number
		#  Execute the loop until, 
		#  When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7) 
			if (num <= -7) 
				#  When number is negative and less than -6
				num += 7
			else 
				#  Reduce the number by -7
				num -= 7
			end

		end

		if (num == 0) 
			#  When it's divisible by 7
			print(" Number ", number ," is divisible by 7", "\n")
		else 
			#  When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7", "\n")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test Case
	task.divisibleBy7(-21)
	task.divisibleBy7(-26)
	task.divisibleBy7(21)
	task.divisibleBy7(326)
	task.divisibleBy7(7315)
end

main()

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Scala Program for
  Check divisibility by 7 without modulus operator
*/
class Divisibility()
{
	// This is check that given number is divisible by 7 or not
	def divisibleBy7(number: Int): Unit = {
		var num: Int = number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			println(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			println(" Number " + number + " is not divisible by 7");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test Case
		task.divisibleBy7(-21);
		task.divisibleBy7(-26);
		task.divisibleBy7(21);
		task.divisibleBy7(326);
		task.divisibleBy7(7315);
	}
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Swift 4 Program for
  Check divisibility by 7 without modulus operator
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	func divisibleBy7(_ number: Int)
	{
		var num: Int = number;
		// Execute the loop until, 
		// When number is outside the range of (-7 to 7)
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			print(" Number ", number ," is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}
main();

input

 Number  -21  is divisible by 7
 Number  -26  is not divisible by 7
 Number  21  is divisible by 7
 Number  326  is not divisible by 7
 Number  7315  is divisible by 7
/*
  Kotlin Program for
  Check divisibility by 7 without modulus operator
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	fun divisibleBy7(number: Int): Unit
	{
		var num: Int = number;
		while (num <= -7 || num >= 7)
		{
			if (num <= -7)
			{
				// When number is negative and less than -6
				num += 7;
			}
			else
			{
				// Reduce the number by -7
				num -= 7;
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			println(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			println(" Number " + number + " is not divisible by 7");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7

Time complexity of above program is n. We can reduce this complexity. See next solution.

The problem of checking whether a number is divisible by 7 without using the modulus operator can be challenging but can be solved using a simple algorithm.

The modulus operator (%) returns the remainder when one number is divided by another. For example, 10 % 3 = 1, which means that 10 is not divisible by 3.

To check divisibility by 7, we can use the following algorithm:

  1. Start with the rightmost digit of the number.
  2. Double the digit and subtract it from the remaining digits.
  3. Repeat steps 1 and 2 with the new number.
  4. If the result is divisible by 7, then the original number is also divisible by 7.

For example, let's check whether the number 378 is divisible by 7:

  1. Start with the rightmost digit: 8
  2. Double the digit and subtract it from the remaining digits: 37 - 2 * 8 = 21
  3. Repeat steps 1 and 2 with the new number: 1
  4. 1 is not divisible by 7, so 378 is not divisible by 7.

This algorithm works because multiplying a number by 2 is equivalent to shifting its binary representation one bit to the left. Therefore, subtracting the doubled digit is equivalent to subtracting a multiple of 7. If the resulting number is divisible by 7, then the original number is also divisible by 7.

// C program for 
// Check divisibility by 7 without modulus operator
// Efficient solution
#include <stdio.h>

// This is check that given number is divisible by 7 or not
void divisibleBy7(int number)
{
	int num = number;
	if (num < 0)
	{
		// When number is negative then change in positive
		num = -num;
	}
	// When num is greater than 6 continue execute loop
	while (num >= 7)
	{
		if (num < 10)
		{
			// Reduce of the number by -7
			num = num - 7;
		}
		else
		{
			// (num / 10) Remove last digit of a number
			// ( num - num / 10 *10 ) get last digit (same as num % 10)
			num = (num / 10) - (2 *(num - num / 10 *10));
		}
	}
	if (num == 0)
	{
		// When it's divisible by 7
		printf(" Number %d is divisible by 7\n", number);
	}
	else
	{
		// When it's not divisible by 7
		printf(" Number %d is not divisible by 7\n", number);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Case
	divisibleBy7(-21);
	divisibleBy7(-26);
	divisibleBy7(21);
	divisibleBy7(326);
	divisibleBy7(7315);
	return 0;
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Java Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/

public class Divisibility
{
    // This is check that given number is divisible by 7 or not
public void divisibleBy7(int number)
{
    int num = number;


    if(num < 0)
    {
        // When number is negative then change in positive
        num = -num;
    }
    // When num is greater than 6 continue execute loop
    while ( num >= 7)
    {
       if(num < 10)
       {
            // Reduce of the number by -7
            num = num - 7;
       }
       else
       {
           // (num / 10) Remove last digit of a number
           // ( num - num / 10 * 10 ) get last digit (same as num % 10)
           num =  (num / 10) - (2 * ( num - num / 10 * 10 ));
       }
    }
    if (num == 0)
    {
        // When it's divisible by 7
        System.out.println(" Number " + number + " is divisible by 7");
    }
    else
    {
        // When it's not divisible by 7
        System.out.println(" Number " + number + " is not divisible by 7");
    }
}
    public static void main(String[] args)
    {
        Divisibility task = new Divisibility();
     
        // Test Case
        task.divisibleBy7(-21);
        task.divisibleBy7(-26);
        task.divisibleBy7(21);
        task.divisibleBy7(326);
        task.divisibleBy7(7315);
    
    }
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/

class Divisibility
{
	public:
		// This is check that given number is divisible by 7 or not
		void divisibleBy7(int number)
		{
			int num = number;
			if (num < 0)
			{
				// When number is negative then change in positive
				num = -num;
			}
			// When num is greater than 6 continue execute loop
			while (num >= 7)
			{
				if (num < 10)
				{
					// Reduce of the number by -7
					num = num - 7;
				}
				else
				{
					// (num / 10) Remove last digit of a number
					// ( num - num / 10 *10 ) get last digit (same as num % 10)
					num = (num / 10) - (2 *(num - num / 10 *10));
				}
			}
			if (num == 0)
			{
				// When it's divisible by 7
				cout << " Number " << number << " is divisible by 7" << endl;
			}
			else
			{
				// When it's not divisible by 7
				cout << " Number " << number << " is not divisible by 7" << endl;
			}
		}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test Case
	task->divisibleBy7(-21);
	task->divisibleBy7(-26);
	task->divisibleBy7(21);
	task->divisibleBy7(326);
	task->divisibleBy7(7315);
	return 0;
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
// Include namespace system
using System;
/*
  Csharp Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
public class Divisibility
{
	// This is check that given number is divisible by 7 or not
	public void divisibleBy7(int number)
	{
		int num = number;
		if (num < 0)
		{
			// When number is negative then change in positive
			num = -num;
		}
		// When num is greater than 6 continue execute loop
		while (num >= 7)
		{
			if (num < 10)
			{
				// Reduce of the number by -7
				num = num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (num / 10) - (2 * (num - num / 10 * 10));
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			Console.WriteLine(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			Console.WriteLine(" Number " + number + " is not divisible by 7");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Case
		task.divisibleBy7(-21);
		task.divisibleBy7(-26);
		task.divisibleBy7(21);
		task.divisibleBy7(326);
		task.divisibleBy7(7315);
	}
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
<?php
/*
  Php Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	public	function divisibleBy7($number)
	{
		$num = $number;
		if ($num < 0)
		{
			// When number is negative then change in positive
			$num = -$num;
		}
		// When num is greater than 6 continue execute loop
		while ($num >= 7)
		{
			if ($num < 10)
			{
				// Reduce of the number by -7
				$num = $num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				$num = ((int)($num / 10)) - (2 * ($num - (int)($num / 10) * 10));
			}
		}
		if ($num == 0)
		{
			// When it's divisible by 7
			echo " Number ".$number.
			" is divisible by 7".
			"\n";
		}
		else
		{
			// When it's not divisible by 7
			echo " Number ".$number.
			" is not divisible by 7".
			"\n";
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test Case
	$task->divisibleBy7(-21);
	$task->divisibleBy7(-26);
	$task->divisibleBy7(21);
	$task->divisibleBy7(326);
	$task->divisibleBy7(7315);
}
main();

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Node JS Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	divisibleBy7(number)
	{
		var num = number;
		if (num < 0)
		{
			// When number is negative then change in positive
			num = -num;
		}
		// When num is greater than 6 continue execute loop
		while (num >= 7)
		{
			if (num < 10)
			{
				// Reduce of the number by -7
				num = num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (parseInt(num / 10)) - (2 * (num - parseInt(num / 10) * 10));
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			console.log(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			console.log(" Number " + number + " is not divisible by 7");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}
main();

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
#  Python 3 Program for
#  Check divisibility by 7 without modulus operator
#  Efficient solution

class Divisibility :
	#  This is check that given number is divisible by 7 or not
	def divisibleBy7(self, number) :
		num = number
		if (num < 0) :
			#  When number is negative then change in positive
			num = -num
		
		#  When num is greater than 6 continue execute loop
		while (num >= 7) :
			if (num < 10) :
				#  Reduce of the number by -7
				num = num - 7
			else :
				#  (num / 10) Remove last digit of a number
				#  ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (int(num / 10)) - (2 * (num - int(num / 10) * 10))
			
		
		if (num == 0) :
			#  When it's divisible by 7
			print(" Number ", number ," is divisible by 7")
		else :
			#  When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7")
		
	

def main() :
	task = Divisibility()
	#  Test Case
	task.divisibleBy7(-21)
	task.divisibleBy7(-26)
	task.divisibleBy7(21)
	task.divisibleBy7(326)
	task.divisibleBy7(7315)

if __name__ == "__main__": main()

input

 Number  -21  is divisible by 7
 Number  -26  is not divisible by 7
 Number  21  is divisible by 7
 Number  326  is not divisible by 7
 Number  7315  is divisible by 7
#  Ruby Program for
#  Check divisibility by 7 without modulus operator
#  Efficient solution

class Divisibility 
	#  This is check that given number is divisible by 7 or not
	def divisibleBy7(number) 
		num = number
		if (num < 0) 
			#  When number is negative then change in positive
			num = -num
		end

		#  When num is greater than 6 continue execute loop
		while (num >= 7) 
			if (num < 10) 
				#  Reduce of the number by -7
				num = num - 7
			else 
				#  (num / 10) Remove last digit of a number
				#  ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (num / 10) - (2 * (num - num / 10 * 10))
			end

		end

		if (num == 0) 
			#  When it's divisible by 7
			print(" Number ", number ," is divisible by 7", "\n")
		else 
			#  When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7", "\n")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test Case
	task.divisibleBy7(-21)
	task.divisibleBy7(-26)
	task.divisibleBy7(21)
	task.divisibleBy7(326)
	task.divisibleBy7(7315)
end

main()

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Scala Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
class Divisibility()
{
	// This is check that given number is divisible by 7 or not
	def divisibleBy7(number: Int): Unit = {
		var num: Int = number;
		if (num < 0)
		{
			// When number is negative then change in positive
			num = -num;
		}
		// When num is greater than 6 continue execute loop
		while (num >= 7)
		{
			if (num < 10)
			{
				// Reduce of the number by -7
				num = num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = ((num / 10).toInt) - (2 * (num - (num / 10).toInt * 10));
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			println(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			println(" Number " + number + " is not divisible by 7");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test Case
		task.divisibleBy7(-21);
		task.divisibleBy7(-26);
		task.divisibleBy7(21);
		task.divisibleBy7(326);
		task.divisibleBy7(7315);
	}
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7
/*
  Swift 4 Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	func divisibleBy7(_ number: Int)
	{
		var num: Int = number;
		if (num < 0)
		{
			// When number is negative then change in positive
			num = -num;
		}
		// When num is greater than 6 continue execute loop
		while (num >= 7)
		{
			if (num < 10)
			{
				// Reduce of the number by -7
				num = num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (num / 10) - (2 * (num - num / 10 * 10));
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			print(" Number ", number ," is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			print(" Number ", number ," is not divisible by 7");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}
main();

input

 Number  -21  is divisible by 7
 Number  -26  is not divisible by 7
 Number  21  is divisible by 7
 Number  326  is not divisible by 7
 Number  7315  is divisible by 7
/*
  Kotlin Program for
  Check divisibility by 7 without modulus operator
  Efficient solution
*/
class Divisibility
{
	// This is check that given number is divisible by 7 or not
	fun divisibleBy7(number: Int): Unit
	{
		var num: Int = number;
		if (num < 0)
		{
			// When number is negative then change in positive
			num = -num;
		}
		while (num >= 7)
		{
			if (num < 10)
			{
				// Reduce of the number by -7
				num = num - 7;
			}
			else
			{
				// (num / 10) Remove last digit of a number
				// ( num - num / 10 * 10 ) get last digit (same as num % 10)
				num = (num / 10) - (2 * (num - num / 10 * 10));
			}
		}
		if (num == 0)
		{
			// When it's divisible by 7
			println(" Number " + number + " is divisible by 7");
		}
		else
		{
			// When it's not divisible by 7
			println(" Number " + number + " is not divisible by 7");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test Case
	task.divisibleBy7(-21);
	task.divisibleBy7(-26);
	task.divisibleBy7(21);
	task.divisibleBy7(326);
	task.divisibleBy7(7315);
}

input

 Number -21 is divisible by 7
 Number -26 is not divisible by 7
 Number 21 is divisible by 7
 Number 326 is not divisible by 7
 Number 7315 is divisible by 7

Comment

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