Hi

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:

Exm:

Input a= 5
Input b=5
ans=10

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

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();
            ob_m.add();
        }

        public static void main(String args[])
        {
            math ob_main=new math();
            ob_main.add();
        }
    }

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 3 Years Ago by paku_dnj: Sub change

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 article has been dead for over six months. Start a new discussion instead.