'How to print odd numbers ( 1 -> 10) by do - while

How to print odd numbers ( 1 -> 10) by do - while?

My code: http://codepad.org/yS6DNq8Y

#include <stdio.h>
#include <conio.h>
 int i;
void Dayso()
{

    do 
    {
        i = 1
        i++;
        if ( i % 2 == 0 )
        {
            continue;
        }
        printf ("\n%d",i);

    }while (i <= 10 );

}

int main()
{
    Dayso ();
    getch();
    return 0;
}

and the output:

Line 18: error: conio.h: No such file or directory
In function 'Dayso':
Line 10: error: expected ';' before 'i'

How do I fix this?



Solution 1:[1]

Compile errors:

  1. There is no conio.h header file in Linux machines. You can remove getch() function in this program.
  2. You are missing semicolon in line 9.

Logic errors:

  1. You are assigning 1 to i variable (9 line) on every do while iteration so you have just created infinite cycle. Move assignment to 1 outside the loop.
  2. You are missing 1 from ods and 11 gets printed in current implementation.

Corrected solution: http://ideone.com/IB3200

#include <stdio.h>

void Dayso()
{
    int i = 1;
    do 
    {
        if ( i % 2 != 0 ) {
            printf ("\n%d",i);
        }

        i++;
    } while (i <= 10 );

}

int main()
{
    Dayso ();
    return 0;
}

Solution 2:[2]

int a = 2;

    do {
        System.out.println(a);
            a++;
        if (a%2==0);
        a++;
    }

    while (a <= 20);
    }
}

// for printing even number using do-while loop in java

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Spikatrix
Solution 2 Aayushi_17 .Mishra