Generate first K multiples of N using Bitwise operators | C ++ program to implement the above approach ; Function to print the first K multiples of N ; Print the value of N * i ; Iterate each bit of N and add pow ( 2 , pos ) , where pos is the index of each set bit ; Check if current bit at pos j is fixed or not ; For next set bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Kmultiples ( int n , int k ) { int a = n ; for ( int i = 1 ; i <= k ; i ++ ) { cout << n << " ▁ * ▁ " << i << " ▁ = ▁ " << a << endl ; int j = 0 ; while ( n >= ( 1 << j ) ) { a += n & ( 1 << j ) ; j ++ ; } } } int main ( ) { int N = 16 , K = 7 ; Kmultiples ( N , K ) ; return 0 ; }