Question: PRIME AND EVEN AND ODD

PRIME AND EVEN AND ODD
Posted in : Java Interview Questions

July 27, 2009 at 2:07 PM

Hi Friend,

1)Following code displays the prime numbers between 1 to 20.

public static void main (String args[]){
String primeNo = "";
int j=0;
System.out.println("Prime Numbers are:" );
for (int i = 1; i < 20; i++){ {
for ( j = 2; j < i; j++){
if (i % j == 0) {
break;
}
}
if (i == j) {
primeNo += i + " ";
}
}
}
System.out.println(primeNo);
}
}

2) Following code displays Even and Odd numbers between 1 to 20.

class EvenAndOddNumbers{
public static void main (String args[]){
String evenNo = "";
String oddNo = "";
for (int i = 1; i <=20; i++){
if (i % 2 == 0) {
evenNo += i + " ";
}
else{
oddNo +=i+ " ";
}
}
System.out.println("Even Numbers between 1 to 20 are: "+evenNo);
System.out.println("Odd Numbers between 1 to 20 are: "+oddNo);
}
}

Hope that it will be helpful for you.

Thanks

PRIME AND EVEN AND ODD - Java Interview Questions
PRIME AND EVEN AND ODD  how to write prime numbers?i want source code? how to write even numbers? i want source code? how to write odd numbers ?i... + " "; } } } System.out.println(primeNo); } } 2) Following code displays Even and Odd numbers
Explore Maharashtra
maintained roads of this serene hill station facilitate car driving and even horse.... Venna Lake a bit far from the town is a good place to go fishing, boating even... is the home of 350 odd forts which are almost rugged by the waves, scorching heat
Beginners Java Tutorial
; Check Even-Odd:-Example Now, you have to learn about the syntax... = 500, y = 70 and z = 3000.   List all even numbers between two numbers Here you will learn to write a program for listing out all the even
Tutorials