July 27, 2009 at 2:07 PM
Hi Friend,
1)Following code displays the prime numbers between 1 to 20.
class PrimeNumbers{
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
Related Tutorials/Questions & Answers:
PRIME AND EVEN AND ODD - Java Interview QuestionsPRIME 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
Advertisements
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...
three integers as x = 500, y = 70 and z = 3000.
List all
even numbers...
all the
even numbers between two numbers. For this first create a class named