Java BASIC Programming exercises

Source: Internet
Author: User

- Tao JAVA BASIC Programming Exercises

"Program 1"

Title: Classical Questions: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the number of rabbits each month?

Program Analysis: The law of Rabbits for the series 1,1,2,3,5,8,13,21 ....

public class prog1{

       public static void Main (string[] args) {

               int n = ten;

              System.out.println ("+n+" Each month the total number of rabbits is "+fun (n)");

      }

       private static int fun (int n) {

               if (n==1 | | n==2)

                  return 1;

              Else

                 return Fun (n-1) + Fun (n-2);

      }

}

"Program 2"

Title: Determine the number of primes between 101-200 and the output of all primes.

Program Analysis: The method of judging primes: to remove 2 to sqrt (this number) with a number, if divisible, indicates that the number is not a prime, and vice versa is a prime.

public class prog2{

public static void Main (string[] args) {

int m = 1;

int n = 1000;

int count = 0;

Number of statistical primes

for (int i=m;i<n;i++) {

if (IsPrime (i)) {

count++;

System.out.print (i+ "");

if (count%10==0) {

System.out.println ();

}

}

}

System.out.println ();

System.out.println ("+count+" in "+m+" and "+n+");

}

Judging prime numbers

private static Boolean isprime (int n) {

Boolean flag = true;

if (n==1)

Flag = false;

else{

for (int i=2;i<=math.sqrt (n); i++) {

if ((n%i) ==0 | | n==1) {

Flag = false;

Break

}

Else

Flag = true;

}

}

return flag;

}

}

···

"Program 3"

Title: Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, its number of cubes and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three times the square +5 of the three +3 Times Square.

Program Analysis: Use for loop control 100-999 number, each number decomposition out of bits, 10 bits, hundred.

public class prog3{

public static void Main (string[] args) {

for (int i=100;i<1000;i++) {

if (Islotus (i))

System.out.print (i+ "");

}

System.out.println ();

}

Judging the number of daffodils

private static Boolean islotus (int lotus) {

int m = 0;

int n = Lotus;

int sum = 0;

m = n/100;

N-= m*100;

sum = m*m*m;

m = N/10;

N-= m*10;

Sum + = m*m*m + n*n*n;

if (sum==lotus)

return true;

Else

return false;

}

}

Java BASIC Programming exercises

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.