Login or Register

  Welcome: Guest


Home >> Article >> Programming >> Java >> Post New Article Bookmark and Share

Addition of 2 numbers in java program

May 28, 2013  by: Joseph  Points: 12   Category: Java  Earning $0.15   Views: 407

For starting with Java we need to know 2 things first is how to print a message and how to accept 2 numbers & print there sum. This article is about how to create a program which accept 2 numbers and print there sum.

         

For beginners who want to learn a particular computer language he/she first of all focus on learning two basic programs tutorial first is printing a message commonly "Hello World" is the message. Secondly Addition of 2 numbers. In this post I would like to explain about the second program- Addition of 2 numbers. Below is a program which accept two numbers and print there sum.

Import Java.util.scanner;
public class add
{
public static void main(String[]args)
{
int a,b,c;
System.out.PrintIn("Enter 2 numbers:");
Scanner s= new scanner(System.in)
a=s.nextInt();
b=s.nextInt();
c=a+b;
System.out.PrintIn(" The sum is: " +c);
}
}


In the above you can see the java codes for accepting 2 numbers and printing there sum. Now I would like to explain each line briefly. First of all the program is started with header file and main function so I am only explaining about the actual program codes.
{ int a,b,c; -: For storing integers java need variables 'int' is a data type which used to represent integers and a,b,c are the variable which used to store integers.

System.out.PrintIn("Enter 2 numbers :"); -: This is an output operator which used to print the messages and variables. Whatever the details that tells between the " " it will display on the screen. Here it is used to ask the user to enter 2 numbers.

Scanner s= new scanner(system.in)
a=s.nextInt();
b=s.nextInt();

-: This is the input operator. Most of the beginners who started with C then to Java this will be a confusing thing because in C the input operation is done with single word 'cin>>' but here it is different s is a representative of the two variables in order to take 2 numbers in one statement it is used to give s.nextInt();

c=a+b; -: I don't thing so I don't want to explain this. Here it is telling the value of c.

System.out.PrintIn("The sum is: " +c);
}
}

-: As I already tell the use of system.out.PrintIn. We know that 'c' is a variable in here +c means we have stored the value of a+b in c so inorder to print the sum of 2 numbers we use +c.

The program to find the subtraction, multiplication, quotient of two numbers is also done with this program by only changing the symbols in c=a(+\-\*\/)b. Java is very easy to learn and it is user friendly language. I hope that you like this article. All the best.




Comment(s)

No response found. Be the first to respond to this post

Post Comment

Please Log In  to post comment

Related Resources



About Java
Java is a purpose object oriented programming language

Enterprise Java Beans : An Overview
This article contains basic information about Enterprise Java Beans (EJBs)Technology .

Application Deployment Tool in java
Java Based Tools for testing and deployment tool

Threads in Java
Here is the most commonly used concept i.e. threads in Java.

Tips for learning java to face an interview related to C and C++
Tips,learning java,java interview,with c

Latest Resources From The Same Category



Use of switch in java programming
Switch is a conditional statement which used to pick a statement if the condition and expression are equal. This article explains about the use and functioning switch along with example.

Addition of 2 numbers in java program
For starting with Java we need to know 2 things first is how to print a message and how to accept 2 numbers & print there sum. This article is about how to create a program which accept 2 numbers and print there sum.

Starting Java Programming
Java is computer language. It help the user to develop softwares,programs and even games also. It is easy to study this computer language. Sometimes you may see certain title says java games, java apps when you try to download mobile apps from mobile store. That means this app is made with java language. This article is about how to write and run a java program.

Usage of Java Programming and Concepts
Describes the salient features of Java and explains about the usage of java.

Threads in Java
Here is the most commonly used concept i.e. threads in Java.

 

Top Performers     Top Contributors     Share and Earn     Terms of Service    Privacy Policy     Contact Us