Computer Science, asked by sunitazirange, 10 months ago

Import java.util.Scanner;
public class series2
{
public static void main(String[] args)
{
double sum = 0;
int n;
System.out.println("1/1! + 2/2! + 3/3! + ..N/N!");
Scanner sc = new Scanner(System.in);
System.out.print("Enter the no.of terms in series:");
n = sc.nextInt();
Sum obj = new Sum();
for(int i = 1; i <= n; i++)
{
sum = sum + (double)i / (obj.(i));
}
System.out.println("Sum of series:"+sum);
}
int fact(int x)
{
int mul = 1;
while(x > 0)
{
mul = mul * x;
x--;
}
return mul;
}
}




can anybody please tell me what is wrong in this?
this is a java program

Answers

Answered by Anonymous
1

Explanation:

i am rewriting the code !!!

i am rewriting the code !!!you check out for the mistakes!!

import java.util.Scanner;

public class series2

{

public static void main(String[] args)

{

double sum=0;

int n;

System.out.println("1/1!+2/2!+3/3!+...N/N!");

Scanner sc=new Scanner(System.in);

System.out.print("Enter the no. of terms in the series");

n=sc.nextInt();

series2 obj=new series2();

for(int i=1;i<=n;i++)

{

int k=obj.fact(i);

int k=obj.fact(i);sum=sum+(double)i/k;

}

System.out.println("Sum of the series:"+sum);

}

int fact(int x)

{

int mul=1;

while (x>0)

{

mul=mul*x;

x--;

}

return mul;

}

}

i have corrected your program..

following were your mistakes:

1-you created an object obj with an invalid class name Sum which should have been series2.

2-your fuction was of type returning and parametrized,so when you invoke the function then it needs to be either stored in a variable of similar data type or should be print statement.so,i used a variable k of int data type.

3-your method of invoking the function was wrong.

I HAVE ALSO CHECKED IT!!!

ATTACHMENTS SHOW THAT IT IS EXECUTING SUCCESSFULLY!!!

Attachments:
Similar questions