Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmEven digits Sum and Odd digits sum divisible by 4...

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 the 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++




// 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;
}


Java




// 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.


Python 3




# 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


C#




// 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.


PHP




<?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)


Javascript




<script>
  
// JavaScript implementation of above algorithm
  
// Function to check the sum
function checkSum(num)
{
    let sum = 0;
  
    // Traverse each digit
    for (let i = 0; i < num.length; i++) {
  
        // converting a character to integer by
        // taking difference of their ASCII value
        let 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
  
    let num = "12347";
    document.write(checkSum(num) ?
                              "Yes" : "No");
  
  
// This code is contributed by Surbhi Tyagi.
  
</script>


Output

No

Time Complexity: O(N)
Auxiliary Space: O(1) as it is using constant space for variables

Method #2: Using string:

  1. We have to convert the given number to a string by taking a new variable.
  2. Traverse the string, Convert each element to integer and add this to sum.
  3. If the sum is even, Check if the sum is divisible by 4
  4. Else if the sum is odd, Check if it is divisible by 3.
  5. Print Yes, if any of the case in step 3 or step 4 satisfies otherwise print No.

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include <iostream>
using namespace std;
string getResult(int n)
{
  
  // Converting integer to string
  string st = to_string(n);
  
  // Initialising sum to 0
  int sum = 0;
  int length = st.length();
  
  // Traversing through the string
  for (auto i : st) 
  {
      
    // Converting character to int
    sum = sum + i - '0';
  }
  if ((sum % 2 == 0 and sum % 4 == 0)
      or (sum % 2 != 0 and sum % 3 == 0))
    return "Yes";
  
  return "No";
}
int main()
{
  
  int n = 202;
  
  // passing this number to get result function
  cout << getResult(n);
  
  return 0;
}
  
// This code is contributed by Abhijeet Kumar(abhijeet19403)


Java




// Java implementation of above approach 
import java.io.*; 
  
class GFG { 
  
  // Function to get Result 
  static String getResult(int n) 
  
  
    // Converting integer to string 
    String st = Integer.toString(n); 
  
    // Initialising sum to 0 
    int sum = 0
    int length = st.length(); 
  
    // Traversing through the string 
    for (int i = 0; i < length; i++) 
    
  
      // Converting character to int 
      sum = sum + st.charAt(i) - '0'
    
    if ((sum % 2 == 0 && sum % 4 == 0
        || (sum % 2 != 0 && sum % 3 == 0)) 
      return "Yes"
  
    return "No"
  
  
  // Driver code 
  public static void main(String[] args) 
  
  
    int n = 202
  
    // Passing this number to get result function 
    System.out.println(getResult(n)); 
  
}
  
// This code is contributed by factworx412


Python3




# Python implementation of above approach
def getResult(n):
    
    # Converting integer to string
    st = str(n)
      
    # Initialising sum to 0
    sum = 0
    length = len(st)
  
    # Traversing through the string
    for i in st:
  
        # Converting character to int
        sum = sum + int(i)
          
    if ((sum % 2 == 0 and sum % 4 == 0) or
            (sum % 2 != 0 and sum % 3 == 0)):
        return 'Yes'
  
    return 'No'
  
  
# Driver Code
n = 202
  
# passing this number to get result function
print(getResult(n))
  
# this code is contributed by vikkycirus


Javascript




<script>
  
// JavaScript implementation of above approach
function getResult(n){
    
    // Converting integer to string
    var st = n.toString();
      
    // Initialising sum to 0
    var sum = 0
    var length = st.length;
  
    // Traversing through the string
    for(let i=0 ; i< st.length ; i++ ){
  
        // Converting character to int
        sum = sum + Number(st[i])
    }
          
    if ((sum % 2 == 0 && sum % 4 == 0) ||
            (sum % 2 != 0 && sum % 3 == 0)){
        return 'Yes'
    }
    else{
    return 'No';
    }
   }
  
  
// Driver Code
var n = 202;
  
// passing this number to get result function
document.write(getResult(n))
  
  
</script>


C#




// C# implementation of above approach
  
using System;
class Gfg{
    static string getResult(int n)
    {
      
      // Converting integer to string
      string st = n.ToString();
      // Initialising sum to 0
      int sum = 0;
      int length = st.Length;
      
      // Traversing through the string
      for (int i=0; i<length; i++) 
      {
          
        // Converting character to int
        int x=st[i] - '0';
        sum+=x;
      }
      if ((sum % 2 == 0 && sum % 4 == 0)
          || (sum % 2 != 0 && sum % 3 == 0))
        return "Yes";
      
      return "No";
    }
    public static void Main(String []args)
    {
      
      int n = 202;
      
      // passing this number to get result function
      Console.WriteLine(getResult(n));
      
    }
}


Output

Yes

Time Complexity: O(N)
Auxiliary Space: O(N)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Thapelo Manthata
I’m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments