Print all numbers that can be obtained by adding A or B to N exactly M times | Java program for tha above approach ; Function to find all possible numbers that can be obtained by adding A or B to N exactly M times ; For maintaining increasing order ; Smallest number that can be achieved ; If A and B are equal , the only number that can be onbtained is N + M * A ; For finding other numbers , subtract A from number 1 time and add B to number 1 time ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { int temp = A ; A = B ; B = temp ; } int number = N + M * A ; System . out . print ( number + " ▁ " ) ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; System . out . print ( number + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; } }