Small exercise _ num1, exercise _ num1

Source: Internet
Author: User

Small exercise _ num1, exercise _ num1

Question: decompose a positive integer into a prime factor. For example, enter 90 and print the output 90 = 2*3*3*5.

1/* 2 decomposition prime factor 3 */4 import java. util. *; 5 class num1 6 {7 public static void main (String [] args) throws Exception 8 {9 boolean B = true; 10 while (B) 11 {12 try13 {14 then SC = new then (System. in); 15 System. out. println ("************************************* *******************"); 16 System. out. println ("START ___ enter a positive integer greater than one:"); 17 int n = SC. nextInt (); 18 int k = 2; 19 System. out. print (n + "="); 20 while (k <= N) 21 {22 if (k = n) // determines whether n is equal to k. if the equal value indicates that n is a prime number, the search is complete, break! 23 {24 System. out. println (n); break; 25} 26 else if (n % k = 0) // at this time, n is definitely not a prime number, and k is used for remainder. if it can be divisible, this k is a prime factor of 27 {28 System. out. print (k + "*"); n = n/k; 29} 30 else // at this time, n is not a prime number, and k is not a prime factor, from small to large, start searching for the next prime factor 31 k ++; 32} 33 while (k> n) 34 {35 System. out. println ("<ERROR-enter a positive integer greater than 1>"); break; 36} 37} 38 catch (Exception e) 39 {40 System. out. println ("<!! The value you entered is not a positive integer greater than 1 or exceeds the calculation range!> "); 41} 42 seconds scgoon = new second (System. in); 43 System. out. println ("___________________________________________________"); 44 System. out. println ("press enter to continue, enter exit to exit"); 45 String sg = scgoon. nextLine (); 46 if (sg. equals ("exit") 47 {B = false;} 48} 49} 50}

 

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.