Showing posts with label Prime. Show all posts
Showing posts with label Prime. Show all posts

Thursday, 13 June 2013

program in java - find prime number within array elements with abstract class

import java.io.*;
abstract class base
{
    int a[]=new int[10];
    abstract void cal();
}
class derived extends base
{
    int i,j,b=0,n;
    public void get()
    {
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter matrix size");
            n=Integer.parseInt(cin.readLine());
            System.out.println("Enter the first matrix");
            for(i=0;i<n;i++)
            {
                a[i]=Integer.parseInt(cin.readLine());

            }

        }
        catch(Exception e)
        {
            System.out.println("wrong data");
        }
    }
    public void cal()
    {
        for(i=0;i<n;i++)
        {
            for(j=2;j<a[i];j++)
            {
                if(a[i]%j==0)
                {
                    b=0;
                    break;
                }
                else
                {
                    b=1;
                }
            }
            if(b==1)
            {
                System.out.println(a[i]+" is prime");
            }

        }

    }
}
class abs_arr_prime
{
    public static void main(String args[])
    {
        derived d=new derived();
        d.get();
        d.cal();

    }

}

print prime number in given series

import java.io.*;
class simple_print_prime_number_in_given_series
{
    public static void main(String args[])
    {
        int start,end,i,j,a=1;
        try
        {
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter the number from where you find the prime number");
            start=Integer.parseInt(cin.readLine());
            System.out.println("Enter the number up to you want to find prime number ");
            end=Integer.parseInt(cin.readLine());
            System.out.println("The prime numbers in between "+start+" and "+end+" are");

            for(i=start;i<=end;i++)
            {
                for(j=2;j<i;j++)
                {
                    if(i%j==0)
                    {
                        a=0;
                        break;
                    }
                    else
                    {
                        a=1;
                    }
                }

                if(a==1)
                {
                    System.out.println(i);
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("wrong data");
        }
    }
}

program to check given number is prime or not ..................

// simple_check_prime_number

import java.io.*;
class simple_check_prime_number
{
    public static void main(String args[])
    {
        try
        {
            int i,no,a=1;
            DataInputStream cin=new DataInputStream(System.in);
            System.out.println("Enter the number to check it is prime or not");
            no=Integer.parseInt(cin.readLine());

            for(i=2;i<no;i++)
            {
                if(no%i==0)
                {
                    a=0;
                    break;
                }
                else
                {
                    a=1;
                }
            }

            if(a==1)
            {
                System.out.println(no+" is prime");
            }
            else
            {
                System.out.println(no+" is not prime");
            }
        }
        catch(Exception e)
        {
            System.out.println("wrong data");
        }
    }
}

Wednesday, 12 June 2013

More Applications in single frame



/**
 * @(#)AllinOne.java
 *
 *
 * @author -> Anant Mahale
 * @version 1.00 2013/5/11

  You can perform various operation with this program
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class AllinOne extends JFrame implements ActionListener
{
    private Button btn_add,btn_subtract,btn_mul,btn_div,btn_arm_strong_number,btn_palindrom,btn_prime,btn_factorial,btn_sum_digit;
    public AllinOne()
    {
        setLayout(null);
       
        setTitle("Operation");
        setSize(500,500);
        setLocation(200,200);
       
        btn_add=new Button("Addition");
        btn_add.setBounds(50,50,150,30);
        btn_add.addActionListener(this);
        add(btn_add);
       
        btn_subtract=new Button("Subtract");
        btn_subtract.setBounds(50,100,150,30);
        btn_subtract.addActionListener(this);
        add(btn_subtract);
       
        btn_mul=new Button("Multiplication");
        btn_mul.setBounds(50,150,150,30);
        btn_mul.addActionListener(this);
        add(btn_mul);
       
        btn_div=new Button("Division");
        btn_div.setBounds(50,200,150,30);
        btn_div.addActionListener(this);
        add(btn_div);
       
        btn_arm_strong_number=new Button("ArmStrong Number");
        btn_arm_strong_number.setBounds(250,50,150,30);
        btn_arm_strong_number.addActionListener(this);
        add(btn_arm_strong_number);
       
        btn_palindrom=new Button("Palindrom");
        btn_palindrom.setBounds(250,100,150,30);
        btn_palindrom.addActionListener(this);
        add(btn_palindrom);
           
        btn_prime=new Button("Prime");
        btn_prime.setBounds(250,150,150,30);
        btn_prime.addActionListener(this);
        add(btn_prime);
       
        btn_factorial=new Button("Factorial");
        btn_factorial.setBounds(250,200,150,30);
        btn_factorial.addActionListener(this);
        add(btn_factorial);
       
        btn_sum_digit=new Button("Sum Of Digit");
        btn_sum_digit.setBounds(150,250,150,30);
        btn_sum_digit.addActionListener(this);
        add(btn_sum_digit);       
    }
    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==btn_add)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int sum=fno+sno;
            JOptionPane.showMessageDialog(this,"Addition - "+sum);           
        }
        if(ae.getSource()==btn_subtract)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int sub=fno-sno;
            JOptionPane.showMessageDialog(this,"Subtract - "+sub);           
        }
        if(ae.getSource()==btn_mul)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int mul=fno*sno;
            JOptionPane.showMessageDialog(this,"Multiplication - "+mul);           
        }
        if(ae.getSource()==btn_div)
        {
            String fn=JOptionPane.showInputDialog(this,"Enter the first number");
            String sn=JOptionPane.showInputDialog(this,"Enter the Second number");
            int fno=Integer.parseInt(fn);
            int sno=Integer.parseInt(sn);
            int div=fno/sno;
            JOptionPane.showMessageDialog(this,"Division - "+div);           
        }
        if(ae.getSource()==btn_arm_strong_number)
        {
            int rem,n,no,div,arm=0;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Armstrong or not");       
            n=Integer.parseInt(str_num);
            no=n;
            while(n!=0)
            {
                rem=n%10;
                div=n/10;
                arm=arm+(rem*rem*rem);
                n=div;
            }
            if(arm==no)
            {
                JOptionPane.showMessageDialog(this,no+" is armstromg");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,no+" is not armstromg");   
            }
        }
        if(ae.getSource()==btn_palindrom)
        {
            int n,no,rem,rev=0;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
            n=Integer.parseInt(str_num);
            no=n;
            while(no!=0)
            {
                rem=no%10;
                rev=rev*10+rem;
                no=no/10;
            }
            if(rev==n)
            {
                JOptionPane.showMessageDialog(this,n+" is palindrom ");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,n+" is not palindrom ");   
            }
        }
        if(ae.getSource()==btn_prime)
        {
            int i,no,a=1;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");       
            no=Integer.parseInt(str_num);
            for(i=2;i<no;i++)
            {
                if(no%i==0)
                {
                    a=0;
                    break;
                }
                else
                {
                    a=1;
                }
            }
            if(a==1)
            {
                JOptionPane.showMessageDialog(this,no+" is prime");   
            }
            else
            {
                JOptionPane.showMessageDialog(this,no+" is not prime");   
            }
        }
        if(ae.getSource()==btn_factorial)
        {
            int n,no,f=1;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
       
            n=Integer.parseInt(str_num);
            no=n;

            while(n>0)
            {
                f=f*n;
                n--;
            }
            JOptionPane.showMessageDialog(this,"factorial of "+no+" is = "+f);
        }
        if(ae.getSource()==btn_sum_digit)
        {
            int sum=0,n,no,rem;
            String str_num=JOptionPane.showInputDialog(this,"Enter the number to check it is Palindrom or not");
            n=Integer.parseInt(str_num);
            no=n;
            while(no!=0)
            {
                rem=no%10;
                sum=sum+rem;
                no=no/10;
            }
            JOptionPane.showMessageDialog(this,n+" of sum of digit is "+sum);
        }       
    }
}
class AllinOnePro
{
    public static void main(String args[])
    {
        new AllinOne().setVisible(true);
    }
   
}