Print all numbers that can be obtained by adding A or B to N exactly M times | C ++ program for the 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 obtained ; If A and B are equal , then only one number can be obtained , i . e . N + M * A ; For finding others numbers , subtract A from number once and add B to number once ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { swap ( A , B ) ; } int number = N + M * A ; cout << number << " ▁ " ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; cout << number << " ▁ " ; } } } int main ( ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; return 0 ; }