0% found this document useful (0 votes)
5 views8 pages

Lab 5

Uploaded by

hassamkiani66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Lab 5

Uploaded by

hassamkiani66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB 5

Name : Muhammad Hassam


Roll no : 037
Section : BSSE-3A
Program to Find Factorial, Fibonacci and Multiplication Table .
Code:
package recursion;

import java.util.Scanner;
public class Recursion {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);


System.out.println("Enter a Number :");
int n =scan.nextInt();
System.out.println("Factorial of "+n+": "+factorial(n));
System.out.println("Fibonacci of "+n+": "+fibonacci(n));
System.out.println("Multiplication Table of "+n+" :");
MultTable(n,1);

public static void MultTable(int n, int start){


if(start>10){
return;
}
else{
System.out.println(n+"*"+start+" = "+n*start);
MultTable(n,start+1);
}
}
public static int fibonacci(int n){
if(n<=1){
return n;
}
return (fibonacci(n-1)+fibonacci(n-2));
}
public static int factorial(int n){
if(n== 0 || n==1){
return 1;
}
else{
return( n*factorial(n-1));
}
}

}
Output:
Bubble Sort for Integer and String
Code:

package sorting;

public class Sorting {

public static void main(String[] args) {


int Arr[]={8,6,5,3};
System.out.println("Before Sorting :");
display(Arr);
System.out.println();
String arr[] = {"ABDULLAH","HASSAM","Ali"};

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 {

public static void main(String[] args) {


Scanner scan=new Scanner(System.in);
System.out.println("Enter a Number to find its sum :");
int num= scan.nextInt();
System.out.println("Sum is "+sumNum(num));
System.out.println("Enter x :");
int num2= scan.nextInt();
System.out.println("Enter n:");
int num3= scan.nextInt();

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;
}
}

public static long power(int x, int n){


if(x==0){
return 0;
}
if(n==0){
return 1;
}
long res = x*power(x,n-1);
return res;
}

}
Output:

You might also like