example using for find value of the series
//Program using for construct
//1+1/1!+1/2!+1/3!+1/4!
package flowcontrol;
public class ForContruct {
public static void main(String[] args) {
int i,j,no;
float sum=1.0f;
int prod;
for(i=1 ; i<5;i++){
//find factorial of i
prod=1;
for(j=1;j<=i;j++){
prod=prod * j;
}
System.out.print(prod);
sum=sum + 1.0F/prod;
}
System.out.println(" sum = " + sum);
}
}
output: 12624 sum = 2.7083335
0 comments:
Post a Comment