Java Foundation Instance (i)

Source: Internet
Author: User

1 Write a method to print a 99 multiplication table with a for loop/**
* One for loop print 99 multiplication table
*/
public void nineninemultitable ()
{
for (int i = 1,J = 1; J <= 9; i++) {
System.out.print (i+ "*" +j+ "=" +i*j+ "");
if (I==J)
{
i=0;
j + +;
System.out.println ();
}
}
}

2. Write a method that can infer whether an integer is prime (prime number except 1 times its own no other can get this value)/**
* Infer whether an integer is a prime number
* @paramn
* @returnboolean
*/
public boolean isprimes (int n)
{
for (int i = 2; I <= math.sqrt (n); i++) {
if (n%i==0)
{
return false;
}
}
return true;
}


3. Write a method, enter a random integer, and return its factorial (for example, 3 is 1*2*3=6)/**
* Get the factorial of a random integer
* @paramn
* @returnn!
*/
publicint factorial (int n)
{
Recursive
if (n==1)
{
return 1;
}
Return n*factorial (n-1);
Non-recursive
int multi = 1;
for (int i = 2; I <= n; i++) {
Multi*=i;
// }
return multi;
}



Java Foundation Instance (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.