Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Interview-Questions PRIME AND EVEN AND ODD
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


bhaskarreddy
PRIME AND EVEN AND ODD
1 Answer(s)      5 years and 4 months ago
Posted in : Java Interview Questions



Advertisement
View Answers

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 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
Advertisements
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
Advertisements
 

 

 

DMCA.com