The number which is divisible by itself and 1 is known as Prime Number, eg :- 5 is a prime number because it is only divisible by itself and 1. Using Java
Continue reading Program to check whether input number is prime or notProgram to check whether input number is prime or not
The number which is divisible by itself and 1 is known as Prime Number, eg :- 5 is a prime number because it is only divisible by itself and 1.
Using Java
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println("Enter any number:");
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + " is a Prime Number");
else
System.out.println(num + " is not a Prime Number");
}
}
Output:
Enter any number:
19
19 is a Prime Number
Output 2:
Enter any number:
6
6 is not a Prime Number
Java Program to Add two Numbers Program to check number is even or odd Program to check Leap Year Program to check whether input number is prime or not Program to display the prime numbers from 1 to 100 Program to find the smallest of three numbers using ternary operator Program to check Vowel or …
Continue reading Basic ProgramsBefore we see the program, lets see how to determine whether a year is a leap year mathematically:To determine whether a year is a leap year, follow these steps:1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.2. If the year is evenly divisible by 100, go …
Continue reading Program to check Leap YearUsing Java Using Python Using Javascript Using C#
Continue reading Program to check number is even or oddUsing Java Sum of two numbers without User input Sum of two numbers with User input The Scanner is class that allows us to capture the user input so that we can get the values of both the numbers from user. The program then calculates the sum and displays it. Using Python Using javascript
Continue reading Program to Add two NumbersWhen JVM starts up, it creates a heap area which is known as runtime data area. This is where all the objects (instances of class) are stored. Since this area is limited, it is required to manage this area efficiently by removing the objects that are no longer in use. The process of removing unused …
Continue reading Garbage Collection in Javafinal keyword can be used along with variables, methods and classes. 1) final variable final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. Lets have a look at the below code: Note: It is considered as a good practice to have constant names in UPPER …
Continue reading Final Keyword In JavaYou must have seen public, private and protected keywords while practising java programs, these are called access modifiers. An access modifier restricts the access of a class, constructor, data member and method in another class. In java we have four access modifiers: 1. default 2. private 3. protected 4. public 1. Default access modifier When …
Continue reading Java Access ModifiersEncapsulation simply means binding object state(fields) and behaviour(methods) together. If you are creating class, you are doing encapsulation. What is encapsulation? The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can …
Continue reading Encapsulation in JavaA class that is declared using “abstract” keyword is known as abstract class. It can have abstract methods(methods without body) as well as concrete methods (regular methods with body). A normal class(non-abstract class) cannot have abstract methods. An abstract class can not be instantiated, which means you are not allowed to create an object of …
Continue reading Abstract Class in Java