
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Make a Divisible by 3 Number Using All Digits in an Array in C++
In this problem, we are given an array. Our task is to check whether a number generated by using all digits of the elements of the array is divisible by 3. If possible then print “Yes” otherwise print “No”.
Let’s take an example to understand the problem
Input − arr = {3, 5, 91, }
Output − YES
Explanation − The number 5193 is divisible by 3. So, our answer is YES.
To solve this problem, we will check its divisibility by 3.
Divisibility by 3 − a number is divisible by 3 if the sum of its digits is divisible by 3.
Now, we will have to find the sum of all array elements. If this sum is divisible by 3, then it is possible to print YES. otherwise No.
Example
Program to show the implementation of our solution
#include <iostream> using namespace std; bool is3DivisibleArray(int arr[]) { int n = sizeof(arr) / sizeof(arr[0]); int rem = 0; for (int i=0; i<n; i++) rem = (rem + arr[i]) % 3; return (rem == 0); } int main(){ int arr[] = { 23, 64, 87, 12, 9 }; cout<<"Creating a number from digits of array which is divisible by 3 "; is3DivisibleArray(arr)?cout<<"is Possible":cout<<"is not Possible"; return 0; }
Output
Creating a number from digits of array which is divisible by 3 is Possible
Advertisements