Five questions for the Java admissions Exam

Source: Internet
Author: User

Print Top 100 prime numbers

 Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); intN =Scan.nextint (); intx = 2;  for(inti = 0; I <= N; i++)    {        intCount = 0;  for(intj = 1; J <= X; J + +)            if(X%j = = 0) Count++; if(Count = = 2) System.out.print (x+ " "); X++; }}

ImportJava.util.Scanner; Public classexarraysortelement { Public Static voidMain (string[] args) {intN, temp; //Scanner class object creationScanner s =NewScanner (system.in); //input Total number of elements to is readSystem.out.print ("Enter The Elements you want:"); N=S.nextint (); //Integer Array Object        intA[] =New int[n]; //Read ElementsSystem.out.println ("Enter All the Elements:");  for(inti = 0; I < n; i++) {A[i]=S.nextint (); }        //sorting elements         for(inti = 0; I < n; i++)         {             for(intj = i + 1; J < N; J + +)             {                if(A[i] >A[j]) {Temp=A[i]; A[i]=A[j]; A[J]=temp; }            }        }        //Print sorted elementsSystem.out.println ("Ascending Order:");  for(inti = 0; I < n; i++) {System.out.println (a[i]); }    }   }

Https://www.includehelp.com/java-programs/sort-an-array-in-ascending-order.aspx

ImportJava.util.Scanner;classprimecheck{ Public Static voidMain (String args[]) {inttemp; BooleanIsprime=true; Scanner Scan=NewScanner (system.in); System.out.println ("Enter any number:"); //capture the input in an integer    intnum=Scan.nextint ();    Scan.close ();  for(inti=2;i<=num/2;i++) {Temp=num%i; if(temp==0) {IsPrime=false;  Break; }    }    //If IsPrime is true and the number is prime else not    if(isprime) System.out.println (Num+ "is a Prime number"); ElseSystem.out.println (Num+ "is not a Prime number"); }}

https://beginnersbook.com/2014/01/java-program-to-check-prime-number/

 Public Booleanispalindrome (String text) {string clean= Text.replaceall ("\\s+", "" "). toLowerCase (); intLength =clean.length (); intForward = 0; intbackward = Length-1;  while(Backward >forward) {        CharForwardchar = Clean.charat (forward++); CharBackwardchar = Clean.charat (backward--); if(Forwardchar! =Backwardchar)return false; }    return true;}ImportJava.util.*; classpalindrome{ Public Static voidMain (String args[]) {string original, reverse= "";//Objects of String classScanner in =NewScanner (system.in); System.out.println ("Enter a string to check if it is a palindrome"); Original=In.nextline (); intLength =original.length ();  for(inti = length-1; I >= 0; i--) Reverse= Reverse +Original.charat (i); if(original.equals (reverse)) System.out.println ("Entered string is a palindrome."); ElseSystem.out.println ("entered string isn ' t a palindrome."); }}    

 Public int Countword (String Word, file file) {int count = 0new  Scanner (file);  while (Scanner.hasnextline ()) {    = scanner.next ();     if (Nexttoken.equalsignorecase (word))    count+ +;} return count;}

Five questions for the Java admissions Exam

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.