guys! can you help me convert this code to java language....
this is my code in c++....i need to convert in java....

# include <iostream.h>
int num1,num2,sum;
double add(double sum);
int main()
{
cout<<"Enter first number";
cin>>num1;
cout<<"Enter second number";
cin>>num2;
sum=add(sum);
cout<<"The sum is: "<< sum;
return 0;
}
double add(double sum)
{
return (sum=num1+num2);
}
}

Recommended Answers

All 4 Replies

What java code do you have so far?
Do you have any specific questions about how to write java code
Or are you looking to hire a programmer to write some java code for you?

Look at the System.out.println() method for writing to the console
Look at the Scanner class for reading from the console.

Search here or google for code examples

guys! can you help me convert this code to java language....
this is my code in c++....i need to convert in java....

# include <iostream.h>
int num1,num2,sum;
double add(double sum);
int main()
{
cout<<"Enter first number";
cin>>num1;
cout<<"Enter second number";
cin>>num2;
sum=add(sum);
cout<<"The sum is: "<< sum;
return 0;
}
double add(double sum)
{
return (sum=num1+num2);
}
}

Buddy, You are not passing num1 and num2 to the function sum.

you are doing sum=add(sum)

Surely there is a typo.

you must be trying sum=add(num1,num2)

Isn't it?

import java.util.Scanner; //Same as "include" statement
                          //Lets you use a Scanner object to get input.
public class Add2 //Creates a new program named "Add2"
{ //This is what is inside the program.
    public static void main(String[] args) //Where the code starts (same as "int main()")
    { //Begins the "main" method
        Scanner input = new Scanner(System.in); //Creates a new Scanner for input.
        int num1,num2,sum; //Creates three variables

        System.out.print("Enter first integer: "); //outputs quoted text
        num1 = input.nextInt(); //scanner reads inputted integer and stores it in num1

        System.out.print("Enter second integer: "); //outputs quoted text
        num2 = input.nextInt(); //scanner reads inputted integer and stores it in num2

        sum=add(num1, num2); //Calls method "add", passes in num1 and num2, and
                             //stores the result in sum.
        
        System.out.println("The sum is: "+sum); //outputs the quoted text + value of sum
    } //Ends the "main" method

    public static int add(int num1, int num2) //Method "add" takes in 2 integers and returns an integer.
    { //Begins the "add" method
        return num1+num2; //Returns the sum of the 2 numbers
    } //Ends the "add" method
} //Ends the Add2 program

Hopefully you don't just copy-paste this code but really try to understand the syntax. Good luck!!!

thanks kvass...don't worry i will....tnx a lot..God Bless

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.