Check that if large number is divisible by 9

Here given code implementation process.

// Java program for
// Check that if large number is divisible by 9
public class Divisibility
{
  public void isDivisibleBy9(String num)
  {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    boolean result = false;
    int n = num.length();
    if (n == 1 && num.charAt(0)=='0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      int sum = 0;
      // Calculate sum of all digit
      for (int i = 0; i < n; ++i)
      {
        sum += (num.charAt(i) - 48);
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      System.out.println(" Given number (" + 
                               num + ") is divisible by 9");
    }
    else
    {
      System.out.println(" Given number (" + 
                               num + ") is not divisible by 9");
    }
  }
  public static void main(String[] args)
  {
    Divisibility task = new Divisibility();
    // Test
    task.isDivisibleBy9("234234125456784564");
    task.isDivisibleBy9("67344523423242342342363455544");
    task.isDivisibleBy9("91999");
    task.isDivisibleBy9("97654218341234234234112");
  }
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 9
class Divisibility
{
  public: void isDivisibleBy9(string num)
  {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    bool result = false;
    int n = num.length();
    if (n == 1 && num[0] == '0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      int sum = 0;
      // Calculate sum of all digit
      for (int i = 0; i < n; ++i)
      {
        sum += (num[i] - 48);
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      cout << " Given number (" 
                 << num << ") is divisible by 9" << endl;
    }
    else
    {
      cout << " Given number (" 
                 << num << ") is not divisible by 9" << endl;
    }
  }
};
int main()
{
  Divisibility *task = new Divisibility();
  // Test
  task->isDivisibleBy9("234234125456784564");
  task->isDivisibleBy9("67344523423242342342363455544");
  task->isDivisibleBy9("91999");
  task->isDivisibleBy9("97654218341234234234112");
  return 0;
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 9
public class Divisibility
{
  public void isDivisibleBy9(String num)
  {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    Boolean result = false;
    int n = num.Length;
    if (n == 1 && num[0] == '0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      int sum = 0;
      // Calculate sum of all digit
      for (int i = 0; i < n; ++i)
      {
        sum += (num[i] - 48);
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      Console.WriteLine(" Given number (" + 
                              num + ") is divisible by 9");
    }
    else
    {
      Console.WriteLine(" Given number (" + 
                              num + ") is not divisible by 9");
    }
  }
  public static void Main(String[] args)
  {
    Divisibility task = new Divisibility();
    // Test
    task.isDivisibleBy9("234234125456784564");
    task.isDivisibleBy9("67344523423242342342363455544");
    task.isDivisibleBy9("91999");
    task.isDivisibleBy9("97654218341234234234112");
  }
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
package main

import "fmt"
// Go program for
// Check that if large number is divisible by 9

func isDivisibleBy9(num string) {
  // Condition
  // ➀ Sum the digits. And the result must be divisible by 9.
  var result bool = false
  var n int = len(num)
  if n == 1 && num[0] == '0' {
    // When number is zero
    result = true
  } else if n > 0 {
    var sum int = 0
    // Calculate sum of all digit
    for i := 0 ; i < n ; i++ {
      sum += ( int(num[i]) - 48)
    }
    if sum % 9 == 0 {
      // When number divisible by 9
      result = true
    }
  }
  if result == true {
    fmt.Println(" Given number (", 
      num, ") is divisible by 9")
  } else {
    fmt.Println(" Given number (", 
      num, ") is not divisible by 9")
  }
}
func main() {

  // Test
  isDivisibleBy9("234234125456784564")
  isDivisibleBy9("67344523423242342342363455544")
  isDivisibleBy9("91999")
  isDivisibleBy9("97654218341234234234112")
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
<?php
// Php program for
// Check that if large number is divisible by 9
class Divisibility
{
  public  function isDivisibleBy9($num)
  {
    // Condition
    // ➀ Sum the digits.
        //    And the result must be divisible by 9.
    $result = false;
    $n = strlen($num);
    if ($n == 1 && $num[0] == '0')
    {
      // When number is zero
      $result = true;
    }
    else if ($n > 0)
    {
      $sum = 0;
      // Calculate sum of all digit
      for ($i = 0; $i < $n; ++$i)
      {
        $sum += (ord($num[$i]) - 48);
      }
      if ($sum % 9 == 0)
      {
        // When number divisible by 9
        $result = true;
      }
    }
    if ($result == true)
    {
      echo(" Given number (".$num.
        ") is divisible by 9".
        "\n");
    }
    else
    {
      echo(" Given number (".$num.
        ") is not divisible by 9".
        "\n");
    }
  }
}

function main()
{
  $task = new Divisibility();
  // Test
  $task->isDivisibleBy9("234234125456784564");
  $task->isDivisibleBy9("67344523423242342342363455544");
  $task->isDivisibleBy9("91999");
  $task->isDivisibleBy9("97654218341234234234112");
}
main();

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
// Node JS program for
// Check that if large number is divisible by 9
class Divisibility
{
  isDivisibleBy9(num)
  {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    var result = false;
    var n = num.length;
    if (n == 1 && num.charAt(0) == '0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      var sum = 0;
      // Calculate sum of all digit
      for (var i = 0; i < n; ++i)
      {
        sum += (num.charAt(i).charCodeAt(0) - 48);
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      console.log(" Given number (" + 
                        num + ") is divisible by 9");
    }
    else
    {
      console.log(" Given number (" + 
                        num + ") is not divisible by 9");
    }
  }
}

function main()
{
  var task = new Divisibility();
  // Test
  task.isDivisibleBy9("234234125456784564");
  task.isDivisibleBy9("67344523423242342342363455544");
  task.isDivisibleBy9("91999");
  task.isDivisibleBy9("97654218341234234234112");
}
main();

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
#  Python 3 program for
#  Check that if large number is divisible by 9
class Divisibility :
  def isDivisibleBy9(self, num) :
    #  Condition
    #  ➀ Sum the digits. And the result must be divisible by 9.
    result = False
    n = len(num)
    if (n == 1 and num[0] == '0') :
      #  When number is zero
      result = True
    elif (n > 0) :
      sum = 0
      i = 0
      #  Calculate sum of all digit
      while (i < n) :
        sum += (ord(num[i]) - 48)
        i += 1
      
      if (sum % 9 == 0) :
        #  When number divisible by 9
        result = True
      
    
    if (result == True) :
      print(" Given number (", num ,") is divisible by 9")
    else :
      print(" Given number (", num ,") is not divisible by 9")
    
  

def main() :
  task = Divisibility()
  #  Test
  task.isDivisibleBy9("234234125456784564")
  task.isDivisibleBy9("67344523423242342342363455544")
  task.isDivisibleBy9("91999")
  task.isDivisibleBy9("97654218341234234234112")

if __name__ == "__main__": main()

Output

 Given number ( 234234125456784564 ) is not divisible by 9
 Given number ( 67344523423242342342363455544 ) is divisible by 9
 Given number ( 91999 ) is not divisible by 9
 Given number ( 97654218341234234234112 ) is divisible by 9
#  Ruby program for
#  Check that if large number is divisible by 9
class Divisibility 
  def isDivisibleBy9(num) 
    #  Condition
    #  ➀ Sum the digits. And the result must be divisible by 9.
    result = false
    n = num.length
    if (n == 1 && num[0] == '0') 
      #  When number is zero
      result = true
    elsif (n > 0) 
      sum = 0
      i = 0
      #  Calculate sum of all digit
      while (i < n) 
        sum += (num[i].ord - 48)
        i += 1
      end

      if (sum % 9 == 0) 
        #  When number divisible by 9
        result = true
      end

    end

    if (result == true) 
      print(" Given number (", num ,") is divisible by 9", "\n")
    else
 
      print(" Given number (", num ,") is not divisible by 9", "\n")
    end

  end

end

def main() 
  task = Divisibility.new()
  #  Test
  task.isDivisibleBy9("234234125456784564")
  task.isDivisibleBy9("67344523423242342342363455544")
  task.isDivisibleBy9("91999")
  task.isDivisibleBy9("97654218341234234234112")
end

main()

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 9
class Divisibility()
{
  def isDivisibleBy9(num: String): Unit = {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    var result: Boolean = false;
    var n: Int = num.length();
    if (n == 1 && num.charAt(0) == '0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      var sum: Int = 0;
      var i: Int = 0;
      // Calculate sum of all digit
      while (i < n)
      {
        sum += (num.charAt(i).toInt - 48);
        i += 1;
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      println(" Given number (" + num + ") is divisible by 9");
    }
    else
    {
      println(" Given number (" + num + ") is not divisible by 9");
    }
  }
}
object Main
{
  def main(args: Array[String]): Unit = {
    var task: Divisibility = new Divisibility();
    // Test
    task.isDivisibleBy9("234234125456784564");
    task.isDivisibleBy9("67344523423242342342363455544");
    task.isDivisibleBy9("91999");
    task.isDivisibleBy9("97654218341234234234112");
  }
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 9
class Divisibility
{
  func isDivisibleBy9(_ value: String)
  {
        let num = Array(value); 
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    var result: Bool = false;
    let n: Int = num.count;
    if (n == 1 && num[0] == "0")
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      var sum: Int = 0;
      var i: Int = 0;
      // Calculate sum of all digit
      while (i < n)
      {
        sum += (Int(UnicodeScalar(String(num[i]))!.value) - 48);
        i += 1;
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      print(" Given number (", value ,") is divisible by 9");
    }
    else
    {
      print(" Given number (", value ,") is not divisible by 9");
    }
  }
}
func main()
{
  let task: Divisibility = Divisibility();
  // Test
  task.isDivisibleBy9("234234125456784564");
  task.isDivisibleBy9("67344523423242342342363455544");
  task.isDivisibleBy9("91999");
  task.isDivisibleBy9("97654218341234234234112");
}
main();

Output

 Given number ( 234234125456784564 ) is not divisible by 9
 Given number ( 67344523423242342342363455544 ) is divisible by 9
 Given number ( 91999 ) is not divisible by 9
 Given number ( 97654218341234234234112 ) is divisible by 9
// Kotlin program for
// Check that if large number is divisible by 9
class Divisibility
{
  fun isDivisibleBy9(num: String): Unit
  {
    // Condition
    // ➀ Sum the digits. And the result must be divisible by 9.
    var result: Boolean = false;
    val n: Int = num.length;
    if (n == 1 && num.get(0) == '0')
    {
      // When number is zero
      result = true;
    }
    else if (n > 0)
    {
      var sum: Int = 0;
      var i: Int = 0;
      // Calculate sum of all digit
      while (i < n)
      {
        sum += (num.get(i).toInt() - 48);
        i += 1;
      }
      if (sum % 9 == 0)
      {
        // When number divisible by 9
        result = true;
      }
    }
    if (result == true)
    {
      println(" Given number (" + num + ") is divisible by 9");
    }
    else
    {
      println(" Given number (" + num + ") is not divisible by 9");
    }
  }
}
fun main(args: Array < String > ): Unit
{
  val task: Divisibility = Divisibility();
  // Test
  task.isDivisibleBy9("234234125456784564");
  task.isDivisibleBy9("67344523423242342342363455544");
  task.isDivisibleBy9("91999");
  task.isDivisibleBy9("97654218341234234234112");
}

Output

 Given number (234234125456784564) is not divisible by 9
 Given number (67344523423242342342363455544) is divisible by 9
 Given number (91999) is not divisible by 9
 Given number (97654218341234234234112) is divisible by 9


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