I am a beginner to java and I have problem with this code, how the (i == num) thats my problem. because i's value getting after the loop i<num. hows it's equle to num. please send me the reply
import java.util.Scanner;
public class Prime_number
{
public static void main(String[] args)
{
Scanner k = new Scanner (System.in);
int i=2;
int a = k.nextInt();
while (i < a )
{
int n = a%i;
{
if (n==0)
{
System.out.println("Not Prime");
break;
}
}
i++;
}
if (i == a)
{
System.out.println("Prime number!");
}
}
}
Hey, just a question, but i could never equal the prime number because its a simple < symbol in the for loop right? if not please explain. is it because the ++ is added during the loop?
why i can't use b's value out side the loop,where b is global.
class a
{
public static void main(String arg[])
{
int a=Integer.parseInt(arg[0]);
int i,b;
for (i=2;i<a;i++)
{
b= a%i;
}
if(b==0)
{
System.out.println( a +" is
not prime");
}
else
{
System.out.println(a+"Prime
number!");
}
}
}
Dude, this is a really speculative piece of code. The only thing it attempts to validate is the integer 11 as a prime number. It does that in both parts of your conditional statement, even though you used the string words "Not Prime"!! What about the other prime numbers that exist in the variable i, that is, 3, 5 and 7. That piece of code fails to capture them.
class abc
{
void display()
{
int a=11;
int x;
int c=0;
for(x=1;x<=a;x++)
{
int d=(x%a==0);
c++;
}
if(C==2)
system.out.println("prime");
else
system.out.println("not prime");
}
}
how (i==num) Dissanayake June 1, 2011 at 11:38 AM
I am a beginner to java and I have problem with this code, how the (i == num) thats my problem. because i's value getting after the loop i<num. hows it's equle to num. please send me the reply
how is working this loopfaisal June 24, 2011 at 11:01 AM
i have this error from this program, Exception in thread "main" java.lang.NoClassDefFoundError: Prime_number/java
ThanksRinku Rajole August 13, 2011 at 7:03 PM
i will try this program lots of time but it give error. Butnow my problem is solved so thanks.
Another SolutionApurba August 17, 2011 at 3:07 PM
import java.util.Scanner; public class Prime_number { public static void main(String[] args) { Scanner k = new Scanner (System.in); int i=2; int a = k.nextInt(); while (i < a ) { int n = a%i; { if (n==0) { System.out.println("Not Prime"); break; } } i++; } if (i == a) { System.out.println("Prime number!"); } } }
primejacinth1215 September 9, 2011 at 6:16 AM
what if i like to use "while"?
Code inefficientAnonymous February 19, 2013 at 2:39 PM
This code is not efficient
formula to find out prime numberDebasish Nanda October 10, 2011 at 3:59 PM
sir/medam please tell me the formula to find out the prime number
Assignmentszs October 20, 2011 at 3:35 PM
good code
doubtnaga October 26, 2011 at 10:16 AM
in this check prime number program how "i" value becomes 11 ie if(i==num) { PRINT(is prime); }
computer sciencekrishnakant November 15, 2011 at 5:02 PM
this very useful for bunkers
ISC Computer Applications - Grade 11Nabil December 6, 2011 at 1:22 AM
In the last "if..." condition, the condition should be if (num - i == 1), as you are running the for loop from i = 2, to i < num.
ThanksNadav David December 25, 2011 at 7:41 PM
It helped me a lot
javaprajot January 17, 2012 at 7:40 PM
how the loops and break are used ??
OptimisationBertie Wheen March 13, 2012 at 4:51 AM
instead of for (i=2; i < num ;i++ ) use for (i = 2; i < (int)Math.sqrt(num); i++)
ProgramAnanimuz March 23, 2012 at 9:50 AM
Hey, just a question, but i could never equal the prime number because its a simple < symbol in the for loop right? if not please explain. is it because the ++ is added during the loop?
prime no. programmesandeep kamboj April 2, 2012 at 11:48 AM
its very good explanation. thnks for good work
want to learn from mistake rahul June 11, 2012 at 6:02 AM
why i can't use b's value out side the loop,where b is global. class a { public static void main(String arg[]) { int a=Integer.parseInt(arg[0]); int i,b; for (i=2;i<a;i++) { b= a%i; } if(b==0) { System.out.println( a +" is not prime"); } else { System.out.println(a+"Prime number!"); } } }
Java program of prime numbersBobby k bose June 14, 2012 at 8:10 PM
Your java program of finding "prime numbers " did really helped me. Thanks for the program!
this codeRaymond June 16, 2012 at 3:13 PM
Dude, this is a really speculative piece of code. The only thing it attempts to validate is the integer 11 as a prime number. It does that in both parts of your conditional statement, even though you used the string words "Not Prime"!! What about the other prime numbers that exist in the variable i, that is, 3, 5 and 7. That piece of code fails to capture them.
upleaseshutupchetan August 20, 2012 at 10:42 AM
class abc { void display() { int a=11; int x; int c=0; for(x=1;x<=a;x++) { int d=(x%a==0); c++; } if(C==2) system.out.println("prime"); else system.out.println("not prime"); } }
mathsmohammad September 16, 2012 at 7:59 PM
how can i the number is prime
Post your Comment