This is the question
This is the code I worked on:
import java.util.Scanner;
public class FlooringTiles {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i, N, length, width, tiles, extra, total;
N = sc.nextInt();
if (N < 1 || N > 100) {
System.out.print("Invalid number of labs. Please try again (1-100)");
} else
for (i = 1; i <= N; i++) {
width = sc.nextInt();
if (width < 1 || width > 10000) {
System.out.print("Invalid width. Please try again (1-10000)");
}
length = sc.nextInt();
if (length < 1 || length > 10000) {
System.out.print("Invalid length. Please try again (1-10000)");
}
tiles = (length * width) / (30 * 30);
extra = ((length * width) % (30 * 30)) / (30 * 30);
total = tiles + extra;
System.out.println("Case #" + i + ": " + total);
}
}
}
I can't seem to get the same output as the sample given in the picture when I use the same sample input. Can anyone help me with this? Thanks a bunch!
Your problem is that you use the area of a tile in your calculation to calculate the "extra" tiles.
You have to calculate the number of rows you need and the number of cols you need separately.
So image you have a length of 301, so you divide this by 30 and you get 10,0333, by using Math.ceil we can round up to 11.
Like this:
int rowsNeeded = (int) Math.ceil(length / 30);
int colsNeeded = (int) Math.ceil(width / 30);
int tiles = rowsNeeded * colsNeeded;
Of course you could also use modulo to calculate the number of rows you need.
Like if length%30 != 0 add 1 to length/30
Related
So I am doing a digit counter thing basically
I want it to display 123 and which number is what place value for example 123
------------------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
------------------------------------
this is my code
import java.util.Scanner;
public class digits {
public static void main(String[] args)
{
Scanner scann = new Scanner(System.in);
System.out.print("Enter any number: ");
int number = scann.nextInt();
int num1 = number % 10;
int num2 = number / 10 % 10;
int num3 = number / 100 % 10;
int num4 = number / 1000 % 10;
int num5 = number / 10000 % 10;
int num6 = number / 100000 % 10;
int num7 = number / 1000000 % 10;
int num8 = number / 10000000 % 10;
scann.close();
System.out.println("Ones: "+num1);
System.out.println("Tens: "+num2);
System.out.println("Hundreds: "+num3);
System.out.println("Thousands: "+num4);
System.out.println("Ten-Thousands: "+num5);
System.out.println("Hundred-Thousands: "+num6);
System.out.println("Millions: "+num7);
System.out.println("Ten-Millions: "+num8);
}
}
How do I stop it from printing the rest if I only type 123?
Output I got
--------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
Thousands: 0
Ten-Thousands: 0
Hundred-Thousands: 0
Millions: 0
Ten-Millions: 0
------------------------
Output I want
--------------------------
Enter any number: 123
Ones: 3
Tens: 2
Hundreds: 1
------------------------
You need to introduce a condition (or conditions) in your code.
You can achieve that with a chain of if statements. But the better way to do it is by utilizing a loop. Because that will allow you to get rid of the intermediate variables (num1, num2, etc) and to avoid duplicating the line of code that prints the remainder on the consol. That will make the code more readable and concise.
In order to be able to apply the loop for this problem, you need to create an array of strings that will store all quantifiers ("Ones: ", "Tens: ", etc).
It can be done like that:
public static final String[] quantifiers =
{"Ones: ", "Tens: ", "Hundreds: ", "Thousands: ",
"Ten-Thousands: ", "Hundred-Thousands: ", "Millions: ", "Ten-Millions: "};
public static void main(String[] args) {
Scanner scann = new Scanner(System.in);
int number = scann.nextInt();
for (int i = 0; i < quantifiers.length && number > 0; i++) {
System.out.println(quantifiers[i] + number % 10);
number /= 10; // does the same as number = number / 10;
}
}
output for input 123
Ones: 3
Tens: 2
Hundreds: 1
It might help your question. Because of hardcoded terms such as 'tens' or 'hundreds', it is not generic enough.
public static void main(String[] args) {
Scanner scann = new Scanner(System.in);
System.out.print("Enter any number: ");
int number = scann.nextInt();
int length = String.valueOf(number).length();
for(int i=0; i< length; i++){
if(i == 0){
System.out.println("Ones: "+ number % 10);
}else if(i == 1)
System.out.println("Tens: " + number / 10 % 10);
else if(i == 2)
System.out.println("Hundreds: "+ number / 100 % 10);
else if(i == 3)
System.out.println("Thousands: "+ number / 1000 % 10);
else if(i == 4)
System.out.println("Ten-Thousands: "+ number / 10000 % 10);
else if(i == 5)
System.out.println("Hundred-Thousands: "+ number / 100000 % 10);
else if(i == 6)
System.out.println("Millions: "+ number / 1000000 % 10);
else if(i == 7)
System.out.println("Ten-Millions: " + number / 10000000 % 10);
}
scann.close();
}
Your code is printing all the numbers because that's what you wrote:
System.out.println("Ones: "+num1);
...and so on.
If you never want to print e.g. thousands, just remove the println for thousands. If you only want to print them if someone actually enters thousands, add an if statement:
if (num4 > 0) {
System.out.println("Thousands: "+num4);
}
Repeat for the others.
I have been given min and max number,
e.g: min = 10 and max = 1000 now i want to split this number into equal parts and in result i should get 10 numbers.
like: 100, 200, 300, 400, 500, 600, 700 , 800 , 900, 1000
this we can do by dividing max with number we want e.g: 1000/10 and add 100 every time.
but, there is one scenario in which we have been given :
min = 14 and max = 1113 in this case if we divide 1113/10 then we will get 111
and the range will come like 0 , 113, 226, 339 ...
but, i don't want to show them like this . It would be better if i can display 110 or 115 or 120 even numbers (round off) instead of 113.
Can anyone help me.
In your example, when min=14 and max=1113 you divide 1113/10 to get 111. If you want to round 111 down to 110, you can perform 111 - (111 % 10) to get 110. Then simply iterate 10 times and multiply the beginning number by the current iteration.
There is a similar post to this here: How to round *down* integers in Java?
Depending on what numbers you want to display (either 110 or 115 or 120) you can approach this in a similar way.
That is an algorithm for scaling a diagram; distributing ticks on an axis.
However 10 ticks is then an over-estimate.
private int[] ticks(int min, int max) {
int gap = (max - min) / 10;
// Round gap up to a nice value - best calculatory:
if (gap < 1) {
gap = 1;
} else if (gap < 5) {
gap = 5;
} else if (gap < 10) {
gap = 10;
} else if (gap < 50) {
gap = 50;
} else if (gap < 100) {
gap = 100;
} else if (gap < 250) {
gap = 250;
...
}
int minTick = (min/gap) * gap;
if (minTick > min) { // For negative numbers.
minTick -= gap;
}
int maxTick = (max/gap) * gap;
if (maxTick > min) { // Negative, and ceiling.
maxTick += gap;
}
int ngap = (maxTick - minTick) / gap;
int[] ticks = new int[ngaps + 1];
for (int i = 0; i <= ngaps; ++i) {
ticks[i] = minTick + i * gap;
}
return ticks;
}
import java.util.Scanner;
public class NeumannsRandomGenerator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter cases:");
int cases = in.nextInt();
int iterations = 0;
for (int i = 0; i <= cases; i++) {
int a = in.nextInt();
int res = ((a * a) / 100) % 10000;
if(res == a){
iterations++;
}
do {
int b = ((res * res) / 100) % 10000;
iterations++;
b = res;
} while (a != res);
System.out.println(iterations);
}
}
}
I am trying to figure Neumans random generator
For Example:
5761 - let it be the first number
5761 * 5761 = 33189121 - raised to power 2
33(1891)21 => 1891 - truncate to get the middle
1891 - it is the second number in the sequence
1891 * 1891 = 3575881 - raised to power 2 (add leading zero to get 8 digits)
03(5758)81 => 5758 - truncate to get the middle
5758 - it is the third number in the sequence (and so on...)
Please help why I am not getting any results:(
I don't know the rules for an iteration where your number is padded like 0315. If the padded value would be 00099225, and the resulting number is 992, then this would be a good start. I also don't see your rule for re-generating, so it just recurses.
I'm doing this quickly, so there could be some unnecessary code here
public int getNeumansRandomNumber(int starting) {
int nsquared = (int) Math.pow(starting, 2);
int length = String.valueOf(nsquared).length();
if (length < 8) {
String zeroPad = "00000000";
String padded = zeroPad.substring(length) + nsquared;
System.out.println("padded="+padded);
nsquared = Integer.valueOf(padded);
}
int middle = (nsquared % 1000000) / 100;
System.out.println(middle);
return getNeumansRandomNumber(middle);
}
References: 1, 2, and 3
I'm working on a school assignment that checks whether a credit card number that is entered is valid or not, using Luhn's Algorithm.
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustration, consider the card number 4388 5760 1840 2626):
Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
Now add all single-digit numbers from Step 1: 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
Add all digits in the odd places from right to left in the card number: 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
Sum the results from Step 2 and Step 3: 37 + 38 = 75
If the result from Step 4 is divisible by 10 the card number is valid; otherwise, it is invalid. For example, the number 4388 5760 1840 2626 is invalid, but the number 4388 5760 1841 0707 is valid.
I need to write this program using the methods in the code I have written:
public class CreditCardValidation {
public static void main(String[] args, long input) {
Scanner numberinput = new Scanner(System.in);
System.out.println("Enter a credit card number as a long integer: ");
long cardnumber = numberinput.nextLong();
if (isValid(input) == true) {
System.out.println(numberinput + " is valid.");
} else {
System.out.println(numberinput + " is invalid.");
}
}
public static boolean isValid(long number){
int total = sumOfDoubleEvenPlace + sumOfOddPlace;
return (total % 10 == 0) && (prefixMatched(number, 1) == true) &&
(getSize(number)>=13) && (getSize(number)<=16);
}
public static int sumOfDoubleEvenPlace(long number) {
int doubledevensum = 0;
long place = 0;
while (number > 0) {
place = number % 100;
doubledevensum += getDigit((int) (place / 10) * 2);
number = number / 100;
}
return doubledevensum;
}
public static int sumOfOddPlace(long number) {
int oddsum = 0;
while (number <= 9) {
oddsum += (int)(number % 10);
number = number % 100;
}
return oddsum;
}
public static int getDigit(int number) {
if (number <= 9) {
return number;
} else {
int firstDigit = number % 10;
int secondDigit = (int)(number / 10);
return firstDigit + secondDigit;
}
}
public static boolean prefixMatched(long number, int d) {
if ((getPrefix(number, d) == 4)
|| (getPrefix(number, d) == 5)
|| (getPrefix(number, d) == 3)) {
if (getPrefix(number, d) == 3) {
System.out.println("\nVisa Card ");
} else if (getPrefix(number, d) == 5) {
System.out.println("\nMaster Card ");
} else if (getPrefix(number, d) == 3) {
System.out.println("\nAmerican Express Card ");
}
return true;
} else {
return false;
}
}
public static int getSize(long d) {
int count = 0;
while (d > 0) {
d = d / 10;
count++;
}
return count;
}
public static long getPrefix(long number, int k) {
if (getSize(number) < k) {
return number;
} else {
int size = (int)getSize(number);
for (int i = 0; i < (size - k); i++) {
number = number / 10;
}
return number;
}
}
}
I just started learning how to program two months ago so I am fairly new to this. The program doesn't compile and I don't know why and what I have to do to fix this. I know there are similar topics already posted regarding this and I have been using this post to help guide me a bit. Can someone help point a student in the right direction and let me know what I'm doing wrong?
Your program isn't compiling because this line:
int total = sumOfDoubleEvenPlace + sumOfOddPlace;
since sumOfDoubleEvenPlace and sumOfOddPlace are functions, you must use them as such:
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
In the function isValid you are trying to add two variables which do not exist. However you have defined them as functions and to use them as functions you must call them as functions using
int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
I am trying to write a simple and quite useless program to generate a list of all integers 1><1000 where the sum of digits is 11. Every time I run this, I end up in an infinite loop. I've tried different things - for(){}, while(){}, adding a if(count>500){break;} to halt it after the loop counter reaches 500....still nothing...where am I going wrong in this?
Thanks in advance
//loops through all numbers whose sum of digits is 11
for(int number = 29; number < 1000; number++) {
//checks the values of the 100,10,and 1 position
int hPlace = number / 100; number = number - (hPlace * 100);
int tPlace = number / 10; number = number - (tPlace * 10);
int oPlace = number;
//sum of digits
int i = hPlace + tPlace + oPlace;
//prints if sum of digits is 11
int count = 0;
if (i == 11) {
count++;
System.out.print(i + " ");
}
//new line after every 10 numbers -- just for formatting
if (count % 10 == 0) {
System.out.println("");
}
}
You are using same variable as controller for your fors. Try to change the controller variable within the for structure from number to number1
You are changing the variable here:
---------------------------------
int hPlace = number / 100; number = number - (hPlace * 100);
---------------------------------
Don't do this
number = number - (hPlace * 100);
when your condition is dependent on number
for(int number = 29; number < 1000; number++)
because you have two nested for loops which both of them use the same variable as counter
for(int number = 29; number < 1000; number++) {
for(number = 29;number < 930;number++) {
//loops through all numbers whose sum of digits is 11
for(int number = 29; number < 1000; number++) {
//checks the values of the 100,10,and 1 position
int hPlace = number / 100;
**number** = number - (hPlace * 100); // PROBLEM!!!
int tPlace = number / 10;
**number** = number - (tPlace * 10); // PROBLEM!!!
int oPlace = number;
//sum of digits
int i = hPlace + tPlace + oPlace;
//prints if sum of digits is 11
int count = 0;
if (i == 11) {
count++;
System.out.print(i + " ");
}
//new line after every 10 numbers -- just for formatting
if (count % 10 == 0) {
System.out.println("");
}
}
if(count>500){break;} to halt it after the loop counter reaches 500....still nothing
This won't work because you're redeclaring count with an initial value of 0 everytime. So the if will always return false.
Also, these following lines:
int hPlace = number / 100; number = number - (hPlace * 100);
int tPlace = number / 10; number = number - (tPlace * 10);
Modify number, which is your loop variable. Your loop will not perform correctly if you modify the loop variable in unexpected ways. Instead, copy the value over to another variable.
Don't change the value of you loop control variable inside the loop, or dangerous things may result. Instead, copy the value into a new variable and use that in the loop.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SumDigits
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a Number:");
String string=br.readLine();
System.out.println("length of Number:"+string.length());
int sum=0;
int number=0;
for(int i=0;i<=string.length()-1;++i)
{
char character=string.charAt(i);
number=Character.getNumericValue(character);
sum=sum+number;
}//for
System.out.println("Sum of digits of Entered Number:"+sum);
}//main()
}//class