qbr
qbr

Reputation: 197

A simple java program to add 2 integers

I have a problem statement:

Write a program to calculate the sum of 2 numbers and print the output.

Input

Line 1: An integer.

Line 2: An integer.

Output :The output consists of a single integer which corresponds to sum, followed by a new line

Sample Input I

3 
1 

Sample Output I

4 

Sample Input II

13 
10

Sample Output II

23

To which my solution is

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Add {

public static void main(String[] args)throws IOException
{
    int a=0, b=0, sum;
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter the numbers to be summed");
    try{
        a=sc.nextInt();
        sc.nextLine();
        b=sc.nextInt();
    }
    catch(InputMismatchException e){
        System.out.println("Please enter an Integer number");
        e.printStackTrace();}
    catch(Exception e){System.out.println(e);}
    
    sum=a+b;
    
    System.out.println(sum);
    sc.close();
}

}

I'm supposed to submit it to an online directory, that I assume tries to execute the program automatically. And when I do, it tells me

 Wrong Answer   Almost there,think some more

I think pondering over it for an hour is more than enough before you decide to call in for reinforcement.

Upvotes: 2

Views: 11456

Answers (5)

Shyamlal Yadav
Shyamlal Yadav

Reputation: 1

package stack;
public class Satck {
    public static int MAX=100;
     int top;
    int [] a= new int [MAX];
  boolean  empty()
    {
        return (top<0);
    }
 Satck()
    {
        top=-1;
    }
 void push(int x)
 {
         a[++top]=x;
     
 }
 public int pop()
 {
    
     int x=a[top--];
         return x;
    
 }
 public int peek()
 {
         int x=a[top];
         return x;
 }
 public static void main(String[] args) {
     
     Satck s=new Satck();
     s.push(10);
     s.push(11);
     s.push(12);
     s.push(13);
     System.out.println(s.peek());
     System.out.println(s.empty());
     System.out.println(s.pop());
     System.out.println(s.peek());
}
}

Upvotes: 0

user2591957
user2591957

Reputation:

Try this:

import java.util.Scanner;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num1 = 0;
        int num2 = 0;
        int sum = 0;

        System.out.println("Enter Number: ");
        num1 = in.nextInt();

        System.out.println("Enter Number2: ");
        num2 = in.nextInt();

        sum = num1 + num2;
        System.out.println(sum);
    }
}

Upvotes: 0

Girish Acharya
Girish Acharya

Reputation: 115

These can be solve by two thing command line arguments or Scanner class or BufferReader.

Using the Command line Arguments.

public Class Sum
   {

     public static void main(String [] args) 
     {
          int a ,b,c;
          a=Integer.parseInt(args[0]);   //using Integer wrapper Class to cast object  
                                           to primitive Datatype Integer.

          b= Integer.parseInt(args[1])  ;

          c= a+b;


         System.out.println("The Sum of two number is : "+c);
     }

   }

Using Command Line Arguments with code re usability(Method Sum)

public Class Sum
   {

     public static long sum(int a,int b)
     {
          return a+b;
     }

     public static void main(String [] args) 
     {
          int a ,b;
          long c;           // for long summation of numbers .

          a=Integer.parseInt(args[0]);   //using Integer wrapper Class to cast object 
                                           to primitive Datatype Integer.

          b= Integer.parseInt(args[1])  ;

          c= sum(a,b);


         System.out.println("The Sum of two number is : "+c);
     }

   } 

Using the External resources from the java.util.Scanner

    public Class Sum
   {

     public static void main(String [] args) 
     {
          int a ,b;
          long c;

          Scanner scan;

          scan = new Scanner(System.in) ;  //Taking system Keyboard for input.

          System.out.println("Enter the value of A: \n");

          a= ss.nextInt() ;

          System.out.println("Enter the value of B: \n");

          b=ss.nextInt();

          c= (long) (a+b); 

         System.out.println("The Sum of two number is : "+c);
     }

   }

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692131

The output should be "a single integer which corresponds to sum, followed by a new line".

But the output of your program is

Enter the numbers to be summed
<the sum>

Upvotes: 10

Jsdodgers
Jsdodgers

Reputation: 5312

remove sc.nextLine(). It makes it move to the next line, but since both integers are on the same line, the value for b remains at 0.

Upvotes: 2

Related Questions