- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?
In this section we will see if one array is given with n numbers, we have to check if we make a number using all of the elements of these numbers, that number will be divisible by 3 or not. If the array elements are {15, 24, 23, 13}, then the we can make integer like 15242313. It will be 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
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"; } }
Output
Divisible
Advertisements