By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,736 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,736 IT Pros & Developers. It's quick & easy.

plz help me..its urgent

P: 7
pls help me to write a program such that we input an integer x,where x>0. For x, the program has to convert it into the sum of consecutive positive integers. for e.g. let x=10 output should be "10= 1+2+3+4" the total no. of consecutive positive integers in the sum expression should be maximal. for e.g. if x= 9 result should be 2+3+4 and not 4+5. If x=4 , the output should be "no answer"

thanx in advance
Nov 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: 2
pixcee,

here's something i came up with. try and check it out if the algorithm/code is ok. as always, there might be other and more efficient ways to solve the problem...

cheers...

eddie

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class PIXCEE2000 {
  6.  
  7.     public static void main(String[] args) {
  8.         try {
  9.             //we input an integer x,where x>0
  10.             int i = readInt();
  11.             if(i <= 0) {
  12.                 System.out.println("Number specified must be greater than zero.");
  13.                 return;   
  14.             }
  15.             ArrayList list = sumOfConsecutiveNumbersEqualTo(i);
  16.             System.out.println("Sum of Consecutive numbers that equates to " + i + " is " + 
  17.                 (list.isEmpty() ? "[No Answer]" : list));
  18.         } catch(Exception e) {
  19.             e.printStackTrace();   
  20.         }           
  21.     }
  22.  
  23.     public static ArrayList sumOfConsecutiveNumbersEqualTo(int number) {
  24.         int sum = 0;
  25.         boolean mustBreak = false;
  26.         ArrayList numlist = new ArrayList();
  27.         for(int a=1; a<number; a++) {
  28.             for(int i=a; i<number; i++) {                
  29.                 sum = sum + i;                
  30.                 if(sum > number) {
  31.                     sum = 0;
  32.                     numlist.clear();
  33.                     break;
  34.                 } else {
  35.                     numlist.add(new Integer(i));
  36.                     if(sum == number) {
  37.                         mustBreak = true;
  38.                         break;
  39.                     }
  40.                 }
  41.             }
  42.             if(mustBreak) {
  43.                 break;   
  44.             }
  45.         }
  46.         if(numlist.size() == 1) { //a size of 1 can never be consecutive numbers
  47.             numlist.clear();
  48.         }
  49.         return numlist;        
  50.     }
  51.  
  52.     public static int readInt()  throws Exception {
  53.         int ret = 0;
  54.         BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
  55.         System.out.print("Enter a Number:");
  56.         String s = buff.readLine();       
  57.         ret = Integer.parseInt(s); 
  58.         // you can do some error handling here[e.g. invalid input entry etc]
  59.         return ret;
  60.     }
  61. }
  62.  
  63.  
Nov 25 '06 #2

P: 34
samething =)
Nov 25 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.