Fibonacci(without using recursion)
public class Test
{
public static void main(String[] args)
{
int a=0 , b=0 , c=1;
System.out.println(a);
System.out.println(b);
for(int i=0;i<15;i++)
{
a=b;
b=c;
c=a+b;
System.out.println(c);
}
}
}
Fibonacci(using recursion)
7
Reverse array without Temp Array
public static int[] reverseArrayWithoutTempArray(int[] array)
{
int i = 0, j = array.length - 1;
for (i = 0; i < array.length / 2; i++, j--)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Iterate random integer array and print the values greater than the current value
class Main
{
static void printNGE(int arr[])
{
int highestVal = 0, i, j;
for (i = 0; i < arr.length; i++)
{
if (arr[i] > highestVal)
{
System.out.println(arr[i]);
highestVal = arr[i];
}
}
}
public static void main(String args[])
{
int arr[]= {11, 13, 21, 3, 45};
printNGE(arr);
}
}
Inheritance program with Class and Interface.Find and print the Area and Perimeter for Rectangle,Square and Circle
- Interface Shape
- Rectangle class implements Shape
- Circle class extends Rectangle
interface Shape
{
void input();
void area();
}
class Circle implements Shape
{
int r = 0;
double pi = 3.14, ar = 0;
@Override
public void input()
{
r = 5;
}
@Override
public void area()
{
ar = pi * r * r;
System.out.println("Area of circle:" + ar);
}
}
class Rectangle extends Circle
{
int l = 0, b = 0;
double ar;
public void input()
{
super.input();
l = 6;
b = 4;
}
public void area()
{
super.area();
ar = l * b;
System.out.println("Area of rectangle:" + ar);
}
}
public class Demo
{
public static void main(String[] args)
{
Rectangle obj = new Rectangle();
obj.input();
obj.area();
}
}
Area of circle:78.5
Area of rectangle:24.0