1.Factorial of Given Number Using Java.
import java.util.*;
class factorial{
public static int fact(int n){
if(n<3){
return 1;
}
else{
return fact(n-1)+fact(n-2);
}
}
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=in.nextInt();
System.out.println(fact(n));
}
}
output:
6
8
2.staireCase Using Java Programming.
class staireCase{
public static void main(String[] args){
int n=5;
for(int i=1;i<=n;i++){
for(int j=0;j<n;j++){
if(j>=n-i)
{
System.out.print("#");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
output:
#
##
###
####
#####
3.Linear Search
//o(n) Time Complexity Worst Case
//o(n) Average Case
//o(1) Best Case
class LinearSearch{
public int find(int[] arr,int data){
int n=arr.length;
for(int i=0;i<n;i++){
if(arr[i]==data){
return i;
}
}
return -1;
}
public static void main(String[] args){
int[] arr={7,10,2,5,1,3,32,44,100,52,62};
LinearSearch l=new LinearSearch();
int find=5;
System.out.println(l.find(arr,find));
}
}
Comments
Post a Comment