Sunday, January 27, 2019

a. prime or not
Prime number: A prime number is a whole number greater than 1 whose only factors are 1 and itself.
Ans:
---
public class PrimeTest {
public static void main(String[] args) {
int n, i, k=0;
            System.out.println("Enter a positive integer: ");
            Scanner sc = new Scanner(System.in);
            n=sc.nextInt();
            for(i=2;i<=n/2;++i){
                 if(n%i==0){
                       k=1;
                       break;
                 }
             }
             if (k==0)
                  System.out.println(n+" is a prime number");
             else {
                 System.out.println(n+" is not a prime number");
              }
              sc.close();
}
}
Output:
-------
Enter a positive integer: 29
29 is a prime number.

Related Posts:

  • Extract digits from a String /* * Input: A14BC654 -> acceptable * Input: AB54C54D -> not acceptable  * Input: A23BED75 -> acceptable * Conditions to check:  * 1) First and Last index values of the string are character and digit respec… Read More
  • Factorial of a given number Factorial of a given number using recursion. public lass FactorialRecursion{         public static void main(String[] ar){              System.out.print("Enter a number … Read More
  • Difference between Iterator and Iterable? Both Iterator and Iterable are interfaces in Java Collection Framework. Here are some differences: Iterator: Iterator  is an interface that manages iteration over an Iterable. It maintains a state of where we are… Read More
  • Remove Duplicate values from an Array Write a Java Program to remove duplicate values from an array? package seleniumrepo; public class DisticntElements {     public static void printDistinctElements(int[] arr){         for(int i=… Read More
  • Difference between Comparator and Comparable? Both are interfaces in Java. Here some common differences between Comparator and Comparable. Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package. Comparato… Read More

0 comments:

Post a Comment

Selenium Training in Realtime

Blog helps to a student or IT employee to develop or improve skills in Software Testing.

Followers

About Me

My photo
Hyderabad, Andhra Pradesh, India
I am Automation Testing Professional. I have completed my graduation in B.Tech (Computers) from JNTU Hyderabad and started my career in Software Testing accidentally since then, I passionate on learning new technologies

Contact Form

Name

Email *

Message *

Popular Posts