-1

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?

Paul
  • 26,170
  • 12
  • 85
  • 119
  • 2
    The error messages are way clear: start adding a `;` at the end of row 9. About the error `Line 18: error: conio.h: No such file or directory`, [this might help you]http://stackoverflow.com/questions/8792317/why-cant-i-find-conio-h-on-linux). – il_raffa Aug 15 '15 at 07:13
  • 1
    What is the very first thing done in the loop body (and thus done every loop)? Why (is this a problem)? – user2864740 Aug 15 '15 at 07:18
  • Move `i=1` to outer scope and add semicolon to it. – Mitat Koyuncu Aug 15 '15 at 07:38

2 Answers2

3

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;
}
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Vycius
  • 31
  • 1
  • 3
0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '22 at 08:11
  • The edit queue is full so I cant fix the obvious errors: starting at ` a =2` and continuing to `a <= 20` means you miss 1 and get 11/13/15/17/19 - so you haven't answered the question... likewise, the question is tagged `C` not java. – Andrew Feb 28 '22 at 10:11