Check if the number formed by the last digits of N numbers is divisible by 10 or not - GeeksforGeeks

13 Mar, 2022
Given an array arr[] of size N consisting of non-zero positive integers. The task is to determine whether the number that is formed by selecting the last digits of all the numbers is divisible by 10 or not. If the number is divisible by 10, then print Yes otherwise print No.Examples:  
Input: arr[] = {12, 65, 46, 37, 99} Output: No 25679 is not divisible by 10.Input: arr[] = {24, 37, 46, 50} Output: Yes 4760 is divisible by 10.  
 
Approach: In order for an integer to be divisible by 10, it must be ending with a 0. So, the last element of the array will decide whether the number formed will be divisible by 10 or not. If the last digit of the last element is 0 then print Yes otherwise print No.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Function that returns true if the// number formed by the last digits of// all the elements is divisible by 10bool isDivisible(int arr[], int n){    // Last digit of the last element    int lastDigit = arr[n - 1] % 10;     // Number formed will be divisible by 10    if (lastDigit == 0)        return true;     return false;} // Driver codeint main(){    int arr[] = { 12, 65, 46, 37, 99 };    int n = sizeof(arr) / sizeof(arr[0]);     if (isDivisible(arr, n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachimport java.util.*;     class GFG{ // Function that returns true if the// number formed by the last digits of// all the elements is divisible by 10static boolean isDivisible(int arr[], int n){    // Last digit of the last element    int lastDigit = arr[n - 1] % 10;     // Number formed will be divisible by 10    if (lastDigit == 0)        return true;     return false;} // Driver codestatic public void main ( String []arg){    int arr[] = { 12, 65, 46, 37, 99 };    int n = arr.length;     if (isDivisible(arr, n))        System.out.println("Yes");    else        System.out.println("No");}} // This code is contributed by Rajput-Ji
# Python3 implementation of the approach # Function that returns true if the# number formed by the last digits of# all the elements is divisible by 10def isDivisible(arr, n) :     # Last digit of the last element    lastDigit = arr[n - 1] % 10;     # Number formed will be divisible by 10    if (lastDigit == 0) :        return True;     return False; # Driver codeif __name__ == "__main__" :     arr = [ 12, 65, 46, 37, 99 ];    n = len(arr);     if (isDivisible(arr, n)) :        print("Yes");    else :        print("No"); # This code is contributed by AnkitRai01
// C# implementation of the approachusing System;     class GFG{ // Function that returns true if the// number formed by the last digits of// all the elements is divisible by 10static bool isDivisible(int []arr, int n){    // Last digit of the last element    int lastDigit = arr[n - 1] % 10;     // Number formed will be divisible by 10    if (lastDigit == 0)        return true;     return false;} // Driver codestatic public void Main(String []arg){    int []arr = { 12, 65, 46, 37, 99 };    int n = arr.Length;     if (isDivisible(arr, n))        Console.WriteLine("Yes");    else        Console.WriteLine("No");}}     // This code is contributed by Rajput-Ji
<script>// Javascript implementation of the approach // Function that returns true if the// number formed by the last digits of// all the elements is divisible by 10function isDivisible(arr, n){    // Last digit of the last element    let lastDigit = arr[n - 1] % 10;     // Number formed will be divisible by 10    if (lastDigit == 0)        return true;     return false;} // Driver code    let arr = [ 12, 65, 46, 37, 99 ];    let n = arr.length;     if (isDivisible(arr, n))        document.write("Yes");    else        document.write("No"); </script>
No
 
Time Complexity: O(1)
Auxiliary Space: O(1)
ankthon
Rajput-Ji
subham348
subhammahato348
Number Divisibility
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Program to add two binary strings
Program to multiply two matrices
Fizz Buzz Implementation
Find pair with maximum GCD in an array
Find Union and Intersection of two unsorted arrays
Count all possible paths from top left to bottom right of a mXn matrix
Count ways to reach the n'th stair