Find out Factorial of a Given Number - with and with out Recursion
Find out Factorial of a Given Number - with and with out Recursion
Here is output :
With Out Recursion
import java.io.*;
class FactorialWithoutRecursion
{
public static void main(String args[]) throws IOException
{
int i, product=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number : ");
int n=Integer.parseInt(br.readLine());
System.out.print("Factorial of " + n + " is.. ");
for(i=n;i>=1;i--)
{
if(i!=1)
{
System.out.print(i + " * ");
}
else
{
System.out.print(i );
}
product=product*i;
}
System.out.println(" = " + product);
}
}
class FactorialWithoutRecursion
{
public static void main(String args[]) throws IOException
{
int i, product=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number : ");
int n=Integer.parseInt(br.readLine());
System.out.print("Factorial of " + n + " is.. ");
for(i=n;i>=1;i--)
{
if(i!=1)
{
System.out.print(i + " * ");
}
else
{
System.out.print(i );
}
product=product*i;
}
System.out.println(" = " + product);
}
}
- Save the file with name FactorialWithoutRecursion.java
- Compile with: javac FactorialWithoutRecursion.java
- Run with: java FactorialWithoutRecursion
With use of Recursion:
Out Put is as following :
import java.io.*;
class Factorial
{
// this is a recursive function
int fact(int n)
{
int result;
if(n!=1)
{
System.out.print(n+" * ");
}
else
{
System.out.print( n );
}
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class FactWithRecursion
{
public static void main(String args[]) throws IOException
{
int r;
Factorial f = new Factorial();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number : ");
int n=Integer.parseInt(br.readLine());
System.out.print("Factorial of "+ n +" is " );
r=f.fact(n);
System.out.print(" = "+r);
}
}
class Factorial
{
// this is a recursive function
int fact(int n)
{
int result;
if(n!=1)
{
System.out.print(n+" * ");
}
else
{
System.out.print( n );
}
if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class FactWithRecursion
{
public static void main(String args[]) throws IOException
{
int r;
Factorial f = new Factorial();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Number : ");
int n=Integer.parseInt(br.readLine());
System.out.print("Factorial of "+ n +" is " );
r=f.fact(n);
System.out.print(" = "+r);
}
}
- Save the file with name FactWithRecursion.java
- Compile with: javac FactWithRecursion.java
- Run with: java FactWithRecursion
String given as command line argument is Palindrome or not
Check String given as command line argument is Palindrome or not?
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String originalString, reverseString="";
originalString = args[0];
int length = originalString.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverseString = reverseString + originalString.charAt(i);
if (originalString.equals(reverseString))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
class Palindrome
{
public static void main(String args[])
{
String originalString, reverseString="";
originalString = args[0];
int length = originalString.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverseString = reverseString + originalString.charAt(i);
if (originalString.equals(reverseString))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
- Save the file with name Palindrome.java
- Compile with: javac Palindrome.java
- Run with: java Palindrome malayalam
- Run with: java Palindrome madam
- Run with: java Palindrome Java
- Run with: java Palindrome good
Fibonacci Series using Java.
Fibonacci Series using Java.
class FibonacciSeries
{
public static void main(String args[])
{
int PrevNum, NextNum, Sum, i;
PrevNum=NextNum=1;
System.out.println();
for(i=1;i<=10;i++)
{
System.out.print(PrevNum+" ");
Sum=PrevNum+NextNum;
PrevNum=NextNum;
NextNum=Sum;
}
}
}
{
public static void main(String args[])
{
int PrevNum, NextNum, Sum, i;
PrevNum=NextNum=1;
System.out.println();
for(i=1;i<=10;i++)
{
System.out.print(PrevNum+" ");
Sum=PrevNum+NextNum;
PrevNum=NextNum;
NextNum=Sum;
}
}
}
- Save the file with name FibonacciSeries.java
- Compile with: javac FibonacciSeries.java
- Run with: java FibonacciSeries
Animation Demo JavaFX
Animation Demo JavaFX
We have come up with New and Better avatar!
Now enjoy our improved version!
This post has been moved to :
http://www.puretechy.com/blog/javafx-animation
Now enjoy our improved version!
This post has been moved to :
http://www.puretechy.com/blog/javafx-animation
Image Editor Using JavaFx
Image Editor Using JavaFX
We have come up with New and Better avatar!
Now enjoy our improved version!
This post has been moved to :
http://www.puretechy.com/blog/javafx-application-image-editor
Sine Wave Using JavaFX
Sine Wave Using JavaFX
We have come up with New and Better avatar!
Now enjoy our improved version!
This post has been moved to :
http://www.puretechy.com/blog/javafx-example-javafx-sine-wave
Now enjoy our improved version!
This post has been moved to :
http://www.puretechy.com/blog/javafx-example-javafx-sine-wave
Subscribe to:
Posts (Atom)