## Peterson Number, Function overloading and Even Number with Start and end limit

Q. Write a program to compute the area of rectangle, square and circle using overloading function. get area(int, int),  get area(int) and get area(double a)

Solution:
Here we calculate Area of Rectangle, Area of Square and Area of Circle using Method Over Loading.

import java.io.*;
public class AreaCalc    // Class started
{
int l,b,s,ar1;
double r,ar2;
int area(int a)            // Area of Square
{
s=a;
ar1=s*s;
return ar1;
}
int area(int x,int y)  // Area of Rectangle
{
l=x;
b=y;
ar1=l*b;
return ar1;
}
double area(double x)  // Area of Circle
{
r=x;
ar2=Math.PI*r*r;
return ar2;
}
public static void main(String args[])throws IOException
{
int a;
double ar;
AreaCalc ob=new AreaCalc();
a=ob.area(10);
System.out.println("Area Of Square="+a);
a=ob.area(10,5);
System.out.println("Area Of Rectangle="+a);
ar=ob.area(5.5);
System.out.println("Area Of Circle="+ar);
}
} // end of class

Output:
Area Of Square=100
Area Of Rectangle=50
Area Of Circle=95.03317777109123

Q2. Write a program to accept a number from user and check whether the number in Peterson or not, using a function (int n). A Peterson number is a number whose sum of factorial of all the digit is the number itself E.g., 145 = 1! + 4! + 5! = 1 + 24 +120 = 145

Solution:
import java.io.*;
/**
* class PetersonNumber
*/
public class PetersonNumber
{
// instance variables
private int n;
private int f;
public int peterson(int x)
{
f=1;
n=x;
for(int i=1; i<=n; i++)
{
f=f*i;
}
return f;
}
public static void main(String args[])throws IOException
{
// Local variable
int a, b, n, m, s=0;
PetersonNumber ob=new PetersonNumber ();
System.out.print("Enter a number: ");
m=n;                // Store the n value to m
while(n!=0)
{
a = n % 10;
b = ob.peterson(a);
s = s + b;
n = n / 10;
}
if(m == s)
{
System.out.println ("PetersonNumber");
}
else
{
System.out.println ("Not PetersonNumber");
}
}
} // end of class

Output:
Enter a number: 145
PetersonNumber
Enter a number: 273
Not PetersonNumber

Q3. Write a program to design a method called void sumEven(int start, int end) to take start and end limit from the main(). The function will display the number of even number and the sum of all the even number between start and end (both inclusive)
Solution:
import java.io.*;
public class SumEven
{
int s,i,start,end;
void sumEven(int s ,int e)
{
start=s;
end=e;
s=0;
for(i=start;i<=end;i++)
{
if(i%2==0)
{
s=s+i;
System.out.print(i+" ");
}
}
System.out.println("\n Sum even="+s);
}
public static void main(String args[])throws IOException
{
// Local variable
int a, b;
SumEven ob=new SumEven ();
System.out.print("Enter start and end: ");
ob.sumEven(a,b);
}
}

Output:
Enter start and end: 5
30
6 8 10 12 14 16 18 20 22 24 26 28 30 Sum even=234

## Binary Search in Java

In binary search the array must be in sorted order either ascending or descending order. Binary search relies on divide and conquer strategy in a sorted list. In Binary Search we split the array into two half. And then check the search value, if the value is less than the mid value then we will only look First half otherwise Second half. In this way we will search until the value found.

Logic :
In this example I have declare array num[]. After then I insert the value. Then with the help of Bubble sort algorithm we do the sorting. After then a search value has taken. Here first of all the desired elements compared with the middle most elements of the array.
If the search element is smaller to the middle most elements then process is repeated with the first half of the array.
If the search element is greater to the middle most elements then process is repeated with the second half of the array.
If the search element is equal to the middle most elements search is completed and position is return in ‘pos ‘ variable.

import java.io.*;
/**
* BinarySearchInJava
*  Array must be in sorted order for Binary search
*/
public class BinarySearchInJava
{
public static void main(String args[]) throws IOException
{
int num[] = new int[10];
int i,s,f=0,t,j;
int low, high, mid, pos;
System.out.println("Enter elements in array:");
for(i=0;i<10;i++)
{
}
System.out.println("Enter element to Search:");
// Sorting of array
for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(num[j] > num[j+1])
{
t = num[j];
num[j] = num[j+1];
num[j+1] = t;
}
}
}
System.out.println("Sorted array:");
for(i=0;i<10;i++)
{
System.out.print(num[i] + " ");
}
// Binary Search
high=10;
low=0;
pos=0;
while(low <= high)
{
mid = (low + high) / 2;
if(s < num[mid])
{
high = mid - 1;
}
else if(s>num[mid])
{
low = mid + 1;
}
else if(s == num[mid])
{
pos = mid + 1;
break;
}
}
System.out.println("\n Search result");
System.out.println(s+" is located at " + pos);
}
}

Output:
Enter elements in array:
10
7
8
4
5
6
9
1
2
3
Enter element to Search:
4
Sorted array:
1 2 3 4 5  6 7 8 9 10
Search result
4 is located at 4
Variable Description for Java:
 Sl. No. Variable Name Data Type Purpose 1 main() void Main function 2 num[] int To store the values in an array 3 i , j int For running the loop 4 s int To store the input (element to be searched) 5 t int For temporarily storing a value 6 low int value in the lowest position 7 mid int value in the middle position 8 high int value in the highest position 9 pos int position of the value which was searched(only if exists) 10 in InputStreamReader To instantiate the InputStreamReader class which exists in java.iopackage 11 br BufferedReader To instantiate the BufferedReader class which exists in java.iopackage using the above variable as passing parameter

Analysis:

Here we are reducing the search area. Here number of comparisons keeps on decreasing. In worst case scenario log(N + 1), that is why it is an efficient search compared to Linear Search.