public static void main(String[] args) {
// TODO code application logic here
int number = 5;
long fact = 1;
for(int i = 1; i <= number; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
The basic unit of OOP is a class, which encapsulates both the static attributes and dynamic behaviors within a "box", and specifies the public interface for using these boxes. In addition to organizing code into individual classes, the Java OOP programmer typically organizes classes themselves into packages. A package is basically a named collection of classes (and interfaces) that are bundled together.
Packages help manage the namespace, or prevent naming conflicts. They also help you organize your files on disk and make them easier to access by other developers using your software. In addition, packages provide access protection: if any class in a package is declared as private, then only those classes within that package can use it.
The java.lang Package
The Java programming language provides a wrapper class that "wraps" the char in a Character object for this purpose. An object of type Character contains a single field whose type is char. This Character class also offers a number of useful class (i.e.,static) methods for manipulating characters. For example:
Character c = new Character('a');
System.out.println(Character.isLetter(c)); //true
System.out.println(Character.isDigit(c)); //false
System.out.
public class FactorialExample {
public static void main(String[] args) {
//This is the number to calculate it's factorial
int number = 10;
System.out.println("Factorial of "+number+" is: "+factorial(number));
}
/*
* Java method to calculate factorial of a given number using recursion
*/
public static long factorial(int number){
if(number == 0){ //base case
return 1; //if number == 0, return 1 as per math definition of factorial (0!)==1 }else{ //recursive call return number*factorial(number -1); } } }
public class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
What we're going to do is to create a class called Factorial, which will contain a method called calcFactorial. This method will take in an integer and return the factorial of that integer.
The main method will be used to test this class.
public class Factorial {
public static int calcFactorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println("3! = " + calcFactorial(3)); // 6
}
}
// Java program to find factorial of a number
import java.lang.*;
import java.io.*;
import java.util.*;
class Factorial
{
// Method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;
return n*factorial(n-1);
}
// Driver method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of "+ num + " is " + factorial(5));
}
}
import java.util.*;
public class Factorial {
public static void main(String[] args) {
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Output: Factorial of 5 is: 120
Tomy is a contributor at AskMeCode. We are committed to providing well-researched, accurate, and valuable content to our readers.
You May Also Like
A Quick Guide On VS Code
Want to write some code in vs code? It is a great editor, and there are many plugins you can...
Why GitHub Is Awesome For Cross Platform Apps
I’ve been writing cross-platform apps for the last 6 years. The most common question I get is, “How do you...
Latex basics and Why you should know them ?
Latex code: %&LaTeX \documentclass[11pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage[T1]{fontenc} \usepackage{amsmath, amsthm, amssymb} \author{John Doe} \begin{document} \title{\LaTeX } \maketitle \tableofcontents \section{Introduction to $\LaTeX$...
Cut-Throat Competition
Since the pandas industry has become cut-throat, the companies that have had to suffer the most are those that don't...
