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

1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
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
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
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 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 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 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
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a...
For the following Ackerman function defined for non-negative integers, write a Java program that: Contains a method called computeAckermann that takes two integer values as parameters and calculates and returns the value of the Ackerman function. The method should be defined inside a class named Ackermann. Test the implementation of Ackerman in (a) by calling it from main function and printing the returned value. The main function should be defined in a separate class named AckermannDemo.
Write a program that reads n integer values. If a negative value is entered, we want...
Write a program that reads n integer values. If a negative value is entered, we want to terminate the input, i.e., exit from the loop. If a zero value is entered, we want to ignore it and read the next value. Any strictly positive values (greater or equal zero) are to be totaled. Print the number of values read, the number of values totaled and the total. If a negative value is entered, print an error message before terminating the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT