-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.cpp
More file actions
47 lines (33 loc) · 1.01 KB
/
day4.cpp
File metadata and controls
47 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;
/*
Suppose you have a multiplication table that is N by N.
That is, a 2D array where the value at the i-th row and j-th column is (i + 1) * (j + 1)
(if 0-indexed) or i * j (if 1-indexed).
Given integers N and X, write a function that returns the number of times X
appears as a value in an N by N multiplication table.
For example, given N = 6 and X = 12, you should return 4,
since the multiplication table looks like this:
| 1 | 2 | 3 | 4 | 5 | 6 |
| 2 | 4 | 6 | 8 | 10 | 12 |
| 3 | 6 | 9 | 12 | 15 | 18 |
| 4 | 8 | 12 | 16 | 20 | 24 |
| 5 | 10 | 15 | 20 | 25 | 30 |
| 6 | 12 | 18 | 24 | 30 | 36 |
And there are 4 12's in the table.
*/
int solution(int n, int x){
int count = 0;
for(int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if ((i*j)==x){
count+=1;
}
}
}
return count;
}
int main(){
cout<<"N=6 and X = 12: "<<solution(6,12)<<endl;
return 0;
}