To convert a 'for' loop into a 'do..while' loop

convert to pdf
convert jpg to pdf
how to convert a file to pdf
convert pdf to editable word
convert image to jpg 20kb
convert jpeg to jpg online
convert antonym
i love pdf

I came across a program to print all the prime numbers between 1 to n, where 'n' is the value supplied by the user. It used the for loop. The program is as follows-

#include <stdio.h>

int main()
{
    int i, j, end, isPrime; // isPrime is used as flag variable

    /* Input upper limit to print prime */
    printf("Find prime numbers between 1 to : ");
    scanf("%d", &end);

    printf("All prime numbers between 1 to %d are:\n", end);

    /* Find all Prime numbers between 1 to end */
    for(i=2; i<=end; i++)
    {
        isPrime = 1; 
        for(j=2; j<=i/2; j++)
        {
            if(i%j==0)
                isPrime = 0;
        }
        if(isPrime==1)
        {
            printf("%4d", i);
        }
    }
    return 0;
}

I very well understand the above code. But just to test my knowledge , I tried writing the same program using the do loop. It didn't work out very well. I searched through books and internet to find a program where they use a do loop to compute prime numbers till n. But couldn't find one. I wanted to know if its even possible or not. To show my efforts, I put below the code I made-

#include <stdio.h>
main()
{
    int i, j, n, isPrime;
    printf("Enter n\n");
    scanf ("%d", &n);
    i = 2;

    do 
    {
        do
        {
            j = 2;
            if (i%j == 0)
                isPrime = 0;
            else
            {
                isPrime = 1;
                printf ("%d", i);
            }
            j = j+1;
        }
        while (j <= i/2);

        i = i+1;
    }
    while (i <= n);
}

You had a good start, but made a few small errors which resulted in big problems

j=2 was set in the second do-while loop. This resulted in j being set to 2 every loop, thus never exiting the do while loop.

Next was the if/else. In the for loop example, the if (isPrime==1) was outside the for loop. You were checking inside the while loop resulting in things being named prime way to soon.

Below is an example of the working code

#include <stdio.h>
main()
{
    int i, j, n, isPrime;
    printf("Enter n\n");
    scanf ("%d", &n);
    printf("All prime numbers between 2 to %d are:\n", n);

    i = 2;
    do 
    {
        isPrime = 1;
        j = 2;

        do
        {
            if (i%j == 0)
                isPrime = 0;
            j = j+1;
        } while (j < i/2);

        if (isPrime == 1 || i == 2)
            printf ("%d\n", i);
        i = i+1;
    }
    while (i <= n);
}

Convert, Convert definition is - to bring over from one belief, view, or party to another. How to use convert in a sentence. Synonym Discussion of convert. Aconvert.com is an extensional site of PDFConvertOnline.com we created. Not just PDF, Aconvert.com can also convert all kinds of document, ebook, image, icon, video, audio and archive files online for free. The letter "A" in domain name stands for "All".

here is the code that you need.

#include <stdio.h>
main() {
  int i, j, n, isPrime;
  printf("Enter n\n");
  scanf("%d", &n);
  i = 2;
  do {
    j = 2;
    isPrime = 1;
    if (i == 2)
      printf("%d ", i);
    do {
      if (i % j == 0) {
        isPrime = 0;
        break;
      }
      j = j + 1;
    } while (j <= i / 2);
    if (isPrime)
      printf("%d ", i);
    i = i + 1;
  } while (i <= n);
}

Word to PDF, No limits to file size, no ad watermarks - just a free and simple online PDF converter to create PDFs with your Word documents. Convert definition is - to bring over from one belief, view, or party to another. How to use convert in a sentence. Synonym Discussion of convert.

A for loop

for (INIT; CONDITION; NEXT) {
    BODY;
}

is equivalent to the while loop

{
    INIT;
    while (CONDITION) {
        BODY;
        NEXT;
    }
}

(except that the for loop does not allow declaring the same name in the INIT and in the outermost block of the BODY).

You made a couple of mistakes in your conversion:

  1. The do { BODY; } while (CONDITION); syntax always executes the BODY at least once. The original for loop might not.

This matters for the case i==2: In the original code, the inner for loop will set j=2, then immediately test whether j<=i/2, which is 2<=1 or false, so the loop body won't execute at all. But your code starts by setting i=2; j=2; then going right to the factor test if (i%j == 0). This is true, so it incorrectly concludes 2 is not a prime.

  1. You moved the isPrime = 1; and printf statements from the outer loop over i to be inside the inner loop over j. This changes the program's behavior in a bad way.

Consider what happens when i==15: We start with j=2, and find that i%j != 0 since 2 is not a factor of 15. So it sets isPrime = 1; and prints 15. The inner loop continues, setting j=3. This time i%j == 0 since 3 is a factor of 15, so it sets isPrime = 0; and prints nothing. The inner loop goes again with j=4. Now it's back to i%j != 0, so it sets isPrime = 1; and prints 15 again. This continues up to j=7, printing 15 a total of four times (when j is 2, 4, 6, and 7).

I'll leave it to you to see if you can now correct the code using while loops.

PDF to Word Converter - 100% Free, The best quality PDF to Word conversion on the market - free and easy to use. No watermarks - convert PDF to Word in seconds. Convert a PDF to a Word Document Using Microsoft Word. You can also convert your PDF to a Word document using just Microsoft Word. Converting in Word often leads to style and formatting issues, so it’s not always the most reliable method.

Best PDF Converter: Create, Convert PDF Files Online (FREE), PDF Converter is a online web-based document to PDF converter software. Convert and create PDF from various types of files like Word DOC, Excel XLS,  Learn how to easily convert an Adobe PDF file to a Microsoft Word file. I use Microsoft Word that comes with Office 365 to do the conversion. The conversion maintains all images, tables, and text.

PDF to Word Converter - 100% Free, Enhanced PDF to Word converter - Bigger files (10 MB), DOCX output. Upload a File to Convert. How to convert a Word document to a Google Doc on desktop. 1. Ensure you're logged into your Google account and then navigate to https://drive.google.com. 2.

PDF Converter, Word, Excel, JPG and HTML files will automatically be converted to PDF format. When you upload a PDF file, select the file type you want to convert to and leave​  Convert a Document to PDF Using Word. If you have the desktop version of Microsoft Word, the easiest way to convert your document to PDF is right in Word itself. Open the document you want to convert, and then click the “File” tab. On the backstage screen, select “Save As” from the list on the left.

PDF to Image – Convert PDF to JPG Online, Free online service to convert a PDF file to a set of optimized JPG images. It offers mass conversion and allows files up to 50 MB. How to Convert a School Bus or Van into a Tiny Home or Recreational Vehicle, covers every step of the process, along with every hard-earned time-, sweat-, and money-saving insight I’ve picked up

Comments
  • 2 is also a prime number.
  • Ah, forgot about that bit... I get a bit confused about different kinds of loops sometimes