I want to take input of two variable as integer and than do addition with the two variables.
The ans will stored in another variable. The program will repeat after every addition end and will ask for user input of varibles and will do addition again.

My qus is taht how can i add all aditions ans again:


Input a= 5
Input b=5

Agin program will ask for
Input a= 6
Input b= 6

now how can i take all " ans " value with program and do additions of all "ans"

Final Ans=10+12=22

my code:

import java.io.*;
import java.util.Scanner;

public class math{

        public void add()
            Scanner keyboard = new Scanner(System.in);

            int a;
            int b;

            System.out.print("\nEnter a : ");
            a = keyboard.nextInt();

            System.out.print("Enter b : ");
            b = keyboard.nextInt();

            int c=a+b;

            System.out.println("\nans is  :"+c);

            math ob_m=new math();

        public static void main(String args[])
            math ob_main=new math();

The code just do addition one after another but i want that it will do one more task that ....

It all add all aditions reasuts also. how can i do it?

Edited by paku_dnj: Sub change

5 Years
Discussion Span
Last Post by JamesCherrill

Your code repeats because you have a recursive call on lines 23/24 That's a strage way to do this, and will cause out-of-memory errors if you keep entering nunbers. Normally you would use a while loop for this.
In that case you can declare and initialise a variable (eg int grandTotal = 0;) before the loop, and each time you calulate a new ans you add that to grandTotal

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.