Showing posts with label natural numbers. Show all posts
Showing posts with label natural numbers. 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+" ");
}
}