Java learning summary 2

1. printout of all the "number of Narcissus", the so-called "number Narcissus" means a 3-digit number, which is equal to the digits of the cube and the number itself. For example, 153 is a "number daffodils."

2. Write a Java program, find the value of + 13-23 + 33-43 + 973-983 + 993-1003 ... of.

3. Programming seek 1! +2! +3! + ... + 20 !.

4. The write Java programs, calculation 8 + 88 + 888 + 10 ... and the front.

5. If a number is exactly equal to the sum of its factors, this number is called the complete number. All finished programming the number of output within 1000.

6. write applications, output meets + ... + n <maximum positive integer 1 + 2 + 3 8888.

7. FIG below for printing cycle (isosceles triangle)

Code as follows:
1,

package homework.one;

public class testreport1 {
    public static void main(String[] args) {
        long i=100;
        long sum,j,k,h;
        for(i=100;i<=999;i++){
            sum=i%10;
            j=i/10;
            h=j%10;
            k=j/10;
            if(i==Math.pow(sum,3)+Math.pow(h,3)+Math.pow(k,3)){
                System.out.println(i);
            }
        }
    }
}

2、

package homework.one;

public class testreport2
{
    public static void main(String[] args)
    {
        int i, j = 0, k = 3;
        int flag = 1;
        for (i = 1; i <= 100; i++)
        {
            j = j + flag * (k + (i * 10));
            flag = -flag;
        }
        System.out.println(j);
    }
}

3、

package homework.one;

public class testreport3
{
    public static void main(String[] args)
    {
        long count = 0;
        int j;
        for (j = 1; j <= 20; j++)
        {
            count = count + factorial(j);
        }
        System.out.println(count);
    }
    public static long factorial(long n)
    {
        long i;
        long sum = 1;
        for (i = 1; i <= n; i++)
        {
            sum = sum * i;
        }
        return sum;
    }
}

4、

package homework.one;

public class testreport4
{
    public static void main(String[] args)
    {
      long j = 0;
      long sum=0;
        for (int i = 1; i <= 10; i++)
        {
            j = j + (sum * 10) + 8;
            sum=sum*10+8;

        }
        System.out.println(j);
    }
}

5、

package homework.one;

public class testreport5
{
    public static void main(String[] args)
    {
        int i, j;                            //i是完数,j是因数
        for (i = 1; i <= 1000; i++)
        {
            int sum=0;
            for (j = 1; j <= i; j++)
            {
                if (i % j == 0)             //判断j是否为i的因数,若是则sum加j
                {
                    sum = j + sum;
                }
            }
                if ((sum - i) == i)     //判断i是否为完数,因子之和sum-i等于本身i
                {
                    System.out.println(i);//输出完数
                }
        }
    }
}

6、

package homework.one;

public class testreport6
{
    public static void main(String[] args)
    {
        int i = 1;
        int sum = 0;
        int n;
        for (i = 1; ; i++)
        {
            sum = sum + i;
            n = i - 1;
            if (sum >= 8888)
            {
                System.out.println(n);
                System.out.println(sum - i);
                break;
            }
        }
    }
}

7、

package homework.one;

public class testreport7 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(" *");
            }
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Operating results are as follows:

Guess you like

Origin www.cnblogs.com/94ha-xc/p/11494108.html