Count of different numbers divisible by 3 that can be obtained by changing at most one digit | Java program fo the above approach ; Function to count the number of possible numbers divisible by 3 ; Calculate the sum ; Store the answer ; Iterate over the range ; Decreasing the sum ; Iterate over the range ; Checking if the new sum is divisible by 3 or not ; If yes increment the value of the count ; Driver Code ; Given number
import java . io . * ; import java . util . * ; class GFG { public static void findCount ( String number ) { int sum = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { sum += number . charAt ( i ) - 48 ; } int count = 0 ; for ( int i = 0 ; i < number . length ( ) ; ++ i ) { int remaining_sum = sum - ( number . charAt ( i ) - 48 ) ; for ( int j = 0 ; j <= 9 ; ++ j ) { if ( ( remaining_sum + j ) % 3 == 0 && j != number . charAt ( i ) - 48 ) { ++ count ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String number = "235" ; findCount ( number ) ; } }