Character Escape Sequence in Java

java comes with a solution to that, here the Character Escape Sequence of Java comes to rescue. The escape sequence allows you to embed special characters like double quotes, line feed, carriage return, tab, etc. into the same String. The above example is corrected as follows:-

String str = "Hello, I am Learning \"Java\"";

Now this is the correct way as per Java. What Java sees is that anything after the "\" symbol is to be placed as it is, and is a part of the whole String overall. Though the symbol "\" has different implications with regard to other escape sequences, like "\n" inserts a new line, etc.

The Character escape Sequence in Java is shown below:-

Escape Sequence ------------Purpose
\ddd ------------ Octal character (ddd)
\uxxxx ------------ Hexadecimal UNICODE character (xxxx)
\’ ------------ Single quote
\” ------------ Double quote
\\ ------------ Backslash
\r ------------ Carriage return
\n ------------ New line (also known as line feed)
\f ------------ Form feed
\t ------------ Tab
\b ------------ Backspace

Program to find Prime Number in Java

import java.io.*;

public class primeNumber
{

public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number you want to test for prime number:");
int num =0;
int check = 0;
try {
num = Integer.parseInt(br.readLine());
for (int i=2; i < num/2; i++ )
{

if(num%i == 0)
{
check = 0;
break;
}
else
{
check = 1;
}
}
if(check == 1 || num == 2)
{
System.out.println("The number is prime");
}
else
{
System.out.println("The number is not prime");
}
} catch (NumberFormatException e)
{

System.out.println("Kindly enter a proper integer number");

} catch (IOException e)
{

System.out.println("There is some error in the input output handling");
}
}
}

Program in Java code for finding Prime Number

import java.io.*;

public class primeNumber
{
public static void main(String[] args)
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println("Enter the number you want to test for prime number:");
int num =0;
int check = 0;
try
{
num = Integer.parseInt(br.readLine());
for (int i=2; i <>
if(num%i == 0)
{
check = 0;
break;
}
else
{
check = 1;
}
}
if(check == 1 || num == 2)
{
System.out.println("The number is prime");
}
else
{
System.out.println("The number is not prime");
}
} catch (NumberFormatException e)
{
System.out.println("Kindly enter a proper integer number");

}
catch (IOException e)
{
System.out.println("There is some error in the input output handling");
}
}
}

Java program to find the Factorial of a given number

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Factorial
{


public static void main(String[] args) throws NumberFormatException, IOException
{
long num;

System.out.println("Enter a number greater then or equal to 0 and less then 40 for which the factorial is desired:-");

InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
num = Integer.parseInt(br.readLine());

if(num>=0 && num < 40)
{
if(num == 0)
{
num =1;
}
System.out.println(factorial(num));
}
else
{
System.out.println("Number out of range");
}
}
private static long factorial(long num)
{
if(num > 1)

{
num = num * factorial(num-1);
}
else
{
return num;
}
return num;
}
}

Program to find the Area and Circumference of Circle in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class AreaCircumferenceCircle
{
final static float pi = 22/7f;

public static void main(String[] args) throws NumberFormatException, IOException
{
System.out.println("Enter the radius of the circle: ");
InputStreamReader io= new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(io);
float radius = Float.parseFloat(br.readLine());
System.out.println("Area of the Circle is: "+radius*radius*pi);
System.out.println("Circumference of the Circle is: "+2*radius*pi);
}
}

Program to calculate Area and Perimeter of Rectangle in Java

import java.io.IOException;


public class AreaPerimeterRectangle
{

public static void main(String[] args) throws NumberFormatException, IOException
{
System.out.println("Enter the length and width of the rectangle: ");
float length = Float.parseFloat(args[0]);
float width = Float.parseFloat(args[1]);
System.out.println("Area of the Rectangle is: "+length*width);
System.out.println("Perimeter of the Rectangle is: "+2*(length+width));
}
}

Sorting array in Ascending order in Java

import java.util.TreeSet;


public class SortingAscending
{

@SuppressWarnings("unchecked")
public static void main(String[] args)
{
int[] num = {3,5,9,2,1,0,45,23,67,93};
TreeSet ts = new TreeSet();

for(int i = 0; i <>
{
ts.add(num[i]);
}
System.out.println("The numbers of the array set in ascending order are: "+ts);
}
}

Program to find Length of a given String in java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NoOfWordsInString
{

public static void main(String[] args)

throws IOException
{
System.out.println("Enter a string or number for which the number of words is to be counted: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String str = br.readLine();

System.out.println("The no of words in the string are: "+str.length());
}
}

Program to Reverse a String in Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class ReverseString
{


/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{

System.out.println("Enter a string to be reversed: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
StringBuffer sb = new StringBuffer().append(str);
System.out.println("The reversed string is:\n"+sb.reverse().toString());
}

}

Aniket Gadekar


aniket gadekar, nagpur
Ph : 08878644295, 09970351838
email : aniketgadekar4967@gmail.com

We Are Founder..