osito_solito
osito_solito

Reputation: 65

How do I put this if-else statement from the while, in a for loop?

So, initially I did this if-else nested in a while loop to add all the squares of the odd numbers between to numbers. For example, if it the two numbers were 10 and 20, the sum of the squares of the odd numbers would be 1165 (11^2 + 13^2 + 15^2 ...). So, now I want to use a for loop instead of a while loop. I've seen other examples, but I can't implement with mine since it's only my second with working with C.

The following is the while loop and the print statement for the answer (I made "i" = firstNum, so firstNum wouldn't change in value, and firstNum and secondNum is just input from the user):

i = firstNum;
while (i <= secondNum) {
    // checks if number is odd or even
    if (i % 2 != 0) {
        sumOdds += i * i;
        i += 2;
    }
    else {
        ++i;
    }
}
printf("\n");
printf("Sum of the squares of odd integers between %d and %d = %d\n", firstNum, secondNum, sumOdds);

Upvotes: 2

Views: 155

Answers (4)

Fucio
Fucio

Reputation: 533

Just use the increment of the loop so you add the remaining value.

for (int i = firstNum; i <= secondNum; i++) {
    // checks if number is odd or even
    if (i % 2 != 0) {
        sumOdds += i * i;
        i++;
    }
}
printf("\n");

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

The for loop can look for example the following way

for ( int i = firstNum + ( firstNum % 2 == 0 ); i <= secondNum; i += 2 )
{
   sumOdds += i * i;
}

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    
    int firstNum = 0, secondNum = 0;
    
    printf( "Enter two numbers: " );
    scanf( "%d %d", &firstNum, &secondNum );
    
    if ( secondNum < firstNum )
    {
        int tmp = firstNum;
        firstNum = secondNum;
        secondNum = tmp;
    }
    
    long long int sumOdds = 0;
    
    for ( int i = firstNum + ( firstNum % 2 == 0 ); i <= secondNum; i += 2 )
    {
        sumOdds += ( long long int )i * i;
    }
    
    printf( "The sum of squares of odd numbers is %llu", sumOdds );
    
    return 0;
}

Its output might look like

Enter two numbers: 10 20
The sum of squares of odd numbers is 1165

Some notes to the program. Firstly the variable i is used only within the for loop. So it should be declared in the minimum scope where it is used

for ( int i = firstNum + ( firstNum % 2 == 0 ); i <= secondNum; i += 2 )
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Secondly to reduce the risk of overflow it is desirable to declare the variable sumOdds at least as having the type long long int.

Upvotes: 4

Nico
Nico

Reputation: 3489

You can use a for loop without the increment.

for(i = firstNum; i <= secondNum;) {
    // checks if number is odd or even
    if (i % 2 != 0) {
        sumOdds += i * i;
        i += 2;
    }
    else {
        ++i;
    }
}

Gives you exactly the same behavior as your while loop.

Upvotes: 2

Adrian Mole
Adrian Mole

Reputation: 51825

You can replace with a for loop with ++i as its terminal statement; then, you can dispense with the whole else {...} block and replace the i += 2 (in the if block) with another ++i (which, combined with the terminal increment, will have the same net effect as i += 2).

Like this:

for (i = firstNum; i <= secondNum; ++i) {
    // checks if number is odd or even
    if (i % 2 != 0) {
        sumOdds += i * i;
        ++i;
    }
}

Upvotes: 1

Related Questions