Multiplication Table in Java

This is a very basic program But for a beginner it is worth trying.

And it also makes us aware about the power of for loop which can repeat so many times within a second.

public class MulTable
{
	public static void main(String[] args)
	{
		int i;
		int num = 9;
		for(i=1; i<=10; i++)
		{
			System.out.println(num+ " * " +i+ " = "+num * i);
		}
	}
}

Snapshot of the o/p

Java Program finding the sum of the digits of a given number

//Java program finding the sum of the digits of a number

 

SumOfDigitsofaNumberDemo.java


public class SumOfDigitsofaNumberDemo
{
 public static void main(String[] args)
 {

	 int originalNumber=2345;
	 int rem=0;
	 int number = originalNumber;
	 int sum = 0;
	//Find sum of the digits of the number

	while(number > 0){
		rem = number % 10;
		number = number / 10;
		sum = sum + rem;
	}

	System.out.println("Sum of the Digits of the "+originalNumber+ " is "+sum);
	}
}

o/p

 Sum of the Digits of 2345 is 14.

 Dry Run

/*

* rem = 2345 % 10 = 5 rem stores remainder

* number = 2345 / 10 = 234 number stores quotient the remaining number

*

*  rem   number  sum=sum+rem

*   5      234      0+5= 5

*   4      23       5+4= 9

*   3      2        9+3= 12

*   2      0        12+2 = 14

*

* */


Java program checking whether a String is Palindrome or not

//Java program checking whether a String is Palindrome or not

PalindromeStringDemo.java

public class PalindromeStringDemo
{
 public static void main(String[] args)
 {

  //String to check
  String strToTest="malayalam";

  int i;
  int n=strToTest.length();

  String reversedString="";
  for(i=n-1;i>=0;i--)
  reversedString=reversedString + strToTest.charAt(i);

  if(strToTest.equals(reversedString))
  System.out.println(strToTest+ " is a palindrome");
  else
  System.out.println(strToTest+ " is not a palindrome");
 }
}