Wednesday, March 16, 2016

Factorial In Java

Factorial In Java - Java programming language tutorial on how to get the result of factorial in java.
Series Summation In Java

How to get factorial injava

import java.io.*;

class nine{
public static void main(String args[]) throws IOException{
Integer value1,value2,sum,sub,mul,div,temp,loop,series = 0;
String s,s1,s2,s3;

InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);

System.out.print("Enter 1st value = ");
s = br.readLine();

System.out.print("Push 2nd value = ");
s2 = br.readLine();

value1 = Integer.valueOf(s);
value2 = Integer.valueOf(s2);
sum = value1+value2;
sub = value1-value2;
mul = value1*value2;
div = value1/value2;

System.out.println("------Output------------------------------------- ");
System.out.println("Addition of two no = "+sum);
System.out.println("Subtraction of two no = "+sub);
System.out.println("Multiplication of two no = "+mul);
System.out.println("Division of two no = "+div);

temp = sum;
sum = div;
div = temp;

temp = sub;
sub = mul;
mul = temp;

System.out.println("-----After Swapping with variable----------------- ");
System.out.println("Addition of two no = "+sum);
System.out.println("Subtraction of two no = "+sub);
System.out.println("Multiplication of two no = "+mul);
System.out.println("Division of two no = "+div);

sum = sum+div;
div = sum-div;
sum = sum-div;

sub = sub+mul;
mul = sub-mul;
sub = sub-mul;

System.out.println("-----After Swapping without variable method 1----- ");
System.out.println("Addition of two no = "+sum);
System.out.println("Subtraction of two no = "+sub);
System.out.println("Multiplication of two no = "+mul);
System.out.println("Division of two no = "+div);

System.out.println("-----Series Summation----------------------------- ");
if(value2 > value1){
for(loop=value1; loop<=value2; loop++){
series = series + loop;
}
System.out.println("Series summation is " + series);
if(series%2==0){
System.out.println("Series summation even number ");
}else{
System.out.println("Series summation odd number ");
}
}else{
System.out.println("Series summation is not possible between "+value1+" & " +value2);
}

Integer a = 1,b,n=1;
System.out.println("Enter a value for b = ");
s3 = br.readLine();
b = Integer.valueOf(s3);
System.out.println("-----Factorial number----------------------------- ");
for(loop = a; loop <= b; loop++){
n = n*loop;
}

        System.out.print("The factorial of "+b+" is : "+n+" ");
        if(n%2==0){
            System.out.println("and which is an even number");
        }else{
            System.out.println("and which is an odd number");
        }

}
}


You Like It, Please Share This Article Using...




Don't Forget To Read This Also...




No comments :

Speak Your Mind: