Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried this code to print prime numbers 1 to 1000 by using the do-while loop but I can not get a proper answer for that please find the error of this code.

What I have tried:

<pre>#include <stdio.h>
int main()
{
    int num = 1 , count = 0;
    do
    {
        for(int i = 1;i<=num;i++)
        {
            if(num%i==0)
            {
                count++;
            }

       }
        if(count==2)
            {
                printf("%d",num);
            }
            num++;

    }while(num>=1000);


     return 0;
}
Posted
Updated 23-Jan-22 7:17am
Comments
Manuri hewage 23-Jan-22 8:04am    
sorry this code has not
 tag

You start by setting num = 1. You then create a loop that runs from 1 to the value of num. And you hope to run the loop while num is greater than or equal to 1000, which it will never be. You need something like:
C++
int num;
for (num = 1; num < 1000; num += 2) // loop through odd numbers  only
{
    // add your code here to check if num is a prime
}
 
Share this answer
 
Comments
CPallini 23-Jan-22 9:57am    
5.
Stefan_Lang 23-Jan-22 10:12am    
Actually, the while loop has a num++; statement right at the end. It's easy to miss because the indentation is wrong.
Richard MacCutchan 23-Jan-22 11:17am    
Yes, but it will break out at the end of the first iteration due to while(num>=1000);.
Stefan_Lang 25-Jan-22 8:02am    
indeed, i missed the '>' . Well spotted.
In other solutions, the bugs of your implementation are correctly addressed.
Anyway, the algorithm you are using is rather inefficient.
Have a look at this Wikipedia page Primality test - Wikipedia[^] for a better one.
 
Share this answer
 
There are 2 errors in your code.
1. couter veribale always incremented and if(count==2) will never met. reset count=0; before num++;
2. while(num<=1000);
Below is the code which will return exact prime numbers between 1 to 1000


#include <stdio.h>
int main()
{
    int num = 1 , count = 0;
    do
    {
        for(int i = 1;i<=num;i++)
        {
            if(num%i == 0)
            {
                count++;
            }
        }
        if(count==2)
        {
            printf("%d,",num);
        }
        count = 0;
        num++;
    }while(num<=1000);
    return 0;
}
 
Share this answer
 
v2
Comments
CPallini 23-Jan-22 9:57am    
5.
Quote:
but I can not get a proper answer for that

Your own code is a black box to you, there is a tool to help you understand what is going on, it is the debugger.

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900