Possible to make a divisible by 3 number using all digits in an array in C++

C++Server Side ProgrammingProgramming

In this problem, we are given an array. Our task is to check whether a number generated by using all digits of the elements of the array is divisible by 3. If possible then print “Yes” otherwise print “No”.

Let’s take an example to understand the problem

Input − arr = {3, 5, 91, }

Output − YES

Explanation − The number 5193 is divisible by 3. So, our answer is YES.

To solve this problem, we will check its divisibility by 3.

Divisibility by 3 − a number is divisible by 3 if the sum of its digits is divisible by 3.

Now, we will have to find the sum of all array elements. If this sum is divisible by 3, then it is possible to print YES. otherwise No.


Program to show the implementation of our solution

 Live Demo

#include <iostream>
using namespace std;
bool is3DivisibleArray(int arr[]) {
   int n = sizeof(arr) / sizeof(arr[0]);
   int rem = 0;
   for (int i=0; i<n; i++)
      rem = (rem + arr[i]) % 3;
   return (rem == 0);
int main(){
   int arr[] = { 23, 64, 87, 12, 9 };
   cout<<"Creating a number from digits of array which is divisible by 3 ";
   is3DivisibleArray(arr)?cout<<"is Possible":cout<<"is not Possible";
   return 0;


Creating a number from digits of array which is divisible by 3 is Possible
Published on 17-Apr-2020 10:52:45