Lab 5
Lab 5
import java.util.Scanner;
public class Recursion {
}
Output:
Bubble Sort for Integer and String
Code:
package sorting;
display(arr);
System.out.println();
System.out.println("After Sorting :");
BubbleSort(Arr);
display(Arr);
System.out.println();
BubbleSort(arr);
display(arr);
}
public static void display(int n[]){
for (int i = 0; i < n.length; i++) {
System.out.print(n[i]+" ");
}
}
public static void display(String n[]){
for (int i = 0; i < n.length; i++) {
System.out.print(n[i]+" ");
}
}
public static void BubbleSort(int n[]){
for(int i=0; i<n.length; i++){
for (int j = 0; j <n.length-i-1; j++) {
if(n[j]>n[j+1]){
int temp = n[j];
n[j]=n[j+1];
n[j+1]=temp;
}
}
}
}
public static void BubbleSort(String n[]){
for(int i = 0; i < n.length; i++){
for (int j = 0; j < n.length - i - 1; j++){
if (n[j].compareTo(n[j+1]) > 0){
String temp = n[j];
n[j] = n[j+1];
n[j+1] = temp;
}
}
}
}
}
Output:
Program to Find Sum of Natural Numbers and x^n of number
Code:
package sumofnaturalnumber;
import java.util.Scanner;
public class SumofNaturalNumber {
System.out.println(num2+"^"+num3+" = "+power(num2,num3));
}
public static long sumNum(int n){
if(n==1){
return 1;
}
else{
return sumNum(n-1)+n;
}
}
}
Output: