Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Jordan Rust, Assignment 3, 11:00 AM, EXTRA CREDIT ATTEMPTED
- import java.util.*;
- public class IntFun
- {
- public static void main (String[] args)
- {
- Scanner kb = new Scanner(System.in);
- //List of all variables.
- int startNum = 0;
- int userNum = 0;
- int odds = 0;
- int evens = 0;
- int zeroes = 0;
- int digit = 0;
- int sum = 0;
- int primeStart;
- //Start of the menu, just to be polite.
- System.out.println("Hello there. Welcome to the number menu.");
- //Gets the user's first integer.
- System.out.println("Please enter a positive integer: ");
- startNum = kb.nextInt();
- //Makes sure the integer is positive.
- if(startNum < 0)
- {
- System.out.println("That isn't a positive integer, please try again.");
- startNum = kb.nextInt();
- }
- //Prints the menu and offers the choice selection.
- if(startNum > 0)
- {
- System.out.println("1.Enter a new number.\n2.Print the number of odd digits, even digits and zereos in the integer.\n3.Print the prime numbers between 2 and the integer.\n4.Print the sum of the digits in the integer.\n5.Quit the program.");
- System.out.println("Your choice please: ");
- userNum = kb.nextInt();
- }
- //Fulfills option one of the menu.
- if(userNum == 1)
- {
- System.out.println("Your new positive integer please: ");
- startNum = kb.nextInt();
- }
- //Fulfills option two of the menu.
- if(userNum == 2)
- {
- boolean isSignificant = false;
- int divisor = 100000000;
- while(divisor > 0)
- {
- digit = startNum/divisor;
- if(digit == 0 && isSignificant == true)
- zeroes ++;
- else if(digit%2 == 0)
- evens ++;
- isSignificant = true;
- if(digit%2 != 0)
- odds ++;
- isSignificant = true;
- startNum = startNum % divisor;
- divisor = divisor/10;
- }
- System.out.println("Evens: " + evens + " Odds: " + odds + " Zeroes: " + zeroes);
- }
- //Fulfills option three of the menu.
- if(userNum == 3)
- {
- System.out.println("The prime numbers are: ");
- for (primeStart = 2; primeStart < startNum; primeStart++ )
- {
- int primeSearch;
- for (primeSearch = 2; primeSearch < primeStart; primeSearch++)
- {
- int primes = primeStart%primeSearch;
- if (primes == 0)
- {
- break;
- }
- }
- if(primeStart == primeSearch)
- {
- System.out.print(" " + primeStart);
- }
- }
- }
- //Fulfills option four of the menu.
- if(userNum == 4)
- {
- int num = startNum;
- while(num > 0)
- {
- digit = num%10;
- sum += digit;
- num = num/10;
- }
- System.out.println("The sum is: " + sum);
- }
- //Fulfills option five of the menu.
- if(userNum == 5)
- {
- System.out.println("You've chosen to quit. Have a nice day!");
- }
- }
- }
Add Comment
Please, Sign In to add comment