Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by Chris Hayes ( 14 years ago )
//Programmed by Chris Hayes
//Extra credit attempted
import java.util.Scanner;
public class IntFun
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int choice = 6;
int num;
System.out.println("Enter a positive integer ");
num = kb.nextInt();
kb.nextLine();
while(num < 0)
{
System.out.println("Sorry, the integer needs to be positive, try again ");
num = kb.nextInt();
}
while(choice > 0 && choice != 5)
{
System.out.println("1. Enter a new number ");
System.out.println("2. Print the number of odd digits, even digits and zeroes in the integer ");
System.out.println("3. Print the prime numbers between 2 and the integer ");
System.out.println("4. Print the sum of the digits of the integer ");
System.out.println();
System.out.println("5. Quit the program ");
System.out.println();
System.out.println("Enter your choice ");
choice = kb.nextInt();
kb.nextLine();
if(choice == 1)
{
System.out.println("Enter the new number ");
num = kb.nextInt();
kb.nextLine();
}
else if(choice == 2)
{
oddEven(num);
}
else if(choice == 3)
{
prime(num);
}
else if(choice == 4)
{
sum(num);
}
}
}
private static void oddEven(int num)
{
int evens = 0;
int odds = 0;
int zeroes = 0;
while(num > 0)
{
int digit = num % 10;
if(digit == 0)
{
zeroes++;
}
else if(digit % 2 == 0)
{
evens++;
}
else
{
odds++;
}
}
num = num / 10;
System.out.println("Here is the answer: " + evens + " evens, " + odds + " odds " + zeroes + " zeroes ");
System.out.println();
}
private static void prime(int num)//Thanks too jjMckee for help on this
{
int inner = 0;
int outer = 0;
String primeNum = "";
for(outer = 2; outer <= num; outer++)
{
boolean prime = true;
for(inner = outer / 2; inner >= 2; inner--)
{
if(outer % inner == 0)
{
prime = false;
}
}
if(prime == true)
{
primeNum = primeNum + outer + " ";
}
}
System.out.println("Prime not including 2: " + primeNum);
}
private static void sum(int num)
{
int digit;
int sum = 0;
int divisor = 10000000;
while(divisor > 0)
{
digit = num / divisor;
sum = sum + digit;
num = num % divisor;
divisor = divisor / 10;
}
System.out.println("The sum of the digit's is " + sum);
}
}
Revise this Paste