Wednesday, 15 August 2012

java - I can;t get all my prime numbers listed out. What's wrong? :S -



java - I can;t get all my prime numbers listed out. What's wrong? :S -

what im trying do: write java programme accepts integers command line argument and; - display number of integers passed - display smallest , largest number - display prime no in list*

import java.util.*; class commandline { public static void main(string [] args) { int a[] = new int[args.length]; int count = 0; (int = 0; i<a.length;i++) { a[i] = integer.parseint(args[i]); count++; } system.out.println("number of digits: " +count ); int big = a[0]; int little = a[0]; int num = 0; (int j = 0; j<a.length; j++) { num = a[j] ; if(num>large) { large=num; } if(num<small) { small=num; } } system.out.println("the largest is:" + large); system.out.println("smallest no : " + small); boolean isprime = true; (int = 2; i<a.length;i++){ (int j=2; j<i; j++) { if((i % j)== 0) { isprime=false; break; } } if(isprime) { system.out.println(i + " prime"); } isprime = true; } } }

your prime number check checks numbers 2 a.length, whereas should check elements of array a.

boolean isprime; (int : a) { isprime = true; (int j=2; j<i; j++) { if ((i % j)== 0) { isprime=false; break; } } if(isprime) { system.out.println(integer.tostring(i) + " prime"); } }

this should help - disclaimer: did not run code.

java

No comments:

Post a Comment