How to input binteger value and add with each up to zero in java


/*In the programming contest sometimes we need to input value upto zero,,If zero then break otherwise perform a operation. That time we can work with this code.........
*/
/// Here "Bigsum" is the class name..........

import java.math.BigInteger;
import java.util.Scanner;

public class Bigsum {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
        BigInteger n;
        BigInteger sum=BigInteger.ZERO;
        while(true){
            n=sc.nextBigInteger();
           
            if(n.compareTo(BigInteger.valueOf(0))==0){
                break;
            }
            else{
                sum=sum.add(n);
            }
        }
        System.out.println(sum);

    }

}

/*--------------------------------------Input---------------------
563473653574356475
3657465847587958765985
3474574985747838753875634756475
37567345648756878565
3765837583475485749846
35873985904589485484
0
*/
/*--------------------------------------Output-----------------

 3474574993245146990145999992830

*/

No comments:

Post a Comment