1) REVERSE THE STRING
public class ReverseString {
public static void main (String[]args)
{
String str="Interview";
String rstr= "";
for (int i=0; i<str.length(); i++)
{
rstr= str.charAt(i) + rstr;
}
System.out.println(rstr);
}
}
PALINDROME
public void palendrome ()
{
String num;
Scanner sc= new Scanner (System.in);
System.out.println("Enter the Number");
num= sc.nextLine();
String Revnum = "";
for (int i=num.length()-1;i>=0;i--)
{
Revnum =Revnum + num.charAt(i);
}
System.out.println("Reverse number is" +Revnum);
if (num.equals(Revnum))
{
System.out.println("Number is Palendrome");
}
else
{
System.out.println("Number is not Palendrome");
}
}
PRIME NUMBER
public void primeNumber() {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number");
num = sc.nextInt();
if (num <= 1) {
System.out.println("Number is not prime");
return;
}
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("Number is prime");
} else {
System.out.println("Number is not prime");
}
}
FIBONACCI
public static void main (String[]args)
{
int n = 20;
// Print the first N numbers
for (int i = 0; i < n; i++) {
System.out.print(fib(i) + " ");
}
}
//Fibonacci series
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
}
No comments:
Post a Comment