Here two somehow similar problems (1.-Sum of uneven numbers until n; 2.-Sum of the series 1/n) to be solved using for-loops. I chose the shown strategy and while for the 1st problem my loop works as expected, for the 2nd it does not iterate. I can't see the difference to understand what is wrong in the last case.
----1----- :
int main()
{
// Example: Input=5, Output=1+3+5=9
int i, lastNum ,sumUneven;
printf("Until what n should the uneven numbers be added: ");
scanf("%d", &lastNum);
sumUneven=0;
for (i=1; 2*i-1<=lastNum; i++) {
sumUneven=sumUneven + (2*i-1);
}
printf("Sum of uneven numbers until %d: %d", lastNum, sumUneven);
}
----2------------:
int main()
{
//My goal: Input=n; Output= 1+ 1/2 + 1/3....+1/n.
int i, n, sum;
printf("Until what n should this series be calculated: ");
scanf("%d", &n);
sum=0;
for (i=1; i<=n; i++) {
sum = sum + 1/i;
}
printf("Sum of the series until 1/%d: %d", n, sum);
}