Member Avatar for Josue198s
#include<stdio.h>

 int main()
{

    int pcheck=1,i;
    int k=1000



        for(i=2;i<k;i++)     
        {

            if((k%i)== 0)
            {
                pcheck=0;

                break;  
            }


        }



        if(pcheck)  
        {
            printf("%d is a prime .\n",k);

        }   
        else
        {
            printf("%d is not a prime.\n",k);


        }



    return 0;
}

this code works to check any number i that write in k but when i put a do while loop.stating that do while should loop while k!=0 then i k-- so that can check if all the number less than 1000 are prime. it prints all not prime..please guys help me

If you're having problems with a do loop, you need to post up code that includes the do loop. ;)

Using CODE TAGS! <of course> Putting your code right between code tags, lets your code stay looking like code, and not like a dog's breakfast.

Your code is not formatted.

Wrong
int k=100;

Correct
int k = 100

#include<stdio.h>

int main()
{

int pcheck = 1,i;
int k = 1000

for(i = 2;i < k;i++)
{

if((k % i) == 0)
{
pcheck = 0;

break;
}
}

if(pcheck)
{
printf("%d is a prime .\n",k);
}
else
{
printf("%d is not a prime.\n",k);
}

return 0;
}

Indentation is pretty important too, especially when you have a problem with looping

#include<stdio.h>
 
int main()
{
	int pcheck = 1,i;
	int k = 1000
 
	for(i = 2;i < k;i++)
	{
		if((k % i) == 0)
		{
			pcheck = 0; 
			break;
		}
	}	
 
	if(pcheck)
	{
		printf("%d is a prime .\n",k);
	}
	else
	{
		printf("%d is not a prime.\n",k);
	}

	return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
 int flag=0;
 int a=11,i,temp,temp=a;
 for(i=1;i<=temp;i++)
 {
if((a%i)==0)
{flag++;
}
}
if(flag==2)
{
 printf("prime");
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.