- package com.instaceofjava;
- public class primenumbers {
- public static void main(String[] args) {
- int num=50;
- int count=0;
- for(int i=2;i<=num;i++){
- count=0;
- for(int j=2;j<=i/2;j++){
- if(i%j==0){
- count++;
- break;
- }
- }
- if(count==0){
- System.out.println(i);
- }
- }
- }
- }
Output:
- 2
- 3
- 5
- 7
- 11
- 13
- 17
- 19
- 23
- 29
- 31
- 37
- 41
- 43
- 47
Java programming interview questions
- Print prime numbers?
- What happens if we place return statement in try catch blocks
- Write a java program to convert binary to decimal
- Java Program to convert Decimal to Binary
- Is it possible to print message without using System.out.println()
- Java program to restrict a class from creating not more than three objects
- Java basic interview programs on this keyword
- Java Program to Sort elements of Java ArrayList Example
- Interfaces allows constructors?
- Can we create static constructor in java
- Super keyword interview questions java
- Java interview questions on final keyword
- Can we create private constructor in java
- Java Program Find Second highest number in an integer array
- Java interview programming questions on interfaces
- Top 15 abstract class interview questions
- Java interview Questions on main() method
- Sort employee object by id in descending order using comparable and TreesSet
- Top 20 collection framework interview Questions
- Java Interview Program to find smallest and second smallest number in an array
- Java Coding Interview programming Questions : Java Test on HashMap
- Explain java data types with example programs
- How to check internet connection using java
- Constructor chaining in java with example programs
- Top 10 Interview Programs and questions on method overriding in java
- Swap two numbers without using third variable in java
- Find sum of digits in java
- How to create immutable class in java
- AtomicInteger in java
- Check Even or Odd without using modulus and division
- String Reverse Without using String API
- Find Biggest substring in between specified character
- Check string is palindrome or not?
- Reverse a number in java?
- Fibonacci series with Recursive?
- Fibonacci series without using Recursive?
- Sort the String using string API?
- Sort the String without using String API?
- what is the difference between method overloading and method overriding?
- How to find largest element in an array with index and value ?
- Sort integer array using bubble sort in java?
- Object Cloning in java example?
- Method Overriding in java?
- Program for create Singleton class?
- Print numbers in pyramid shape?
- Check armstrong number or not?
- Producer Consumer Problem?
- Remove duplicate elements from an array
- Convert Byte Array to String
- Print 1 to 10 without using loops
- Add 2 Matrices
- Multiply 2 Matrices
- How to Add elements to hash map and Display
- Sort ArrayList in descending order
- Sort Object Using Comparator
- Count Number of Occurrences of character in a String
- Can we Overload static methods in java
- Can we Override static methods in java
- Can we call super class static methods from sub class
- Explain return type in java
- Can we call Sub class methods using super class object?
- Can we Override private methods ?
- Basic Programming Questions to Practice : Test your Skill
- Java programming interview questions on collections
how this count functions in this program?
ReplyDeletepublic class Print_prime {
ReplyDeletepublic void findPrime (int start_number, int last_number){
System.out.println("prime number between "+start_number+" and "+last_number
+" are");
for (int i = start_number; i < last_number+1; i++) {
if(i%2 == 0){
System.out.println(i);
}
}
}
public static void main(String[] args) {
Print_prime pp = new Print_prime();
pp.findPrime(10, 101);
}
}
your program is to print even numbers, not prime
DeleteYes, the above program is not correct. To print prime numbers, the logic mentioned here is not correct. Please see correct logic to print all prime numbers till the input upperbase number
Deleteimport java.util.Scanner;
public class PrimeNumbers {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrimeNumbers object = new PrimeNumbers();
object.printPrimeNumbers();
}
public void printPrimeNumbers(){
int upperBase;
int num;
System.out.println("Enter the upper base number:");
Scanner input = new Scanner(System.in);
upperBase = input.nextInt();
input.close();
System.out.println("\nPrime numbers between 0 and "+upperBase+" are:\t");
for(int i=2; i<=upperBase;i++){
int count=0;
for(num=i;num>1;num--){
if(i%num==0){
count++;
}
}
if(count==1){
System.out.println(i);
}
}
}
}
nice post
ReplyDeleteGood work sir, Thanks for the proper explanation about JAVA . I found one of the good resource related JAVA and OOPS concepts. It is providing in-depth knowledge on JAVA and OOPS. which I am sharing a link with you where you can get more clear on JAVA and OOPS. To know more Just have a look at this link
ReplyDeleteJava Tutorial
Class and object
Inheritance
Polymorphism
Abstraction
Encapsulation
,,