Question

In: Computer Science

JAVA PROGRAM 1. Write a program to find the factorial value of any non-negative number entered...

JAVA PROGRAM

1. Write a program to find the factorial value of any non-negative number entered through the keyboard.(method) (Factorial of n: n! = 1*2*3*…*n, 0! = 1.)

2. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (method)

Please post a screenshot of the codes. Thanks!

Solutions

Expert Solution

(1)

import java.util.Scanner;

//creating class factorial
public class Factorial {
   //Declare method for calculating factorial
   public int fact(int fact)
   {
       int f=1;
       for(int i=fact;i>0;i--)
       {
           f=f*i;
       }
       return f;
   }
   //main method
public static void main(String[] args) {
   //creating data variable
   int num;
   int fact = 1;
   //creating object of scanner class for taking input from user
   Scanner input=new Scanner(System.in);
   //creating object of our class
   Factorial fc=new Factorial();
   System.out.print("Enter a number: ");
   //taking input from user
   num=input.nextInt();
   //check number is greater then 0 or not
   if(num>=0)
   {
   fact=fc.fact(num);
   System.out.println("Factorial of "+num +" is :"+fact);
   }
   else {
       System.out.println("plz entered value greater then 0");
   }
      input.close();
}
}

screenshot -

Output-

(2)

import java.util.Scanner;

//creating class prime
public class Prime {
   //Declare method prime for check number is prime or not
public void isPrime(int num)
{
   int i,m=0,flag=0;
   m=num/2;
   if(num==0||num==1){
   System.out.println(num+" is not prime number");
   }else{
   for(i=2;i<=m;i++){
   if(num%i==0){
   System.out.println(num+" is not prime number");
   flag=1;
   break;
   }
   }
   if(flag==0) { System.out.println(num+" is prime number"); }
   }
  
}
public static void main(String[] args) {
   //creating object of scanner class for taking input from user
       Scanner input=new Scanner(System.in);
       //creating object of our class
       Prime prime=new Prime();
       System.out.print("enter positive Integer: ");
       //taking input from user
       int num=input.nextInt();
       if(num>=0)
       {
           //calling method isPrime
       prime.isPrime(num);  
       }
       else {
           System.out.println("Please enter a positive number");
       }
       input.close();
}
}

screenshot-


Output-


Related Solutions

Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is...
Assignment Instructions: 1) The Factorial The factorial of a non-negative integer ??, denoted by ??!, is the product of all positive integers less than or equal to ??. The textbook has an example of a recursive MIPS implementation of factorial. Additionally, a simplified version of the MIPS assembly language recursive implementation of the factorial function is attached. Trace the factorial example carefully using QTSPIM 2) Recursive definition of multiplication The function ??????????(??, ??) for two positive integers 1 ? ??,...
Write java program that reads daily sales for 30 days until -1 is entered and find...
Write java program that reads daily sales for 30 days until -1 is entered and find and display the following: a) average sales for 30 days. b) number of days that daily sales over $5000.00             
Write down the C++ Program To Find Factorial.
Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
In Java please What happens if you call factorial( with a negative value of n? With...
In Java please What happens if you call factorial( with a negative value of n? With a large value of say 35? Write a recursive function that takes an integer n as its argument and returns ln(n!)
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and...
C++ [2] Write a program that prompts the user to enter a non-negative decimal number and a base in the range 2 <= base <= 16. Write a function multibaseOutput() that displays the number in the specified base. The program terminates when the user enters a number of 0 and a base 0. Run: Enter a non-negative decimal number and base (2 <= B <= 16) or 0 0 to terminate: 155 16     155 base 16 is 9B Enter...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
Use Java (Find the number of days in a month) Write a program that prompts the...
Use Java (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, If the user entered month 2 and year 2012, the program should display that February 2012 has 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 has 31 days. Sample Run 1 Enter a month in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT