Home  >  Article  >  Backend Development  >  Check if a C/C++ program divisible by 3 can be constructed using all the numbers in an array

Check if a C/C++ program divisible by 3 can be constructed using all the numbers in an array

王林
王林forward
2023-09-17 11:53:031559browse

Check if a C/C++ program divisible by 3 can be constructed using all the numbers in an array

To check if a number is divisible by 3, we add all the digits of the number and then calculate whether the sum is divisible by 3. In this problem, there is an array of integers arr[] and we need to check whether the number consisting of these numbers is divisible by 3. If it is divisible, print 'yes', otherwise print 'no'

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

Explanation

Construct a number that can be divisible by 3 , for example 945510.

So the answer will be yes, when divisible by 3, the remainder of the sum 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;
}

The above is the detailed content of Check if a C/C++ program divisible by 3 can be constructed using all the numbers in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete