Java program for Pronic Number

Java program for Pronic Number

Java program for pronic Number


According to  Wikipedia  A pronic number is a number which is the product of two consecutive integers, that is, a number of the form n(n + 1). It is also called oblong number, rectangular number or heteromecic number.
Examples : 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552 etc

Method I


// Java program to find Pronic Number
import java.io.*;
class PronicNumber
{
    public static void main(String args[]) throws IOException
    {

        int n, i;           // n for number and i for loop
        boolean f=false;    // f for flag, initialize with false
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter a number to find pronic or not : ");
        n = Integer.parseInt(br.readLine());
        for(i=1; i<=n; i++)
        {
            if(i*(i+1) == n)
            {
                f = true;
                break;
            }
        }
         // Checking the pronic number
        if(f == true)
            System.out.println(n+" is a Pronic Number.");
        else
            System.out.println(n+" is not a Pronic Number.");    
    }
}

Sample Output:
Enter a number to find pronic or not : 110
110 is a Pronic Number.
Enter a number to find pronic or not : 272
272 is a Pronic Number.
Enter a number to find pronic or not : 6
6 is a Pronic Number.
Enter a number to find pronic or not : 35
35 is not a Pronic Number.


Method II


// Java program to find Pronic Number
import java.io.*;
class PronicNumberWithFunction
{
    int n, i;           // n instance variable for number and i for loop
    boolean f=false;    // f instance for flag, initialize with false
    boolean pronic()throws IOException
    {
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter a number to find pronic or not : ");
        n = Integer.parseInt(br.readLine());
        for(i=1; i<=n; i++)
        {
            if(i*(i+1) == n)
            {
                f = true;
                break;
            }
        }
        return f;
    }
    // Main method start
    public static void main(String args[]) throws IOException
    {
        // Checking the pronic number
        boolean f;
        PronicNumberWithFunction ob = new PronicNumberWithFunction();
        f = ob.pronic();
        if(f == true)
            System.out.println("A number is a Pronic Number.");
        else
            System.out.println("The number is not a Pronic Number.");    
    }
}

Sample Output:
Enter a number to find pronic or not : 12
The number is a Pronic Number.
Enter a number to find pronic or not : 130
The number is not a Pronic Number.

Java program for Selection Sort

Java program for Selection Sort

Selection Sort 


Selection sort is a very easy and simple sorting algorithm. It repeatedly selects the smallest or largest elements from the array and places it in the first position of the array. This process is repeated with the rest of the array. The second smallest or largest element is selected and put onto the next slot and this process is repeated till the end of the array. Time complexity is  O(n2) 

Java program for Selection Sort

In the following, we have not taken ZERO into the array number[]. In the program, we first print the data elements without sorting them. After that selection sort algorithm applied. In that, a value is selected and checked with another value, if it is greater than the previous one then the value is swapped with each other. In this fashion, all the elements of the array arranged in ascending order. And print the elements.

import java.util.Scanner;
public class SelectoinSort {

    public static void main(String arg[]) {
        Scanner sc = new Scanner(System.in);
        int number[] = new int[10];
        int i, j, l, k = 0, m;
        int t; // temperory variable
        System.out.println("Enter integer");
        for (i = 0; i < 10; i++) {
            m = sc.nextInt();
            if (m != 0) {
                number[k] = m;
                k++;
            }
        }
        // Printing unsorted data
        System.out.println("Unsorted order");
        for (i = 0; i < k; i++) {
            System.out.print(number[i] + " ");
        }
        // Sorting technique 
        for (i = 0; i < k; i++) {
            for (j = i + 1; j < k; j++) {
                if (number[i] >number[j]) {
                    t = number[i];
                    number[i] = number[j];
                    number[j] = t;
                }
            }
        }
        // Print sorted data
        System.out.println("\n Sorted order");
        for (i = 0; i < k; i++) {
            System.out.print(number[i] + " ");
        }
    }
}

Sample output
Enter integer
50
65
90
66
45
21
45
65
25
89
Unsorted order
50 65 90 66 45 21 45 65 25 89
 Sorted order
21 25 45 45 50 65 65 66 89 90

SN
Variables
Type
Description
1
main()
void
This is the main entry point of the program
2
number[]
int
int   array for the name
3
m
int
For checking zero
4
i, j
int
i and j are for loop
5
t
int
Use for a temporary variable

Selection Sort in java

Program to sort an array of strings using Selection Sort


import java.util.Scanner;
public class SelectionSortForString
{
     public static void main(String arg[]) {
        Scanner sc = new Scanner(System.in);
        // String Array
        String studentName[] = new String[10];
        int i, j, l ;
        String t;
        System.out.println("Enter Student Name:");
        for (i = 0; i < 10; i++) {
            studentName[i]= sc.next();
           
        }
        // Printing unsorted data
        System.out.println("Unsorted order");
        for (i = 0; i < 10; i++) {
            System.out.print(studentName[i] + " ");
        }
        // Sorting technique
        for (i = 0; i <9; i++) {
            for (j = i + 1; j < 10; j++) {
                if (studentName[i].compareTo(studentName[j])>0) {
                    t = studentName[i];
                    studentName[i] = studentName[j];
                    studentName[j] = t;
                }
            }
        }
        // Print sorted data
        System.out.println("\n Sorted order");
        for (i = 0; i < 10; i++) {
            System.out.print(studentName[i] + " ");
        }
    }

}

Analysis of Selection sort

Selecting the lowest element requires scanning all n elements (this takes n − 1 comparisons) and then swapping it into the first position. Finding the next lowest element requires scanning the remaining n − 1 elements and so on
(n-1) + (n-2) + (n-3) + ….. + (n-k) + 3 + 2 + 1
n(n-1)/2
O(n
2
)

More Java program

Java Program to find Emirp Number

Java Program to find Emirp Number

Java Program to find Emirp Number

Java program for Emirp Number

A number which is prime backward and forwards is called emirp number. Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp to check if a given number is Emirp number or not. Some of the members of the class are given below:
Class name                 Emirp
Data members/instance variables:
n                               stores the number
reverse                     stores the reverse of the number
f                                stores the divisor
Member functions :
Emirp(int n)               to assign n = n, reverse = 0 and f = 2
int isprime(int x)         check if the number is prime using the recursive technique and return I if prime otherwise return 0
void isEmirp( )             reverse the given number and check if both the original number and the reverse number are prime. by invoking the function isprime(int) and display the result with an appropriate message
Specify the class Emirp giving details of the constructor(illt), int isprime(int) and void isEmirp().
Define the main( ) function to create an object and call the methods to check for Emirp number.


import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
 * class Emirp
 * ISC 2013 paper Question 08
 */
public class Emirp
{
    // instance variables
    private int n;      // For number
    private int reverse;    // reverseeres number
    private int temp;   // temporary variable     

    /**
     * Constructor for objects of class Emirp
     */
    public Emirp(int n)
    {
        // initialise instance variables
        this.n = n;
        reverse = 0;
        temp = 2;
    }

    public int isPrime(int x, int i)
    {
       
        if(i==1){
            return 1;
        }
        else
        {
            if(x%i==0)
                return 0;
            else
                return isPrime(x,i-1);
        }
    }
    void isEmirp()
    {
        int a,p1, p2;
        temp = n;                         
        while(n !=0 )
        {
            a = n % 10;
            reverse = reverse * 10 +a;
            n = n/10;
        }
        System.out.println(reverse);
        p1 = isPrime(temp,temp/2);          // call the isPrime for the original number
        p2 = isPrime(reverse,reverse/2);    // call the isPrime for the reverse number
        if( p1 == 1 && p2  == 1)
        {
            System.out.println("Number is Emirp");
        }
        else
        {
            System.out.println("Number is not Emirp");
        }
    }
    public static void main(String args[])throws IOException
    {
        int n;
        InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(in);
        System.out.println("Enter the number ");
        n = Integer.parseInt(br.readLine());
        Emirp ob = new Emirp(n);
        ob.isEmirp();
    }
}


Sample Output
Enter the number
13
31
Number is Emirp

Enter the number
14
41
Number is not Emirp

Enter the number
71
17
Number is Emirp
Sl. No.
Variable Name
Data Type
Purpose
1
n, reverse, temp
int
instance variables
2
Emirp()
Constructor
The constructor of Emirp class
3
isPrime(int, int)
int
Checking prime number or not (recursive method)
4
isEmirp()
void
Checking Emirp
5
a, p1, p2
int
Local variable to isEmirp() method
6
in
InputStreamReader
The object of InputStreamReader class which exists in java.io package
7
br
BufferedReader
Use to create object br which exists in java.io package
main()
void
Main function



More Program

Neon number program in java