0% found this document useful (0 votes)
439 views5 pages

Java Program To Add Two Numbers

This document discusses two Java programs to calculate the sum of two numbers. The first program hardcodes the numbers, while the second program takes numbers as input from the user. It also explains how to calculate the area of a circle in Java using a static method, importing the Scanner class to take user input, and applying the formula for area of a circle which represents Pi as 22/7. The document provides line-by-line explanation of the code to demonstrate how it works.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
439 views5 pages

Java Program To Add Two Numbers

This document discusses two Java programs to calculate the sum of two numbers. The first program hardcodes the numbers, while the second program takes numbers as input from the user. It also explains how to calculate the area of a circle in Java using a static method, importing the Scanner class to take user input, and applying the formula for area of a circle which represents Pi as 22/7. The document provides line-by-line explanation of the code to demonstrate how it works.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Java Program to Add two Numbers

Here we will see two programs to add two numbers, In the first program we specify the value of
both the numbers in the program itself. The second programs takes both the numbers (entered by
user) and prints the sum.

First Example: Sum of two numbers


public class AddTwoNumbers {

public static void main(String[] args) {

int num1 = 5, num2 = 15, sum;


sum = num1 + num2;

System.out.println("Sum of these numbers: "+sum);


}
}

Output:

Sum of these numbers: 20

Second Example: Sum of two numbers using Scanner


The scanner allows us to capture the user input so that we can get the values of both the numbers
from user. The program then calculates the sum and displays it.

import java.util.Scanner;
public class AddTwoNumbers2 {

public static void main(String[] args) {

int num1, num2, sum;


Scanner sc = new Scanner(System.in);
System.out.println("Enter First Number: ");
num1 = sc.nextInt();

System.out.println("Enter Second Number: ");


num2 = sc.nextInt();

sc.close();
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

Output:
Enter First Number:
121
Enter Second Number:
19
Sum of these numbers: 140
Print Area Of Circle 5 Different Ways With Examples
[wp_ad_camp_3]

Below is the program to calculate the AOC in java, if you were new to java coding then we can
also share the complete step by steps with detailed explanation on how this java program works,
just below this code check it out.

1. Static Method
Static Method Program
Java
1 import java.util.Scanner;
2 class AreaOfCircle
3 {
4    public static void main(String args[])
5     {  
6       
7       Scanner s= new Scanner(System.in);
8         
9          System.out.println("Enter the radius:");
10          double r= s.nextDouble();
11          double  area=(22*r*r)/7 ;
12          System.out.println("Area of Circle is: " + area);      
13    }
14 }
Output:
1 Enter the radius :
27
3 Area of circle : 154.0
 

As we know that formula in math is : 

But, if you were written like that, then the system won’t understand, until and unless you assign
the value of “PIE” is 22/7.  As usual , you always represent the values in binary numbers.

The system can’t able to understand whether the given number is a number, or given letter is a
letter.  All it understood as ” Character ” or just a “ Symbol “. Here goes the complete step by
step explanation of the above code and how it works.
Step – 1:

1 import java.util.Scanner;

( here ‘import’ is a keyword in java used to get features from inbuilt packages. here we using a
package called until it consists of many classes and we using one of the class Scanner to get
command over console which is the interface between user and program. )

Step – 2:

1 public static void main(String args[])

( The main function, where the execution of  program start  from here onwards )

Step – 3:

1 Scanner s= new Scanner(System.in);

( 1. The scanner is a class used to scan the input data which was given by the user through a
console.

so to get access on a console we want to create an object  Syntax:new Scanner(); after creating an
object that reference will store in variable ‘s’ )

Step – 4:

1 System.out.println("Enter the radius:");

( above code is giving instructions for the user to  give the input  for radius)

Step – 5:

1 double r= s.nextDouble();

 ( here above instruction is get input in the required format.  first we want to know about
nextDouble() method it takes a token(collection of symbols divide by white space
example:”ABC”,  “DEF” , “GHI”,”10″,”20″)

which is given by  user.when user give 10 as input actually in user perspective  it is number but
any number or string  which entered on  console by default  those are  strings,  1o as  string but
we want  10 as  number format  for that we have method to convert string to number(Int, Double,
Float, Long…..)  some of those method are:

1.nextDouble() ,

2 nextFloat(),
3.nextInt(),   

4.nextLong()

5.nextShort()

6.next() or nextString() ).

Step – 6:

1 double  area=(22*r*r)/7 ;

Step – 7: System.out.println(“Area of Circle is: ” + area);   ( Once, you entered the radius , the
value stored in a particular function ( nextDouble(); ) and read those values with the help of a
scanner and display the output for a given value.

A : The major difference is where double can represent the output even after the decimal point ,
whereas in ” Int ” only the numbers before decimal point will take into consideration.

You might also like