Divisibilidad por 12 para un gran número

Dado un número grande, la tarea es comprobar si el número es divisible por 12 o no.

Ejemplos: 

Input : 12244824607284961224
Output : Yes

Input : 92387493287593874594898678979792
Output : No

Este es un enfoque muy simple. si un número es divisible por 4 y 3 entonces el número es divisible por 12. 
Punto 1 . Si los dos últimos dígitos del número son divisibles por 4, entonces el número es divisible por 4. Consulte la divisibilidad por 4 para números grandes para obtener más detalles. 

punto 2 . si la suma de todos los dígitos de un número se divide por 3, entonces el número es divisible por 3. Consulte la divisibilidad por 3 para números grandes para obtener más detalles. 

C++

// C++ Program to check if
// number is divisible by 12
#include <iostream>
using namespace std;
 
bool isDvisibleBy12(string num)
{
    // if number greater then 3
    if (num.length() >= 3) {
 
        // find last digit
        int d1 = (int)num[num.length() - 1];
 
        // no is odd
        if (d1 % 2 != 0)
            return (0);
 
        // find second last digit
        int d2 = (int)num[num.length() - 2];
 
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.length(); i++)
            sum += num[i];           
         
        return (sum % 3 == 0 && (d2 * 10 + d1) % 4 == 0);           
    }
     
    else {
         
        // if number is less then
        // or equal to 100
        int number = stoi(num);
        return (number % 12 == 0);
    }
}
 
// Driver function
int main()
{
    string num = "12244824607284961224"; 
    if (isDvisibleBy12(num))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

Java

// Java Program to check if
// number is divisible by 12
 
import java.io.*;
 
class GFG {
static boolean isDvisibleBy12(String num)
{
    // if number greater then 3
    if (num.length() >= 3) {
  
        // find last digit
        int d1 = (int)num.charAt(num.length() - 1);
  
        // no is odd
        if (d1 % 2 != 0)
            return false;
  
        // find second last digit
        int d2 = (int)num.charAt(num.length() - 2);
  
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.length(); i++)
            sum += num.charAt(i);           
          
        return (sum % 3 == 0 &&
               (d2 * 10 + d1) % 4 == 0);           
    }
      
    else {
          
        // if number is less then
        // or equal to 100
        int number = Integer.parseInt(num);
        return (number % 12 == 0);
    }
 
// driver function
}
    public static void main (String[] args) {
 
    String num = "12244824607284961224"; 
    if (isDvisibleBy12(num))
        System.out.print("Yes");
    else
        System.out.print("No");
         
    }
}
 
// This code is contributed by Gitanjali.

Python3

# Python Program to check if
# number is divisible by 12
 
import math
 
def isDvisibleBy12( num):
 
    # if number greater then 3
    if (len(num) >= 3):
  
        # find last digit
        d1 = int(num[len(num) - 1]);
  
        # no is odd
        if (d1 % 2 != 0):
            return False
  
        # find second last digit
        d2 = int(num[len(num) - 2])
  
        # find sum of all digits
        sum = 0
        for  i in range(0, len(num) ):
            sum += int(num[i])          
          
        return (sum % 3 == 0 and
               (d2 * 10 + d1) % 4 == 0)           
 
      
    else :
          
        # f number is less then
        # r equal to 100
        number = int(num)
        return (number % 12 == 0)
     
 
num = "12244824607284961224" 
if(isDvisibleBy12(num)):
       print("Yes")
else:
       print("No")
 
# This code is contributed by Gitanjali.

C#

// C# Program to check if
// number is divisible by 12
using System;
 
class GFG
{
static bool isDvisibleBy12(string num)
{
    // if number greater then 3
    if (num.Length >= 3) {
 
        // find last digit
        int d1 = (int)num[num.Length - 1];
 
        // no is odd
        if (d1 % 2 != 0)
            return false;
 
        // find second last digit
        int d2 = (int)num[num.Length - 2];
 
        // find sum of all digits
        int sum = 0;
        for (int i = 0; i < num.Length; i++)
            sum += num[i];        
         
        return (sum % 3 == 0 &&
            (d2 * 10 + d1) % 4 == 0);        
    }
     
    else {
         
        // if number is less then
        // or equal to 100
        int number = int.Parse(num);
        return (number % 12 == 0);
    }
}
 
    // Driver function
    public static void Main ()
    {
       String num = "12244824607284961224";
       if (isDvisibleBy12(num))
          Console.Write("Yes");
       else
          Console.Write("No");
    }
}
 
// This code is contributed by nitin mittal.

PHP

<?php
// PHP Program to check if
// number is divisible by 12
 
function isDvisibleBy12($num)
{
     
    // if number greater then 3
    if (strlen($num) >= 3)
    {
 
        // find last digit
        $d1 = (int)$num[strlen($num) - 1];
 
        // no is odd
        if ($d1 % 2 != 0)
            return (0);
 
        // find second last digit
        $d2 = (int)$num[strlen($num) - 2];
 
        // find sum of all digits
        $sum = 0;
        for ($i = 0; $i < strlen($num); $i++)
            $sum += $num[$i];    
         
        return ($sum % 3 == 0 &&
               ($d2 * 10 + $d1) % 4 == 0);        
    }
     
    else {
         
        // if number is less then
        // or equal to 100
        $number = stoi($num);
        return ($number % 12 == 0);
    }
}
 
// Driver Code
$num = "12244824607284961224";
if (isDvisibleBy12($num))
    echo("Yes");
else
    echo("No");
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// Javascript program to check if
// number is divisible by 12
function isDvisibleBy12(num)
{
     
    // If number greater then 3
    if (num.length >= 3)
    {
         
        // Find last digit
        let d1 = num[num.length - 1].charCodeAt();
 
        // No is odd
        if (d1 % 2 != 0)
            return false;
 
        // Find second last digit
        let d2 = num[num.length - 2].charCodeAt();
 
        // Find sum of all digits
        let sum = 0;
        for(let i = 0; i < num.length; i++)
            sum += num[i].charCodeAt();        
 
        return ((sum % 3 == 0) &&
                 (d2 * 10 + d1) % 4 == 0);        
    }
 
    else
    {
         
        // If number is less then
        // or equal to 100
        let number = parseInt(num, 10);
        document.write(number);
        return(number % 12 == 0);
    }
}
 
// Driver code
let num = "12244824607284961224";
if (isDvisibleBy12(num))
    document.write("Yes");
else
    document.write("No");
     
// This code is contributed by divyeshrabadiya07
 
</script>
Producción

Yes

Método: Comprobación de que el número dado es divisible por 12 o no utilizando el operador de división de módulo «%».  

Java

// Java code for the above approach
// To check whether the given number is divisible by 12 or
// not
import java.math.BigInteger;
 
class GFG {
 
  public static void main(String[] args)
  {
    // input number
    BigInteger num = new BigInteger("12244824607284961224");
 
    // finding given number is divisible by 20 or not
    if (num.mod(new BigInteger("12"))
        .equals(new BigInteger("0"))) {
      System.out.println("Yes");
    }
    else {
      System.out.println("No");
    }
  }
}
 
// This code is contributed by phasing17

Python3

# Python code
# To check whether the given number is divisible by 12 or not
 
#input
n=12244824607284961224
# the above input can also be given as n=input() -> taking input from user
# finding given number is divisible by 12 or not
if int(n)%12==0:
  print("Yes")
else:
  print("No")
   
  # this code is contributed by gangarajula laxmi

C#

using System;
 
public class GFG {
 
    static public void Main()
    {
 
        // input number
        double num = 12244824607284961224;
 
        // finding given number is divisible by 12 or not
        if (num % 12 == 0) {
            Console.Write("Yes");
        }
        else {
            Console.Write("No");
        }
    }
}
 
// This code is contributed by laxmigangarajula03

Javascript

<script>
        // JavaScript code for the above approach
        // To check whether the given number is divisible by 12 or not
  
        // input
        var n = 12244824607284961224
         
        // finding given number is divisible by 12 or not
        if (n % 12 == 0)
            document.write("Yes")
        else
            document.write("No")
  
// This code is contributed by laxmigangarajula03
    </script>
Producción

Yes

Publicación traducida automáticamente

Artículo escrito por Amit_Soni y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *