C/C++ Program to check whether it is possible to make the divisible by 3 number using all digits in an array?


To check whether a number is divisible by 3, we add all the digits of the number and then calculate that the sum is divisible by 3 or not. In this problem, there is an array of integers arr[], and we have to check if a number formed with these number is divisible by 3. If the number formed is divisible then print ‘yes’ else print ‘no’

Input: arr[] = {45, 51, 90}
Output: Yes

Explanation

construct a number which is divisible by 3, for example, 945510.

So the answer will be Yes Find the remainder of the sum when divided by 3 true if the remainder is 0.

Example

#include <stdio.h>
int main() {
   int arr[] = { 45, 51, 90 };
   int n =3;
   int rem = 0;
   for (int i = 0; i < n; i++) {
      rem = (rem + arr[i]) % 3;
   }
   if (rem==0)
      printf("Yes\n");
   else
      printf("No\n");
   return 0;
}

Updated on: 19-Aug-2019

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements