java基础编程2

1.求两个数的最大公约数

package day04;
//两个数的最大公约数
import java.util.Scanner;

public class DivisorDemo {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入第一个数:");
		int number1=scan.nextInt();
		System.out.println("请输入第二个数:");
		int number2=scan.nextInt();
		int min=number1;
		if(min>number2)
			min=number2;
		for(int i=min;i>0;i--) {
				if(number1%i==0&&number2%i==0) {
					System.out.println(number1+"和"+number2+"的最大公约数为:"+i);
					break;
				}
			}
		
		

	}

}

2.输出九九乘法表

package day04;
//乘法表
public class Multipilaction {

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

	}

}

3.查找2到某个范围内的质数,并每行为5个,并计算质数的个数

package day04;

import java.util.Scanner;

public class Prime {

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入你要查询的数的范围:2~");
		int number=scan.nextInt();
		scan.close();
		int sum=0;
		for (int i = 2; i <=number; i++) {
			int j=2;
			for(;j<=(int)(Math.sqrt(i));j++) {
				if(i%j==0) {
					break;
				}
				
			}
			if(j>(int)(Math.sqrt(i))) {
				sum+=1;
				System.out.print(i+"\t");
				if(sum%5==0) {
					System.out.println();
				}
			}
		}
		System.out.println("质数的总数是:"+sum);

	}

}

 

你可能感兴趣的:(java基础编程2)