February 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. […]

February 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 […]

February 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 […]

January 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 […]

January 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 […]

January 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! = ” + […]

January 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 […]

January 9, 2018 at 11:56 am

How to Create a Checkerboard pattern Using Asterisks in Java

Checkerboard Pattern 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 […]

January 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 […]

September 17, 2017 at 6:18 pm

Java Program to show Maximum and Minimum

This Java program prints the maximum and the minimum number of a given numerical digit. For example – typical output would be Please enter a number 42308 and theoutput is –  minimum digit is 0 and the maximum digit is 8   /** Method that shows the minimum and maximum digit *of any given number **/ import java.util.*; public class MaxMin { static void maxMin(int number){ int digit = 0; […]