Task - Exception Handling - Factorial of a Number #28
Replies: 61 comments 1 reply
|
`import java.util.Scanner; public static int factorial(int n) throws IllegalArgumentException int fac = 1; } } ` |
|
|
`package ExceptionHandling; import java.util.Scanner; class MathUtils{ } public class FactorialDriver { } |
|
`import java.util.Scanner; public class FactorialExcep { }` |
|
|
|
|
|
|
`import java.util.Scanner; public class exceptiontask { }` |
|
|
|
`import java.util.Scanner; class MathUtils { } |
|
|
Exception Handling `import java.util.Scanner; public class Fact { }` |
User Defined Exceptionsimport java.util.Scanner;
class MyException extends Exception {
MyException() {
}
MyException(String msg) {
super(msg);
}
}
class MathUtils {
// -------------------------------------------------------------
// Returns the factorial of the argument given
// -------------------------------------------------------------
public static int factorial28(int n) throws MyException {
if (n < -16) {
throw new MyException(
"Negative integer provided. The factorial is bigger than can be represented by an int.");
}
if (n < 0) {
throw new MyException("Negative integer provided");
}
if (n > 16) {
throw new MyException("Integer greater than 16 provided");
}
int fac = 1;
for (int i = n; i > 0; i--)
fac *= i;
return fac;
}
}
public class discussion28 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter integer");
int n = sc.nextInt();
try {
System.out.println("Factorial of no :" + MathUtils.factorial28(n));
} catch (MyException e) {
System.out.println(e);
}
}
} |
import java.util.Scanner;
class MathUtils
{
public static int factorial(int n)
{
if(n < 0)
{
throw new IllegalArgumentException("Please Enter a positive number for Factorial");
}
if(n > 16)
{
throw new IllegalArgumentException("Please Enter a number less than 17 to get desired output");
}
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}
public class FactCheck {
public static void main(String[] args) {
System.out.println("Enter a positive or negative number, negative number should be less than -17\n");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
try {
int fact = MathUtils.factorial(number);
System.out.println("Factorial of the number is = "+fact);
}catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
main(args);
}
}
} |
class MathUtils {
// -------------------------------------------------------------
// Returns the factorial of the argument given
// -------------------------------------------------------------
public static int factorial(int n) throws IllegalArgumentException{
int fac = 1;
if(n>16)
{
throw new IllegalArgumentException(" arithmetic overflow : the factorial is bigger than can be represented by an int "+n);
}
if(n<0)
{
throw new IllegalArgumentException("factorial for negative number not allowed" +n);
}
for(int i = n; i > 0; i--) {
fac *= i;
}
return fac;
}
public static void main(String[] args) {
for (int i=-1;i<18;i++)
{
try {
System.out.println("Factorial of "+i+ "is "+MathUtils.factorial(i));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
} |
codeimport java.util.Scanner;
public class Discussion28{
public static int factorial(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException("Argument passed should not be less than 0");
}
if (n > 16) {
throw new IllegalArgumentException("Argument passed should not be greater than 16");
}
int factorial = 1;
for (int i = n; i > 0; i--)
factorial *= i;
return factorial;
}
public static void main(String[] args) throws IllegalArgumentException{
try {
Scanner sc = new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
} |
Codeimport java.util.Scanner;
public class FactorialException {
public static int factorial(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException("Argument passed should not be less than 0");
}
if (n > 16) {
throw new IllegalArgumentException("Argument passed should not be greater than 16");
}
int fac = 1;
for (int i = n; i > 0; i--)
fac *= i;
return fac;
}
public static void main(String[] args) throws IllegalArgumentException{
try {
Scanner sc = new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
} |
codepackage assignment28;
import java.util.Scanner;
public class Factorials {
public static int factorial(int n) throws IllegalArgumentException{
if(n<0) {
throw new IllegalArgumentException("VAlue should not be less than zero");
}
if(n>16) {
throw new IllegalArgumentException("Value should not greater than 16");
}
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int n = sc.nextInt();
try {
System.out.println(factorial(n));
}catch(IllegalArgumentException e){
System.out.println(e);
}
}
} |
|
import java.util.*; }`` |
package p1;
import java.util.Scanner;
class MathUtils
{
public static int factorial(int n)
{
if(n < 0)
{
throw new IllegalArgumentException("Please Enter a positive number for Factorial");
}
if(n > 17)
{
throw new IllegalArgumentException("Please Enter a number less than 17 to get desired output");
}
int fac = 1;
for (int i=n; i>0; i--)
fac *= i;
return fac;
}
}
public class FactCheck {
public static void main(String[] args) {
System.out.println("Enter a positive or negative number, negative number should be less than -17\n");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
try {
int fact = MathUtils.factorial(number);
System.out.println("Factorial of the number is = "+fact);
}catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
} |
class MathUtils {
// -------------------------------------------------------------
// Returns the factorial of the argument given
// -------------------------------------------------------------
public static int factorial(int n) throws IllegalArgumentException{
int fac = 1;
if(n>16)
{
throw new IllegalArgumentException(" arithmetic overflow : the factorial is bigger than can be represented by an int "+n);
}
if(n<0)
{
throw new IllegalArgumentException("factorial for negative number not allowed" +n);
}
for(int i = n; i > 0; i--) {
fac *= i;
}
return fac;
}
public static void main(String[] args) {
for (int i=-1;i<18;i++)
{
try {
System.out.println("Factorial of "+i+ "is "+MathUtils.factorial(i));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
} |
import java.util.Scanner;
public class Factorial {
public static int factorial(int num) throws IllegalArgumentException {
if (num < 0) {
throw new IllegalArgumentException("Argument passed should not be less than 0");
}
else if (num > 20) {
throw new IllegalArgumentException("Argument passed should not be greater than 20");
}
else {
int fact = 1;
for (int i = 1; i <= num; i++)
fact *= i;
return fact;
}
}
public static void main(String[] args) throws IllegalArgumentException{
try {
Scanner sc = new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
} |
import java.util.Scanner;
public class Factorial {
public static int factorial(int num) throws IllegalArgumentException {
if (num < 0) {
throw new IllegalArgumentException("Argument passed should not be less than 0");
}
else if (num > 20) {
throw new IllegalArgumentException("Argument passed should not be greater than 20");
}
else {
int fact = 1;
for (int i = 1; i <= num; i++)
fact *= i;
return fact;
}
}
public static void main(String[] args) throws IllegalArgumentException{
try {
Scanner sc = new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
} catch (IllegalArgumentException e) {
System.out.println(e);
}
}
} |
|
Naga Nandhini import java.util.Scanner;
public class Factorials {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("Enter an integer to compute its factorial (or type 'exit' to quit): ");
String input = sc.nextLine();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Exiting...");
break;
}
try {
int num = Integer.parseInt(input);
int result = MathUtils.factorial(num);
System.out.println("Factorial of " + num + " is: " + result);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
}
sc.close();
}
}
class MathUtils {
public static int factorial(int n) throws IllegalArgumentException {
if (n < 0)
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
if (n > 16)
throw new IllegalArgumentException("Input too large. Factorial is only supported for values <= 16.");
int fac = 1;
for (int i = n; i > 0; i--)
fac *= i;
return fac;
}
} |
|
PRASETHA N package com.practice.problems.java;
import java.util.Scanner;
public class Factorials {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
try {
System.out.println("Factorial of no: " + MathUtils.factorial(n));
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
class MathUtils {
public static int factorial(int n) throws IllegalArgumentException {
if (n < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers: " + n);
}
if (n > 16) {
throw new IllegalArgumentException("Factorial result too large to fit in int for: " + n);
}
int fac = 1;
for (int i = n; i > 0; i--) {
fac *= i;
}
return fac;
}
} |
|
|
import java.util.Scanner; public class MathUtil { } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
WAP that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. Study the code, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for larger integers and that it always returns 1 for negative integers.
All reactions