Even digits Sum and Odd digits sum divisible by 4 and 3 respectively

Given a number N represented as a string The task is to print ‘Yes’ if the sum of digits is even and is divisible by 4 or if the sum of digits is odd and is divisible by 3 otherwise ‘No’.

Examples:

Input:  12345
Output: Yes

Input: 894561
Output: Yes   


Below is the step by step algorithm
:



  1. Calculate sum of all digits.
  2. If the sum is even:
    • Check if the sum is divisible by 4
  3. Else if the sum is odd:
    • Check if it is divisible by 3.
  4. Print Yes, if any of the case in step 2 or step 3 satisfies otherwise print No.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above algorithm
  
#include<bits/stdc++.h>
using namespace std;
  
// Function to check the sum 
bool checkSum(string num)
{
    int sum = 0;
      
// Traverse each digit
    for (int i = 0; i < num.length(); i++) 
    {
  
            // converting a character to integer by
            // taking difference of their ASCII value
            int digit = num[i] - '0';
            sum += digit;
        }
          
    // Check if sum is even and divisible by 4
    // or if sum is odd and divisible by 3 then
    // return true, else return false
    if ((sum % 2 == 0 && sum % 4 == 0) ||
        (sum % 2 !=0 && sum % 3 == 0))
        return true;
          
    return false;
}
  
// Driver code
int main()
{
  
    string num = "12347";
    checkSum(num) ? cout << "Yes"
                : cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above algorithm
import java.lang.*;
class Geeks {
  
// Function to check the sum 
static boolean checkSum(String num)
{
    int sum = 0;
      
    // Traverse each digit
    for (int i = 0; i < num.length(); i++) 
    {
  
            // converting a character to integer by
            // taking difference of their ASCII value
            int digit = num.charAt(i) - '0';
            sum += digit;
        }
          
    // Check if sum is even and divisible by 4
    // or if sum is odd and divisible by 3 then
    // return true, else return false
    if ((sum % 2 == 0 && sum % 4 == 0) ||
        (sum % 2 !=0 && sum % 3 == 0))
        return true;
          
    return false;
}
  
// Driver code
public static void main(String args[])
{
  
    String num = "12347";
    System.out.println(checkSum(num) ? "Yes" : "No");
  
}
}
  
// This code is contributed by ankita_saini.

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of 
# above algorithm
  
# Function to check the sum 
def checkSum(num):
  
    sum = 0
      
    # Traverse each digit
    for i in range(len(num)): 
  
        # converting a character to 
        # integer by taking difference 
        # of their ASCII value
        digit = ord(num[i]) - ord('0')
        sum += digit
          
    # Check if sum is even and
    # divisible by 4 or if sum 
    # is odd and divisible by 3 
    # then return true, else 
    # return false
    if ((sum % 2 == 0 and sum % 4 == 0) or
        (sum % 2 != 0 and sum % 3 == 0)):
        return True
          
    return False
  
# Driver code
if __name__ == "__main__":
      
    num = "12347"
    print("Yes") if checkSum(num) else print("No")
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above algorithm
using System;
  
class GFG 
{
  
// Function to check the sum 
static bool checkSum(String num)
{
    int sum = 0;
      
    // Traverse each digit
    for (int i = 0; i < num.Length; i++) 
    {
  
        // converting a character to 
        // integer by taking difference
        // of their ASCII value
        int digit = num[i] - '0';
        sum += digit;
    }
          
    // Check if sum is even and 
    // divisible by 4 or if sum 
    // is odd and divisible by 3 
    // then return true, else
    // return false
    if ((sum % 2 == 0 && sum % 4 == 0) ||
        (sum % 2 !=0 && sum % 3 == 0))
        return true;
          
    return false;
}
  
// Driver code
public static void Main(String []args)
{
    String num = "12347";
    Console.WriteLine(checkSum(num) ? 
                              "Yes" : "No");
}
}
  
// This code is contributed
// by ankita_saini.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above algorithm
  
// Function to check the sum 
function checkSum($num)
{
    $sum = 0;
      
    // Traverse each digit
    for ($i = 0; $i < sizeof($num); $i++) 
    {
  
        // converting a character to 
        // integer by taking difference 
        // of their ASCII value
        $digit = $num[$i] - '0';
        $sum += $digit;
    }
      
    // Check if sum is even and divisible 
    // by 4 or if sum is odd and divisible 
    // by 3 then return true, else return false
    if (($sum % 2 == 0 && $sum % 4 == 0) ||
        ($sum % 2 != 0 && $sum % 3 == 0))
        return true;
          
    return false;
}
  
// Driver code
$num = "12347";
if(checkSum($num)) 
    echo "Yes";
else
    echo "No";
  
// This code is contributed by
// Akanksha Rai(Abby_akku)

chevron_right


Output:

No

Time Complexity :O(n)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.