Java Programming Write a program that will read in a number from the user betwee
ID: 3621524 • Letter: J
Question
Java Programming
Write a program that will read in a number from the user between 0 to 99 and spell out that number. The program must also report any values that are out of range.
2. There once was a wise servant who saved the life of a princess. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.
There are 64 squares on a chessboard.
Write a program that shows how many grains were on each square and the total number of grains.
In other words, I want to type
java Grains
and see
square 1: 1 grain
square 2: 2 grains
square 3: 4 grains
square 4: 8 grains
Explanation / Answer
please rate - thanks
in the future CRAMSTER rule 1 question to a post
import java.math.BigInteger;
public class Grains
{public static void main(String [] args)
{int i;
BigInteger j = new BigInteger ("1");
BigInteger BigInteger ("1");
BigInteger total = new BigInteger ("0");
for(i=1;i<=64;i++)
{System.out.print("square "+i+": "+j+" grain");
if(j!=one)
System.out.print("s");
System.out.println();
j=j.add(j);
total=total.add(j);
}
System.out.println("Total grains used = "+total);
}
}
------------------------------------------------
import java.util.*;
public class numbertowords
{public static void main(String [] args)
{int n,units,tens;
Scanner in=new Scanner(System.in);
System.out.print("Enter a number between 0 and 99(-999 to exit): ");
n=in.nextInt();
while(n!=-999)
{
while(n<0||n>99)
{System.out.print("Please enter a number between 0 and 99 ");
System.out.print("Enter a number between 0 and 99: ");
n=in.nextInt();
}
System.out.print("You entered the number ");
units=n%10;
tens=n/10;
switch(tens)
{ case 0:n=100;
break;
case 1: switch(units)
{
case 9:System.out.print("nineteen");
break;
case 8:System.out.print("eighteen");
break;
case 7:System.out.print("seventeen");
break;
case 6: System.out.print("sixteen");
break;
case 5: System.out.print("fifteen");
break;
case 4: System.out.print("fourteen");
break;
case 3: System.out.print("thirteen");
break;
case 2: System.out.print("twelve");
break;
case 1: System.out.print("eleven");
break;
case 0: System.out.print("ten");
break;
}
break;
case 2: System.out.print("twenty ");
break;
case 3: System.out.print("thirty ");
break;
case 4: System.out.print("forty ");
break;
case 5: System.out.print("fifty ");
break;
case 6: System.out.print("sixty ");
break;
case 7: System.out.print("seventy ");
break;
case 8: System.out.print("eighty ");
break;
case 9: System.out.print("ninty ");
break;
}
if(n>20)
{switch(units)
{case 9: System.out.print("nine");
break;
case 8: System.out.print("eight");
break;
case 7: System.out.print("seven");
break;
case 6: System.out.print("six");
break;
case 5: System.out.print("five");
break;
case 4: System.out.print("four");
break;
case 3: System.out.print("three");
break;
case 2: System.out.print("two");
break;
case 1: System.out.print("one");
break;
case 0: if(n==100)
System.out.print("zero");
break;
}
}
System.out.print(" Enter a number between 0 and 99(-999 to exit): ");
n=in.nextInt();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.