Jump to content
xisto Community
beeseven

Need Help With C Program To Test If A Number Is Prime Ending unexpectedly somewhere near for-loop

Recommended Posts

Here is a simple C program for PRIMENeed Help With C Program To Test If A Number Is Prime

#include<stdio.H>#include<conio.H>#define PRIME 1#define NON_PRIME 0Void main(){int and,flag,I;Clrscr();Printf("Enter number:");Scanf("%d",&and);Flag=PRIME;    /* Assume Number is always PRIME */For(I=2;I<and;I++)  { if(and%I==0)    flag=NON_PRIME;  }if(flag==PRIME)  printf("and%d is PRIME",and);Else  printf("and%d is NOT PRIME.",and);getch();}

-reply by Ankit PokhrelKeywords:

Share this post


Link to post
Share on other sites
Plz help me i get Time limit exe... in 10200Need Help With C Program To Test If A Number Is Prime

 [10200]

#include<iostream>#include<math.H>Using namespace std;Int main(){   //freopen("1.Txt","r",stdin);   int a,b;   long long int c,pri[10000];   while((scanf("%d%d",&a,&B))==2)   {     int p,t=0,count=0;     for(p=a;p<=b;p++)     {       c=p*p+p+41;       long long int d=sqrt©;       long long int I;       int j=0;       for(I=1;I<=d;I++)       {         if(c%I==0)           j++;         if(j>1)           break;       }       if(j==1)       {           pri[t]=c;         t++;       }       count++;     }     float avg;     avg=float((t))/float(count)*100;     printf("%.2fand",avg);   }   return 0;}

-reply by Masum

Share this post


Link to post
Share on other sites

#include <stdio.H>#include<math.H>#include<conio.H>

main(){Clrscr();Printf("Enter a number: ");

int and;Scanf("%d", &and);

if(and == 2)Printf("%d is prime", and);Else if(and % 2 == 0 || and < 2)Printf("%d is not prime", and);Else{Int x;For(x = 0; x < (int)sqrt((double)and); x++)If(and % x == 0){Printf("%d is not prime", and);Return 0;}Printf("%d is prime", and);return 0;}

getch();Return 0;}

but it only answers 2 to 4...

-reply by kint

Share this post


Link to post
Share on other sites
number is prime or notNeed Help With C Program To Test If A Number Is Prime

#include<stdio.H>Main(){Int I,and;Printf("enter the number to be checkedand");Scanf("%d",&and);For(I=2;I<and;I++) { if(and%I==0)    {Printf("The number is not a prime numberand");Break;}Else{Printf("the number is a prime numberand");Break;   }}If(and==1){Printf("The number is neither a prime or nor a cmpositeand");} }

-reply by monish

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.