package Loops;
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
System.out.println("the number is "+n);
float result=0;
for(float i=1;i<=n;i++) {
result=result+1/i;
}
System.out.println("the result is "+result);
}
}
output:-
5
the number is 5
the result is 2.2833335
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
System.out.println("the number is "+n);
float result=0;
for(float i=1;i<=n;i++) {
result=result+1/i;
}
System.out.println("the result is "+result);
}
}
output:-
5
the number is 5
the result is 2.2833335
Comments
Post a Comment