Sunday, March 22, 2020

50 faq


Question 1- Write a program to check the given number is prime or not?
import java.util.*;

class PrimeTest
{
                public static void main(String args[])
                {
                                System.out.print("Enter The Number: ");
                                Scanner sc=new Scanner(System.in);
                                int number=sc.nextInt();
                                                               
                                boolean flag=true;
               
                                for(int i=2;i<=number/2;i++)
                                {
                                                if(number%i==0)
                                                {
                                                                flag=false;
                                                                break;
                                                }

                                }
                                if(flag)
                                {
                                                System.out.println("Prime Numner");
                                }
                                else
                                {
                                                System.out.println("Not Prime Numner");                        
                                }
                }
}


Question 2- Write a program to find HCF of given two numbers?
//This Java program accepts 2 numbers from the user and prints their HCF.

import java.util.*;
class hcf
{
    public static void main(String Args[])
    {
        System.out.println("Enter 2 numbers");
        Scanner sc=new Scanner(System.in);
        int m=sc.nextInt();
        int n=sc.nextInt();

        int h=1;
        int p=m*n;
        for(int i=2;i<p;i++)
        {
            if((m%i==0)&&(n%i==0))
            {
                h=i;
            }
        }
        System.out.println("HCF="+h);
    }
}

No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...