Search This Blog

UI/UX Design | Web Development | Mobile Application Development

Whether you need UI/ UX design, web development services, or mobile app development services, Qaabil Digitals will be more than happy to help you. We work with top software developers, designers, and project managers. We tailor our services to suit your exact needs. We closely look at your company, listen to your story, and bring about the best ideas and solutions that can help you drive solid results. We don’t just produce work that works. We produce work that matters. Get a quote now Or Talk to us

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

Share