+ 1

Whats wrong with this code?

//program to check prime no. #include<stdio.h> #include<conio.h> int main() { clrscr(); int count=1,i=1,num,d); printf("Enter A no"); while (i>=1 && i<=num) { d=(num/i); i++; if(d==1 || d==num) count=count+1; } if (count==2) printf("a prime no."); else printf("not a prime no."); getch(); return 0; }

1st Oct 2017, 6:16 AM
Ishu Maurya
Ishu Maurya - avatar
13 ответов
+ 1
1. use clrscr after variable declaration. 2. variable d is declared d) 3. capture the number input from user.
1st Oct 2017, 6:43 AM
balachandar
balachandar - avatar
+ 1
you havent define a value for your num variable. The while loop is ignored. And your d never created.
1st Oct 2017, 6:52 AM
Kostas Batz
Kostas Batz - avatar
0
have declared all variables as int and called clrscr function in a fn.(as i can call a fn anytime, anywhere). Its a program to calculate prime.. dont know y its not working
1st Oct 2017, 7:02 AM
Ishu Maurya
Ishu Maurya - avatar
0
count ==2. (the no. itself and one)
1st Oct 2017, 7:05 AM
Ishu Maurya
Ishu Maurya - avatar
0
count will not be 2 for 17 because d will be 1 for i of 9 through 17.
1st Oct 2017, 7:10 AM
John Wells
John Wells - avatar
0
as d is defined as int.. it will either take 1 or 2.... so it'll be 1 till 9. i think ?
1st Oct 2017, 7:13 AM
Ishu Maurya
Ishu Maurya - avatar
0
Better test r=num%i; if (r==0) count++;
1st Oct 2017, 7:14 AM
John Wells
John Wells - avatar
0
still shows wrong
1st Oct 2017, 7:24 AM
Ishu Maurya
Ishu Maurya - avatar
0
count should initialized to 0 not 1
1st Oct 2017, 7:29 AM
John Wells
John Wells - avatar
0
instead of using while try to use for loop and the biggest factor of any number wont be bigger than the half of that value. i'll write code for you give me 10 min posting here.
1st Oct 2017, 7:56 AM
Keshave Jat
Keshave Jat - avatar
0
sorry for the delay was busy here is you code https://code.sololearn.com/cvmG2iMbSFL9 writing this similar code with while also i'll update link later here is code with while loop https://code.sololearn.com/ct3I9NhaJ3TK
1st Oct 2017, 9:48 AM
Keshave Jat
Keshave Jat - avatar
- 1
once num is defined (say 5), you exit the loop with d = 5/5 = 1 so your test of d==2 will never be true.
1st Oct 2017, 7:02 AM
John Wells
John Wells - avatar