Nested for loop

Anyone can guide me for this ?
Write a program to find factors of number between 2 and 10 by using nested for loop
Couldt get any answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream> 
using namespace std; 
 
int main() { 
 
  for(int i=2;i<=10;++i) { 

    for(int j=i;j<=10;++j){
    	cout<<"Factor of "<<j<<":"<<endl;
	}
 
 
  return 0; 
}
}
Well, your first problem is that your return statement is within your outer for loop.
Please practice proper code indentation, it will save you a lot of time.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream> 
using namespace std; 
 
int main() { 
    for (int i=2;i<=10;++i) { 
        for (int j=i;j<=10;++j) {
    	    cout<<"Factor of "<<j<<":"<<endl;
	} 
    }
    return 0;
}


...but besides that, you aren't actually doing any calculations. You're just printing j a bunch of times.

If you want to know the factors if a number i, you need to try to find the remainder of i/j. You do this through the modulus operator, %.

So, let's say i = 10, and j = 2, 10 is evenly divisible by 2, which means 10 % 2 == 0. Therefore, 2 is a factor of 10.
Topic archived. No new replies allowed.