标签:arrays sig elements red XA element ++ object tps
打印前100个prime numbers
public static void main(String[] args) { Scanner scan = new Scanner(System.in); int N = scan.nextInt(); int x = 2; for(int i = 0; i <= N; i++) { int count = 0; for(int j = 1; j <= x; j++) if(x%j == 0) count++; if(count == 2) System.out.print(x + " "); x++; } }
import java.util.Scanner; public class ExArraySortElement { public static void main(String[] args) { int n, temp; //scanner class object creation Scanner s = new Scanner(System.in); //input total number of elements to be read System.out.print("Enter the elements you want : "); n = s.nextInt(); //integer array object int a[] = new int[n]; //read elements System.out.println("Enter all the elements:"); for (int i = 0; i < n; i++) { a[i] = s.nextInt(); } //sorting elements for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } //print sorted elements System.out.println("Ascending Order:"); for (int i = 0; i < n ; i++) { System.out.println(a[i]); } } }
https://www.includehelp.com/java-programs/sort-an-array-in-ascending-order.aspx
import java.util.Scanner; class PrimeCheck { public static void main(String args[]) { int temp; boolean isPrime=true; Scanner scan= new Scanner(System.in); System.out.println("Enter any number:"); //capture the input in an integer int num=scan.nextInt(); scan.close(); for(int i=2;i<=num/2;i++) { temp=num%i; if(temp==0) { isPrime=false; break; } } //If isPrime is true then the number is prime else not if(isPrime) System.out.println(num + " is a Prime Number"); else System.out.println(num + " is not a Prime Number"); } }
https://beginnersbook.com/2014/01/java-program-to-check-prime-number/
public boolean isPalindrome(String text) { String clean = text.replaceAll("\\s+", "").toLowerCase(); int length = clean.length(); int forward = 0; int backward = length - 1; while (backward > forward) { char forwardChar = clean.charAt(forward++); char backwardChar = clean.charAt(backward--); if (forwardChar != backwardChar) return false; } return true; } import java.util.*; class Palindrome { public static void main(String args[]) { String original, reverse = ""; // Objects of String class Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); original = in.nextLine(); int length = original.length(); for ( int i = length - 1; i >= 0; i-- ) reverse = reverse + original.charAt(i); if (original.equals(reverse)) System.out.println("Entered string is a palindrome."); else System.out.println("Entered string isn‘t a palindrome."); } }
public int countWord(String word, File file) { int count = 0; Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String nextToken = scanner.next(); if (nextToken.equalsIgnoreCase(word)) count++; } return count; }
标签:arrays sig elements red XA element ++ object tps
原文地址:https://www.cnblogs.com/immiao0319/p/9164701.html