Need Help.!!!!!

It's about C programing atually.
I was writing a program to check whether number is prime or not.
.
I used while loop.
program i wrote -
-------------------------------------------------

#include<stdio.h>
#include<conio.h>
main()
{
int a,i;
i=2;
printf("Enter the number\n\n-->");
scanf("%d",&a);
while(a%i==0 && i<a)
{printf("Number is composite");}
i++;
printf("\n\nNumber is prime");
getch();
}
----------------------------------------------
.
here in output if number is prime it shows - number is prime
if it's composite then it goes on typing "number is composite" infinite times...
.
.
HELP ME ON THIS PLEASE.
I'm just a beginner.
i++ is outside the loop. Your loop only checks whether the number is odd, not prime as i is always 2 in the loop.
Note that even if you do that, it will still not work right. Your logic is wrong. if a is odd, the loop will not be entered no matter what you write in it.
i see .



#include<stdio.h>
#include<conio.h>
main()
{
int a,i;
i=2;
printf("Enter the number--> ");
scanf("%d",&a);
if(a%i==0 && i<a)
printf("\nNumber is composite\n");
else
printf("\nNumber is prime\n");

}
thnx for help.
But i got the solution.
here -
-------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
main()
{
int a,i;
i=2;
printf("Enter the number\n\n-->");
scanf("%d",&a);
if(a%i==0 && i<a)
{printf("\n\nNumber is composite");}
else
{printf("\n\nNumber is prime");}
i++;
getch();
}
---------------------------------------------------------------
@ googleking.
.
hey u check only if number is divisible by 2 or not. That is not test for prime. It is test for even and odd.
The program i wrote(corrected-lataste version) is correct. I have checked output. It's perfect. :)
VirSawant, googleking
You both posted the exact same code. You both just test for evens or odds. Just try 9 and see how it fails.
To check if a number is prime you need to check that it is not divisible by any number smaller than itself.
Have a for loop i = 2 to a-1. If a is divisible by i, print "not prime" else keep looping.
@ hamsterman: Boring! Use Trial Division: http://en.wikipedia.org/wiki/Trial_division
This way the OP may learn some actual math coding...
oh ya i see
but for some numbers it's working properly.
67 - prime
12- composite
...
what shoud i do now???
i initiated i with 2 and after checking divisiblity i increased it by 1 n then agained checked... untill i<the given number... what's wrong with this????
@ OP: Use a different technique to determine if a number is prime or not. Both hamsterman and I have given valid answers on what to do next.
thnx.
i'll see trial division and let you people know.
I'm a beginner... will need help ...
@hamsterman -
***********************************************
#include<stdio.h>
#include<conio.h>
main()
{
int a,i;
i=2;
printf("Enter the number\n\n-->");
scanf("%d",&a);
for(i=2;i++;i<a)
{if(a%i==0)
{printf("\n\nNumber is composite");}
else
{printf("\n\nNumber is prime");}
}
getch();
}
******************************************
what about this??????????
Last edited on
superb...............
thank u all
.
@computergeek01
i'll also check about trial division.. just reading that...
.
*************************************************
#include<stdio.h>
#include<conio.h>
main()
{
int a,i;
i=2;
printf("Enter the number\n\n-->");
scanf("%d",&a);
for(i=2;i++;i<a)
{if(a%i==0)
{printf("\n\nNumber is composite");
break;}
else
{printf("\n\nNumber is prime");
break;}
}
getch();
}
********************************************
no. read up on for loops.. do you test your code at all ? (Try 25)
also, http://www.cplusplus.com/forum/articles/42672/
and properly format your code..
ya i tested for 25 ............. it's not working again!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :(
but why is it so... it's now showing 9 a composite... so i was happy.. but today i checked 25 ... shet... it says prime...................................
Firstly, the syntax of for loop is for( initial state; condition; increment ){ ... }. You mixed that up. Secondly, both if conditions result in a break, so that loop will never loop.
Read my post where I explained the algorithm again.
Topic archived. No new replies allowed.