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();
}
Related
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.");
}
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 years ago.
Improve this question
I use the following code to count the amount of digits in a while loop, so "0" should be 1, "10" should be 2 etc. - however the code does not seem to work. Can you please help me?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
division=x/10;
counter++;
}
while(division!=0);
printf("This number contains : %d digits",counter);
return 0;
}
please change division=x/10; to x /= 10 and corresponding while condition. x is not changed your code, thus you get stucked in you while loop forever
This line:
division = x / 10;
Will be performed forever since the condition given in the while logic never becomes false.
If you do:
do {
x = x / 10;
counter++;
} while (x != 0);
It'll work.
Enhanced version of your code:
#include <stdio.h>
int main() {
int x;
int counter = 0;
printf("Enter a number : ");
// looping until a correct format is provided
while (scanf("%d", &x) == 0) {
printf("Incorrect values, enter again: ");
fseek(stdin, 0, SEEK_END);
}
do {
x = x / 10;
counter++;
} while (x != 0);
printf("This number contains : %d digits.", counter);
return 0;
}
The intention behind the "enhanced version" is to verify if the input is correctly given as formatted in coding (i.e. accepting an integer and nothing else) which isn't in your program.
Also, you don't need to include stdlib.h for your own code. That works without it too.
You'll then get the following sample output:
Enter a number : asdlfjal;sdk
Incorrect values, enter again: asdf sdf
Incorrect values, enter again: 33334
This number contains : 5 digits.
You are not changing the division value. This should work
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
int division;
int counter=0;
printf("Enter a number : ");
scanf("%d",&x);
do
{
x=x/10;
counter++;
}
while(x!=0);
printf("This number contains : %d digits",counter);
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Problem is :- Write a C code that ask the user to enter 10 numbers, then ask him to enter another number to search on it in the 10 numbers and print its location in case it is found. In case the number is not found, it will print number no exist.
Code I write is :-
#include<stdio.h>
void main(void)
{
int n, i, value,j;
for (i=1;i<=10;++i)
{
printf("enter number %d : ",i);
scanf("%d",&n);
}
printf("Enter the value to search: ");
scanf("%d",&value);
/*next part of code is not correct
that can not search to find the place of number */
for (j=1;j<=10;++j)
{
if (value == n)
{
printf("value is exist at element number %d",n);
}
else
{
printf("value is not exist\n");
}
}
}
output will be:-
(after enter the numbers).
Enter the value to search is 12.
value is exist at element 9
#include <stdio.h>
int main(){
//there are 10 int in n
int n[10], i, value;
for (i=0;i<10;i=i+1)//array count from 0 ,i=i+1 same as i++
{
printf("enter number %d : ",i);
scanf(" %d",&(n[i]));//& mean get address so you will push what you input to n[0]~n[9]
//little tip before %d remain a space for some reason if you keep learn you will know
}
printf("Enter the value to search: ");
scanf(" %d",&value);//& mean get address so you push what you input to value here
for (i=0;i<10;i=i+1)
{
if (value == n[i])
{
printf("value is exist is element number %d\n",n[i]);
break;//break mean out of for loop
//
}
}
if(i==10){//if search all not found then i will be 10 because after loop i will +1
//if break i will not +1
printf("value is not exist\n");
}
return 0;//remember "int" main() so you need return 0
}
Keep learning you will be stronger
Array is simple
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).
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;
}