Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Wednesday, 19 October 2011

Java program which calculates next number by adding all previous numbers using exception handling (by Adler Lawn Service, LLC)

import java.util.*;

class EverySum
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b=0;
System.out.println ("\n\t The number must be 50 or less");
System.out.print ("\n\t Enter the nth number = ");
a=in.nextInt();
try
{
if (a>50)
check.test();
else
{
for (int i=1;i<a;++i)
{
b+=i;
System.out.print ("\t "+b);
}
}

}
catch (IndexOutOfBoundsException e)
{
System.out.println ("\n\t Invalid number: "+e);
}


}
}

class check
{
static void test() throws IndexOutOfBoundsException
{
throw new IndexOutOfBoundsException (">50");
}
}

No comments:

Post a Comment