Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-17 11:53:03
forward
1557 people have browsed it

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
Copy after login

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;
}
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!