for loop
Oct 31, 2013 at 8:12pm Oct 31, 2013 at 8:12pm UTC
Hello!
Please, what is wrong with the loop?
Why is this loop writing out 1 and not 5???
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
// skrajšanje vlomkov
#include<iostream>
using namespace std;
int main(){
int a= 10;
int b=6;
int e;
if (a<b){e=a;}
else
{e=b;}
for (int i=2; i<=e; i++)
{
if (a%i==0)
{
a/=i;
}
}
cout << a;
return 0;
}
Oct 31, 2013 at 8:43pm Oct 31, 2013 at 8:43pm UTC
it does write out 5's if you do it like this:
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
#include<iostream>
using namespace std;
int main(){
int a= 10;
int b=6;
int e;
if (a<b)
{
e=a;
}
else
{
e=b; // e is 6
}
for ( int i = 2 ; i <= e ; ++i )
{
if (a % i == 0)
{
a/=i;
}
cout << a <<" " ;
}
return 0;
}
would help if you tell us what exactly are you trying to achieve with this simple for loop.
Topic archived. No new replies allowed.