Why this C program of prime number is wrong? [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Please help ! Why this Prime number Program in C is not working ?
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
}
getch();
}
Please answer-- Why This program is not working ??

Use a flag to check whether the number is divisible or not?
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,flag=0;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
flag = 1;
break;
}
}
if ( flag == 1 )
{
printf("%d is not a prime number",n);
}
else
{
printf("%d is a prime number",n);
}
getch();
}
Also, the for loop: for(i=2;i<=n-1;i++) iterates more than required. You should set i <= sqrt(n).

Related

Why is my program to check for prime numbers showing numbers with last digit 5 as prime numbers? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I wrote a simple and short program to check for prime numbers in C. It is showing normal results for all numbers except for those numbers which have last digit as 5 by printing them as prime numbers, even those numbers which are clearly not prime, like 15, 25, 45 etc.
My question is that why is it doing so and how can I fix it?
Here is my code -:
#include<stdio.h>
int main()
{
int num, count=0;
printf("\nEnter a number : ");
scanf("%d",&num);
for(int i=2; i<=num/2; i++)
{
if(num%i==0)
count=1;
break;
}
if(count==1)
printf("\nNot a prime number.");
else
printf("\nPrime Number.");
}
Any help would be great.
Thank You.
Your program executes the for cycle only once, since the removal of the curly brackets causes only the line count=1 to be executed when the if condition becomes true. Thus, the break command is ALWAYS executed. You could fix it by writing the line as:
if (num%i==0) {
count=1;
break;
}
Hope this helps!
#include<stdio.h>
int main()
{
int num, count=0;
printf("\nEnter a number : ");
scanf("%d",&num);
for(int i=2; i<=num/2; i++)
{
if(num%i==0){ // you have to add {} for multi-line code
count=1;
break;
}
}
if(count==1)
printf("\nNot a prime number.");
else
printf("\nPrime Number.");
}

I am making a prime number checker: when I try to run it, it stopped working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include <stdio.h>
int main() {
int x, i, counter = 0;
printf("Input number!\t");
scanf("%d", &x);
for (i = 0; i <= x; i++) {
if (x % i == 0) {
counter++;
}
}
if (counter <= 2) {
printf("%d is a prime number.", x);
} else {
printf("%d is not a prime number.", x);
}
return 0;
}
It seems the loop part is the problem but I don't know why. I'm very new to programming so please bear with it if its a silly mistake.
Try this Code.
Its going to Infinity when divide by ZERO after giving input. Make
sure loop start with 1, when division inside loop
#include <stdio.h>
int main()
{
int x,i,counter=0;
printf("Input number!\t");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
if(counter<=2)
{
printf("%d is a prime number.",x);
}
else
{
printf("%d is not a prime number.",x);
}
return 0;
}
As #Some programmer dude mentioned, you can't do the x%0 because of division by 0 - this occured on first iteration.
So change your loop to starting from 1 like below:
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}

what is wrong with this code? (to check whether a given number is a prime number) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
#include <stdio.h>
#include <conio.h>
void main() {
int a, n, x;
clrscr();
printf("enter a number");
scanf("%d", &a);
n > 1;
a != n && n < a;
if (a / n == x)
printf("a is not a prime no");
else
printf("a is a prime no");
}
If I run this and put a composite number, it still shows it as prime.
your if statement is never true duo to n and x are not initialized. Therefore you only get your else as return. Moreover your expression n>1; and a != n && n < a; return a bool which is not compered to anything. In that case you need to use a for loop.
Here is a link About for loops
int main()
{
int a,n,x = 0;
printf("enter a number: ");
scanf("%d",&a);
for(n=2; n<=a/2; ++n)
{
if(a%n==0)
{
x=1;
break;
}
}
if (x==0)
printf("",n);
else
printf("a is not a prime no");
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();//clearing the screen
int n,x=2,count=0;//Here count is initialised to 0,if it is not prime it remains the same,else it will be equal to 1.You will understand this as you go down
//A number is a prime number if it is not divisible by any other number from 2 and the number before it.
printf("Enter a number : ");
scanf("%d",&n);
while(x<n)//As this checking process should continue till the number just preceding it
{
if(n%x==0)//checking if the number n is divisible by x or not
{
count++;//IF divisible,there is no meaning in continuing,So we are coming out of the loop by incrementing the variable "count"
break;
}
else
x++;
}
if(count==0)
{
printf("%d is a prime number",n);
return 0;//Here if number is prime,There is no need to go further and execute till end,To reduce time complexity ,We will write a return statement to stop executing the code.
}
printf("%d is not a prime number",n);
return 0;
}

Program to find next immediate prime number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
void main()
{
int n,i,j,flag=0,out;
clrscr();
printf("enter the num\n");
scanf("%d",&n);
for(i=n+1;i<=100;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("next prime is:%d",i);
break;
}
}
getch();
}
In this code if i entered input as 8 . Then i=9,j=2
then 9%2==0 the condition becomes false and 9 will be printed as next prime number which is wrong answer.Please give me correct solution for this program.
I checked your program and it produces the correct output.
Now for explanation, for the case you described
In this code if i entered input as 8 . Then i=9,j=2 then 9%2==0 the
condition becomes false and 9 will be printed as next prime number
which is wrong answer.
So, it will get (9%2 == 0) => false. Then it will not check the if(flag==0) condition and print 9 as next prime. instead, it will increase j and continue looping till j<i.
Hence, it will produce correct answer.
Check the following code :
void main()
{
int n,i,j;
clrscr();
printf("Enter the number : ");
scanf("%d",&n);
for(i=n+1;;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
break;
}
if(j==i)
{
printf("The next prime number is :%d",i);
break;
}
}
getch();
}

Prime numbers for a given range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include<stdio.h>
int main()
{
int i, j, a, b, c=0;
scanf("%d %d", &a, &b);
for(i=a; i<=b; i++)
{
for(j=1; j<=i; j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
printf("%d\n", i);
}
}
return 0;
}
The program however does not print prime numbers for a given range. Please help.
You have to reset c to 0 after every iteration. The loops should look like this
for(i=a; i<=b; i++)
{
c = 0;
...
An advice, you don't have to go till the number everytime to check for primality, you can go till square root of that number.

Resources