Reputation: 11
The task is to ask a user to input 2 values (say m and n) wherein these values may be positive or negative. The program should generate the numbers from n to m such that the interval is 1. Ex, if m = -3 and n = 4, the numbers generated are -3, -2, -1, 0, 1, 2, 3, 4, however when i use my code, it will only show the positive values. Thank you!
int
main ()
{
int m, n, i;
scanf("%d%d", &m, &n);
for ( i = 0; i <= m || i <= n; i++)
printf("%d", i);
return 0;
}
Upvotes: 1
Views: 154
Reputation: 7490
int
main ()
{
int m, n, i;
scanf("%d%d", &m, &n);
for ( i = m; i <= n; i++)
printf("%d ", i);
return 0;
}
Assuming that m
is the lower boundary, you have to start your loop from i=m
.
Upvotes: 0
Reputation: 6206
Try this:
int m, n, i, min, max;
scanf("%d%d", &m, &n);
if (m < n) {
min = m;
max = n;
}
else {
min = n;
max = m;
}
for (i = min; i <= max; i++)
printf("%d", i);
Upvotes: 1