You are on page 1of 2

Algorithm:Step:1 Start Step:2 Read a number n Step:3 if(n<0) write"factorial of negative numbers is not possible" and exit (end

of if statement) Step:4 if(n=0) write"factorial of 0=1" and exit Step:5 fact=1 for(m=1;m<=n;m++) { fact= fact*m } Step:6 write"factorial". Step:7 Stop

import java.io.*; class Factorial{ public static void main(String[] args) { try{ BufferedReader object = new BufferedRead er(new InputStreamReader(System.in)); System.out.println("enter the number"); int a= Integer.parseInt(object.readLine( )); int fact= 1; System.out.println("Factorial of " +a+ " :"); for (int i= 1; i<=a; i++){ fact=fact*i; } System.out.println(fact); } catch (Exception e){} } } public class bubbleSort{ public static void main(String a[]){ int i; int array[] = {12,9,4,99,120,1,3,10}; System.out.println("Values Before the so rt:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); bubble_srt(array, array.length); System.out.print("Values after the sort: \n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); } public static void bubble_srt( int a[], int n ){ int i, j,t=0; for(i = 0; i < n; i++){ for(j = 1; j < (n-i); j++){ if(a[j-1] > a[j]){ t = a[j-1]; a[j-1]=a[j]; a[j]=t; } } } } }
INSERTION_SORT (A)

class StringReverse {public static void main(String[] args) { Sring s="Reverse of String"; int N = s.length(); char[] a = new char[N]; for (int i = 0; i < N; i++) a[i] = s.charAt(N-i-1); String reverse = new String(a); System.out.println("Reverse of String::::::::::::::::::::"+reverse); }} public class ReverseNumber { public static void main(String args[]) { int num =12345; int rev = 0; while (num != 0) { rev = rev * 10 + num % 10; num /= 10; } System.out.println("::::::::"+rev); } }

public void selectionSort(int[] arr) { int i, j, minIndex, tmp;

int n = arr.length; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } }} public class Palindrome { public static void main(String args[]) { String s="malayalam"; int i; int n=s.length(); String str=""; for(i=n-1;i>=0;i--) str=str+s.charAt(i); if(str.equals(s)) System.out.println(s+ "is palindrome "); else System.out.println(s+ "is not a palindrome "); }}

1. 2. 3. 4. 5. 6. 7. 8.

FOR j 2 TO length[A] DO key A[j] {Put A[j] into the sorted sequence A[1 . . j 1]} ij1 WHILE i > 0 and A[i] > key DO A[i +1] A[i] ii1 A[i + 1] key

int partition(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } }; return i; }

You might also like