Click here to Skip to main content
15,916,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I print the Scanner the output of the functions above, and I am a beginner in Java

What I have tried:

    import java.util.Scanner;

   public class factorial {


   // Factorial number  Non Recursive Use a for loop
    public static int factorial(int num) {
      int n=1;
      if(num==0)
          return 1;
      for(int i=1; i<=num; i++) {
          n=n*i;
      }
          return n;
    }


   // With Recursive: a function is calling itself
   public static int r(int numb) {
    if(numb==0)
        return 1;

        else
        return (numb * r (numb-1));

}



    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter factorial Number: ");
        System.out.println("Factorial Recursive is: "+n);
        System.out.println("Factorial Number Non-Recursive is: "+numb);




 }  // End of main


   }  // End of class
Java

Posted
Updated 27-Mar-21 9:44am

1 solution

Look at your main method: You read the number to calculate the factorial of before you prompt the user to enter it!

Then printing your answers is simple - you just have to call the method you wrote and print the result.

If you define a method then calling it is easy:
Java
public static void main(String[] args) {
    System.out.println(" 3 + 6 = " + Add(3, 6));
}
private static int Add(int x, int y) {
    return x + y;
}
 
Share this answer
 
Comments
iE7TRAF702 28-Mar-21 7:17am    
thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900