Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Friday, February 10, 2012

Recursive function in java to print all natural numbers from 1 upto (n-1)

// recursive func to print a series of natural nos. upto (x-1)
import java.io.*;
public class RecFUNCseries
{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter an integer: ");
int x=Integer.parseInt(br.readLine());
series(x);
}
public static void series(int x)
{
if(x>0)
series(--x);
if(x!=0)
System.out.print(x+" ");
}
}

Java Program to print the initials of a name with last name written in full

public class Initials
{
public static void initials(String S)
{
int[]A=new int[S.length()];
int sp=0;
for(int i=0; i if(S.charAt(i)==' ')
A[sp++]=i;

if(sp==0)
System.out.println(S);
else
{

System.out.print(S.charAt(0)+". ");

for(int i=0; i
System.out.print(S.charAt(A[i]+1)+". ");


System.out.print(S.substring(S.lastIndexOf(' ')+1, S.length()));
}
}
}

Wednesday, February 8, 2012

java program To print a String in reverse order using recursion

// to print a string in reverse order using recursion
import java.io.*;
class reverseString
{
public static void main(String args[])throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a String: ");

String s=br.readLine();
reverse(s, s.length());
}

public static void reverse(String S, int a)throws IOException
{
System.out.print(S.charAt(--a));
if(a>0)
reverse(S,a);
}
}

Saturday, February 6, 2010

Shortest Function to find if a number is prime

class prime
{
public static boolean PrimeCheck(int a)//a should be greater than 0
{
for(int i=2; i<=a/2; i++)
if(a%i==0)
return false;
return true;
}
}