Home > Article > Backend Development > 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
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.
#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!