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

 
 


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

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 Pages:
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
Java Programming: Chapter 3 Quiz Answers
entered is even or odd. (You can use TextIO.getInt() to read the integer. Recall... the user whether the number is even or odd, requires an if statement to decide between... if (n % 2 == 0) // tell the user whether the number is even or odd
Java Programming: Chapter 3 Quiz
the user whether the number entered is even or odd. (You can use TextIO.getInt() to read the integer. Recall that an integer n is even if n % 2 == 0... to prime a loop? Question 5: Explain what is meant by an animation and how
Java Programming: Section 4.4
even be used as the test condition in an if, while, or do..while statement... currentN) { if (currentN % 2 == 1) // test if current N is odd... if (currentN % 2 == 1) // test if current N is odd
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... integers as x = 500, y = 70 and z = 3000.   List all even numbers between... the even numbers between two numbers. For this first create a class named

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.