|
|
|
Program number 2, ooooooooohhhhh.
I bet your just sooooooooo impressed. This program, FunNumbers, takes in a value from the user,
breaks up the digits and adds them together. HAVE FUN!
//Name of Program File: FunNumbers.java
//Author: Travis McGrath
//INFO140 Section # 04 Date: Sept 24, 2001
//Your Company Name: Funamators
//Description: This program will take a number between 1 and 1000, split up
// the digits, and then add them all together. :0)
public class FunNumbers
{
public static void main(String[]args)
{
//explain progran to user
System.out.println("This program will take a number between 1 and 1000" +
" from you, split up the digits, and then add them all together. :0)");
//get number between 1 and 1000 from user
System.out.print("Enter a number between 1 and 1000, for example 567: ");
int yourNumber = MyInput.readInt();
//while loop to make sure the number isn't higher then 1000 or lower then 1
while ((yourNumber < 1) || (yourNumber > 1000))
{
System.out.print("Please try again and enter a number between 1 and " +
"1000: ");
yourNumber = MyInput.readInt();
}//end of while
//find number of thousands
int numberOfThousands = yourNumber/1000;
int remainder = yourNumber%1000;
//find number of Hundreds
int numberOfHundreds = remainder/100;
remainder = remainder%100;
//find number of tens
int numberOftens = remainder/10;
remainder = remainder%10;
//find number of ones
int numberOfOnes = remainder;
//calculate the sum of digits
int sumOfDigits = numberOfThousands + numberOfHundreds + numberOftens +
numberOfOnes;
//display the results
System.out.println("The sum of all the digits in " + yourNumber +
" is " + sumOfDigits);
}//end main
}//end class
|