Advertisements
Advertisements
Question
Observe the following code. It may contain Syntax, Logical or runtime errors. Execute it in Java and identify the error, if any. Rewrite the code without the error. Also write the aim of the code.
import java.io.*;
class Paper
}
int fnBox(int limit)
{
for(int sum = 0, t= 1; t <= limit;t++)
sum = sum + t;
return sum;
}
void main()
{
int N;
Scanner sc = new Scanner(System.in);
System.out.print("Enter limit:");
N = sc.nextInt();
System.out.print("Result=" + fnBox(N));
}
}Code Writing
Advertisements
Solution
import java.io.*;
import java.util.*;
class Paper
}
int fnBox(int limit)
{
for(int sum = 0, t= 1; t <= limit;t++)
sum = sum + t;
return sum;
}
void main()
{
int N;
Scanner sc = new Scanner(System.in);
System.out.print("Enter limit:");
N = sc.nextInt();
System.out.print("Result=" + fnBox(N));
}
}shaalaa.com
Is there an error in this question or solution?
