plz help me..its urgent

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pixcee2000
    New Member
    • Nov 2006
    • 7

    plz help me..its urgent

    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
  • eddiepo
    New Member
    • Nov 2006
    • 2

    #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

    Code:
    import java.io.*;
    import java.util.*;
    
    public class PIXCEE2000 {
    
        public static void main(String[] args) {
            try {
                //we input an integer x,where x>0
                int i = readInt();
                if(i <= 0) {
                    System.out.println("Number specified must be greater than zero.");
                    return;   
                }
                ArrayList list = sumOfConsecutiveNumbersEqualTo(i);
                System.out.println("Sum of Consecutive numbers that equates to " + i + " is " + 
                    (list.isEmpty() ? "[No Answer]" : list));
            } catch(Exception e) {
                e.printStackTrace();   
            }           
        }
        
        public static ArrayList sumOfConsecutiveNumbersEqualTo(int number) {
            int sum = 0;
            boolean mustBreak = false;
            ArrayList numlist = new ArrayList();
            for(int a=1; a<number; a++) {
                for(int i=a; i<number; i++) {                
                    sum = sum + i;                
                    if(sum > number) {
                        sum = 0;
                        numlist.clear();
                        break;
                    } else {
                        numlist.add(new Integer(i));
                        if(sum == number) {
                            mustBreak = true;
                            break;
                        }
                    }
                }
                if(mustBreak) {
                    break;   
                }
            }
            if(numlist.size() == 1) { //a size of 1 can never be consecutive numbers
                numlist.clear();
            }
            return numlist;        
        }
            
        public static int readInt()  throws Exception {
            int ret = 0;
            BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter a Number:");
            String s = buff.readLine();       
            ret = Integer.parseInt(s); 
            // you can do some error handling here[e.g. invalid input entry etc]
            return ret;
        }
    }

    Comment

    • Frugoo Scape
      New Member
      • Oct 2006
      • 34

      #3
      samething =)

      Comment

      Working...