This page contains a sample solution to one of the exercises from Introduction to Programming Using Java. Exercise 2.2:Write a program that simulates rolling a pair of dice. You can simulate rolling one die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number you pick represents the number on the die after it is rolled. As pointed out in Section 2.5, the expression (int)(Math.random()*6) + 1does the computation to select a random integer between 1 and 6. You can assign this value to a variable to represent one of the dice that are being rolled. Do this twice and add the results together to get the total roll. Your program should report the number showing on each die as well as the total roll. For example: The first die comes up 3 The second die comes up 5 Your total roll is 8Discussion When designing a program, one of the first things you should ask yourself is, "What values do I need to represent?" The answer helps you decide what variables to declare in the program. This program will need some variables to represent the numbers showing on each die and the total of the two dice. Since these numbers are all integers, we can use three variables of type int. I'll call the variables die1, die2, and roll. The program begins by declaring the variables: int die1; int die2; int roll;In the actual program, of course, I've added a comment to explain the purpose of each variable. The values of die1 and die2 can be computed using the expression given in the exercise: die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1;Note that even though the expressions on the right-hand sides of these assignment statements are the same, the values can be different because the function Math.random() can return different values when it is called twice. We can then compute roll = die1 + die2 and use three System.out.println statements to display the three lines of output: System.out.println("The first die comes up " + die1); System.out.println("The second die comes up " + die2); System.out.println("Your total roll is " + roll);Note that I've chosen to use the concatenation operator, +, to append the value of die1 onto the string "The first die comes up". Alternatively, I could use two output statements: System.out.print("The first die comes up "); System.out.println(die1);Yet another possibility is to use System.out.printf: System.out.printf("The first die comes up %d%n", die1);I'll also note that I could get away without the variable roll, since I could output the value of the expression die1 + die2 directly: System.out.println("Your total roll is " + (die1 + die2));However, it's generally better style to have a meaningful name for a quantity. By the way, the parentheses around (die1 + die2) are essential because of the precedence rules for the + operator. You might try to experiment with leaving them out and see what happens. As a final variation, all three lines of output could be produced using a multiline text block. Text blocks were discussed in Subsection 2.3.4. Using a text block also requires System.out.printf: System.out.printf(""" The first die comes up %d The second die comes up %d Your total roll is %d """, die1, die2, roll);The Solution public class RollTheDice { /* This program simulates rolling a pair of dice. The number that comes up on each die is output, followed by the total of the two dice. */ public static void main(String[] args) { int die1; // The number on the first die. int die2; // The number on the second die. int roll; // The total roll (sum of the two dice). die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1; roll = die1 + die2; System.out.println("The first die comes up " + die1); System.out.println("The second die comes up " + die2); System.out.println("Your total roll is " + roll); } // end main() } // end classpublic class Main { public static void main(String[] args) { int count = 0, num = 0003452; while (num != 0) { // num = num/10 num /= 10; ++count; } System.out.println("Number of digits: " + count); } } Output Number of digits: 4In this program, while the loop is iterated until the test expression num != 0 is evaluated to 0 (false).
Note: The program ignores any zero's present before the number. Hence, for digits like 000333, the output will be 3. Example 2: Count Number of Digits in an Integer using for looppublic class Main { public static void main(String[] args) { int count = 0, num = 123456; for (; num != 0; num /= 10, ++count) { } System.out.println("Number of digits: " + count); } }Output Number of digits: 6In this program, instead of using a while loop, we use a for loop without any body. On each iteration, the value of num is divided by 10 and count is incremented by 1. The for loop exits when num != 0 is false, i.e. num = 0. Since, for loop doesn't have a body, you can change it to a single statement in Java as such: for(; num != 0; num/=10, ++count);
Given a number N and a digit D, the task is to count the occurrences of D in N. Approach: Take out the digits one by one in N and check if this digit is equal to D. If equal, then increment the count by 1. In the end, print the count.Below is the implementation of the above approach:
Article Tags :
Count the number of 2s as digit in all numbers from 0 to n. Examples : Input : 22 Output : 6 Explanation: Total 2s that appear as digit from 0 to 22 are (2, 12, 20, 21, 22); Input : 100 Output : 20 Explanation: total 2's comes between 0 to 100 are (2, 12, 20, 21, 22..29, 32, 42, 52, 62, 72, 82, 92);A Simple Brute force solution is to iterate through all numbers from 0 to n. For every number being visited, count the number of 2’s in it. Finally return total count. Below is implementation of the idea.
Below is an alternate implementation of this approach.
Improved Solution We know that roughly one tenth of the time, the last digit will be a 2 since it happens once in any sequence of ten numbers. In fact, any digit is a 2 roughly one tenth of the time. We say “roughly” because there are (very common) boundary conditions. For example, between 1 and 100, the 10’s digit is a 2 exactly 1/10th of the time. However, between 1 and 37, the 10’s digit is a 2 much more than 1/10th of the time. Case digits < 2 In other words, we can round down to the nearest 10d+1, and then divide by 10, to compute the number of 2s in the d-th digit. Case digit > 2 Case digit = 2 Now, all we need is to iterate through each digit in the number. Implementing this code is reasonably straightforward. Below is the implementation of the idea.
Time Complexity:O(logN) This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Article Tags : |