I wrote a code to find the lcm of multiple numbers, but the loop is looping one less time. The first line of the input contains a positive integer N, and each of the next N lines contains 2 space-separated integers. The code will then add each of the two intengers and calculate the lcm of the sums. I think I have made a stupid mistake in the last loop, but I can't find it. I am a beginner, so please point out my stupid mistake in the code. Thank you!
#include <cstdio>
lcm (int x, int y){
if (x > y){
for (int i = 1; i <= y; i++){
if ((x * i) % y == 0){
int lcm = x * i;
return lcm;
break;
}
}
}elseif (x < y){
for (int i = 1; i <= x; i++){
if ((y * i) % x == 0){
int lcm = y * i;
return lcm;
break;
}
}
}
}
int main (){
int number = 0;
scanf("%d",&number);
int a[number] = { };//The first intengers or the sum of the 2 intengers
int b[number] = { };//The second intengers
int c = 1;
while (c < number){
scanf("%d %d\n",&a[c],&b[c]);
a[c] += b[c];
c++;
}
int n = 1;
int d = 0;
while (d < number){
n = lcm(n, a[d]);
if (d == number - 1){
printf("%d\n",n);
}
d++;
}
}
Some inputs and outputs:
test1:
3 input
1 3 input
2 4 input
5 8 input
12 output
let me see if I understand.
The second loop was working fine, but you decided to edit it. Now it fails, but you don't think that it's necessary to show your changes.