Computer Science, asked by aspriyadhi27, 1 year ago

How to create a java program to add two numbers by getting the value from the user?

Answers

Answered by satyasharma52
0
Java program to add two numbers: Given below is the code of a Java program that adds two numbers which are entered by a user.



//import java.util.Scanner;public class Inputfunctions {  public static void main(String[] args) {   Scanner readme = new Scanner(System.in);   System.out.println("Enter Two Numbers (Press Enter after each):");   //two variables to hold numbers   double n1, n2, n3;   n1 = readme.nextDouble();   n2 = readme.nextDouble();   n3 = n1 + 2;   System.out.println("Total = " + n3); 
Answered by atrs7391
0

Your program:

package com.company;

import java.util.Scanner;

class test

{

   public static void main(String[] args)

   {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter first number: ");

       double n1 = sc.nextDouble();

       System.out.println("Enter second number: ");

       double n2 = sc.nextDouble();

       long n3 = (long)n1+(long)n2;

       System.out.println("The sum of the numbers: "+n3);

   }

}

Similar questions