Home > Backend Development > C++ > Check if it is possible to make a C/C++ program divisible by 3 using all the numbers in the array

Check if it is possible to make a C/C++ program divisible by 3 using all the numbers in the array

WBOY
Release: 2023-09-05 18:49:06
forward
1418 people have browsed it

Check if it is possible to make a C/C++ program divisible by 3 using all the numbers in the array

In this section we will see if an array contains n numbers we have to check if a number is generated using all the elements of these numbers and if the number is divisible by 3 . If the array elements are {15, 24, 23, 13} then we can make an integer like 15242313. Divisible by 3.

Algorithm

checkDivThree(arr)

Begin
   rem := 0
   for each element e in arr, do
      rem := (rem + e) mod 3
   done
   if rem is 0, then
      return true
   end if
   return false
End
Copy after login

Example

#include<iostream>
#define MAX 4
using namespace std;
bool checkDivThree(int arr[], int n){
   int rem = 0;
   for(int i = 0; i<n; i++){
      rem = (rem + arr[i]) % 3;
   }
   if(rem == 0){
      return true;
   }
   return false;
}
main() {
   int arr[] = {15, 24, 23, 13};
   int n = sizeof(arr)/sizeof(arr[0]);
   if(checkDivThree(arr, n)){
      cout << "Divisible";
   }else{
      cout << "Not Divisible";
   }
}
Copy after login

Output

Divisible
Copy after login

The above is the detailed content of Check if it is possible to make a C/C++ program divisible by 3 using all the numbers in the 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