Feb 3, 2018 at 4:50 pm

Reading Individual digits of a Number in Java

In this post, I will show two ways of reading individual digits of a number in the Java language. The first method is using String, and it is the easier one. First, we change the input number to string in the following way just as we used previously to find the first digit of a number String strInput = String.valueOf(input); After that, we read each character using the charAt() method. Using a for loop, you can read individual characters like this for(int i = 0; i < strInput.length(); i++){ System.out.print(strNum.charAt(i)); } The second method doesn’t change the input to String. […]

Feb 2, 2018 at 1:46 pm

Java program to find Sum of Digits in a Number

The following program takes an integer number as an input and returns the sum of the individual digits. The number can be positive or negative. For instance, if the user enters 456, the program returns 15 which is the sum of 4 + 5 + 6. Additionally, the program makes sure the user enters only a valid integer (int) number. The program is listed below: /* This program reads individual digits of a given number and returns their sum */ import java.util.*; public class AddDigits { public static void addDigits(int number){ int digit = 0; int sum = 0; while […]

Feb 1, 2018 at 4:30 pm

Java Program to Find Perfect Number

The following Java program displays the available perfect numbers within a defined range of numbers. I use the Java for-loop to iterate between the minimum and maximum numbers to be checked. If a positive integer is equal to the sum of its divisors, we call it a perfect number, assuming the divisors don’t include the number itself. For instance, the factors of 6 are 1, 2 and 3. If we add the factors, that is 1 + 2 + 3, we get 6. Since this number is the same as the original number . It is a perfect number. As […]

Jan 20, 2018 at 12:59 pm

How to Check if a Word is a Palindrome in Java

In this post, I will show how to check if a word or sentence is palindrome in Java. If a word or sentence reads the same backwards or forward, we call it palindrome. For instance, if we reverse the word ‘madam’ we get the same word – ‘madam’. Words such as racecar, rotator, tattarrattat, which is also the longest palindrome in the Oxford English Dictionary according to Wikipedia, are palindrome words. Using a Java program, we can check if a word is a palindrome. We can at least use two methods to reverse a particular word or string in Java. […]

Jan 18, 2018 at 11:26 am

Simple Java Program to Find the First and Last Digit of a number

Here is a simple Java program to find the first and the last digit of a given number. Please note if you want the user to supply the number, you need to check for its validity. You also have to make sure the first character is not a minus sign. Otherwise, if the user supplies a negative number, the following line in the program will return the minus (-) sign instead of a number, which is what you expect. String.valueOf(number).charAt(0)   here is the code public class FirstLastDigit { public static void main(String[] args) { int number = 979870678; char […]

Jan 17, 2018 at 11:03 am

Simple Java program to find Factorial of a number

The Java code below shows how to get the factorial of numbers. For instance 4! = 4 x 3 x 2 x 1 = 24. Since Zero factorial is defined to be one, i.e. 0! = 1, the loop starts from 1. The code below calculates the factorial of numbers from 1 to 10. public class DisplayFactorial{ public static void main(String[] args){ int j = 1; System.out.println(“0! = ” + 1); for(int i=1; i<10; i++){ j = j *i; System.out.println(i + “! = ” + j); } } }   When we run the program we will get output similar […]

Jan 13, 2018 at 1:25 pm

Display number patterns using java

Here is a list of number patterns that you can produce using any programming language. I use Java in this post. I hope you will find these simple programs useful for programming challenges or assignments you might encounter. You can check also other pattern by clicking here. Here is the first pattern including the full code. I didn’t include the Scanner class for user input because I wanted to limit the number of code lines to the minimum. As you can see in the code, the ‘j’ in the inner loop is printed from 1 up to the upper limit […]

Jan 9, 2018 at 11:56 am

How to Create a Checkerboard pattern Using Asterisks in Java

Here is a simple program to draw a checkerboard pattern in Java. There can be a different way of doing it. In this case, the program pads every other row with a space to have a zigzag effect. To do that the program checks if the row number is even and if it is, it prints spaces at the beginning of the row. It uses the modulus operator (%) and the if/else code. The if/else section should be placed in the outer loop to limit the space to only the beginning of the row.   Here is the Java code: […]

Jan 8, 2018 at 8:38 pm

How to Create a Hollow Rectangle Using Asterisks in Java

A hollow rectangle such as the box shown below can be drawn by printing the star symbol on only the first and the last row, and the first and the last column. We can fill the rest of the spaces, i.e. from row two up to the (last row – 1) and from column two up to the (last column – 1) with spaces to produce the hollow rectangle.   Given the variable ‘size’ as the size of the row and the column: we can write the logic as follows: for (int i=1; i<=size; i++){ for(int j = 1; j<=size; […]

Nov 11, 2017 at 8:39 pm

Math Worksheets – Multiplication of 2 digits by 2 digits

We have free and printable math worksheets for your kids. All of the worksheets are pdf files to make printing easier. This page includes multiplication of two digits by two digits worksheets. Click any one of the link below to download or print the files : One Two Three Four A sample of the worksheet can be seen below:    

1 2