Wednesday, 19 August 2015

Find out factorial of Number.

package com.JavaInterview.Programs;

import java.util.*;

/**
 * Find Out Factorial of a number
 * 
 * 
 */
public class Factorial {

 public void findFactorial(int n) {
  int fact = 1;
  if (n < 0) {
   System.out.println("The Number should be non-negative");
  } else {
   for (int i = 1; i <= n; i++) {
    fact = fact * i;
   }
  }
  System.out.println("factorial Of Number  " + n + " is : " + fact);
 }

 public static void main(String[] args) {

  Scanner input = new Scanner(System.in);
  System.out.println("Please enter the number :");
  int num = input.nextInt();

  Factorial f = new Factorial();
  f.findFactorial(num);

 }

}

Output :
Please enter the number :
6
factorial Of Number  6 is : 720


Prints the numbers from 1 to 50 but for multiple of 3,5 print Fizz,Buzz,FizzBuzz.

package com.JavaInterview.Programs;
import java.util.*;

/* Write a Java program that prints the numbers from 1 to n. But for multiples of 
 * three print "Fizz" instead of the number and for the multiples of five print 
 * "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"
 * */

public class FizzBuzz {

 public void printFizzBuzz(int n)
 {
  for(int i=1;i<n;i++)
  {
   if(i%(3*5)==0)
   {System.out.println("FizzBuzz");}
   else if(i%3==0)
   {System.out.println("Fizz");}
   else if(i%5==0)
   {System.out.println("Buzz");}
   else
   {System.out.print(i+" ,");}
  }
 }

 public static void main(String args[])
 {
  Scanner input=new Scanner(System.in);
  System.out.println("Please enter the length of series");
  int num=input.nextInt();
 
  FizzBuzz fb=new FizzBuzz();
  fb.printFizzBuzz(num);
 }

}

Output:

Please enter the length of series
20
1 ,2 ,Fizz
4 ,Buzz
Fizz
7 ,8 ,Fizz
Buzz
11 ,Fizz
13 ,14 ,FizzBuzz
16 ,17 ,Fizz
19 ,


Program to replace character from the String using user defined method.

package com.JavaInterview.Programs;
/*How to replace characters from a string using user defined method .Here we have use  StringBuffer * ,you can also use StringBuilder.
*
* @author:http://javainterviewprograms.blogspot.in/

*/

public class CharacterReplace2 {

 //User method to replace characters from a string

 public static String replace(String s,char from,char to)
 {
  StringBuffer str=new StringBuffer(s.length());
 
  for(int i=0;i<s.length();i++)
  {
   if(s.charAt(i)==from)
   {
    str.append(to);
   }
   else
   {
    str.append(s.charAt(i));
   }
  }
  return str.toString();
 }

 public static void main(String args[])
 {
  String original="This is a JIP Example String";
 
  System.out.println("Original String : "+original);
 
  
  String changed=replace(original, 'i','*');
 
  System.out.println("Changed String :" + changed);
 }
}

Output:

Original String : This is a JIP Example String
Changed String :Th*s *s a JIP Example Str*ng


Program to replace character from the String .

package com.JavaInterview.Programs;
/* How to get replace character or a string into a string with the given string. String provides replace() * method to replace a specific character or a string which occurs first. replaceAll() method replaces a *&specific character or a string at each occurrence. 
* @author:http://javainterviewprograms.blogspot.in/
*/

public class CharacterReplace {

public static void main(String args[])
{
String str="This is JIP Example String";

System.out.println("Original String : " + str);
//Replace one characters in string with another 
System.out.println("Replace Character 's' with 'x' :"+str.replace('s', 'x'));

//Replace first substring with another
System.out.println("Replace first substring 'is' with 'xy' :"+str.replaceFirst("is", "xy"));

//Replace all substring with another
System.out.println("Replace all substring 'is' with 'pp' :"+str.replaceAll("is", "pp"));
}

}

Output :
Original String : This is JIP Example String
Replace Character 's' with 'x' :Thix ix JIP Example String
Replace first substring 'is' with 'xy' :Thxy is JIP Example String
Replace all substring 'is' with 'pp' :Thpp pp JIP Example String

Wednesday, 12 August 2015

Swap Numbers

package com.JavaInterview.Programs;

import java.util.*;
/*
* @author:http://javainterviewprograms.blogspot.in/

*/

public class SwapNumber {

 public void swap(int number1, int number2) {
  System.out.printf("Nmbers before Swap :" + number1 +","+ number2);
  number1 = number1 + number2;
  number2 = number1 - number2;
  number1 = number1 - number2;

  System.out.printf("Numbers after swap :" + number1+","+ number2);
 }

 public static void main(String args[]) {
  Scanner input1 = new Scanner(System.in);
  Scanner input2 = new Scanner(System.in);

  System.out.println("Please enter the number :");
  int num1 = input1.nextInt();
  int num2 = input2.nextInt();

  SwapNumber SN = new SwapNumber();
  SN.swap(num1, num2);
 }
}

Output :
Please enter the number :
23
54
Nmbers before Swap :23,54Numbers after swap :54,23


Sum of Prime Number

package com.JavaInterview.Programs;

import java.util.*;

/**
 * Find Out Factorial of a number
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */

public class SumOfPrime {

 public boolean isPrime(int number) {
  boolean status = true;
  for (int i = 2; i <= number / 2; i++) {
   if (number / i == 0) {
    status = false;
    break;
   }
  }
  return status;
 }

 public void sum(int totalCount)
 {
  int number=2;
  int sum=0;
  int count=0;
  while(count <   totalCount)
  {
   if(isPrime(number))
   {
    sum+=number;
    count++;
   }
   number++;
  }
 
  System.out.println("Sum of first "+totalCount +" Prime Number is :"+sum);
 }

 public static void main(String args[])
 {
  SumOfPrime SP=new SumOfPrime();
  Scanner input=new Scanner(System.in);
 
  System.out.println("Please enter lengthof Prime number that you want to add :");
  int count=input.nextInt();
  SP.sum(count);
 }


}

Output :
Please enter lengthof Prime number that you want to add :
12

Sum of first 12 Prime Number is :90


Sum Of Digits

package com.JavaInterview.Programs;

import java.util.Scanner;

/**
 * Find Out Factorial of a number
 * 
 * @author:http://javainterviewprograms.blogspot.in/
 * 
 */

public class SumOfDigit {

 int totalSum = 0;

 public int sum(int number) {

  if (number == 0) {
   return totalSum;

  } else {
   totalSum += (number % 10);
   sum(number / 10);
  }
  return totalSum;
 }

 public static void main(String args[]) {
  Scanner input = new Scanner(System.in);
  System.out.println("Please enter number :");
  int number = input.nextInt();

  SumOfDigit SD = new SumOfDigit();
  System.out.println("sum Of Digit of Number  " + number + " :"
    + SD.sum(number));
 }
}

Output:
Please enter number :
23
sum Of Digit of Number  23 :5


Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...