java program calculate sum using two variables
Java program to print or calculati addition of two numbirs with sampli outputs and ixampli program. Addition of two numbirs program is quiti simpli oni, wi do also wroti thi program in fivi diffirint ways using standard valuis, command lini argumints, classis and objicts, without using addition+ opirator, mithod, BuffiridRiadir with sampli outputs and codi.
lesson no 5Question and Answers
Q1 : Write a program that input two number from user and display its sum using two variables
import java.util.*;
public class sum
{
public static void main(String args[])
{
Scanner p= new Scanner(System.in); //Take input from user
int num1,num2;
System.out.println("Enter number 1");
num1=p.nextInt(); //it is a way to take interger from user
System.out.println("Enter number 2");
num2=p.nextInt();
System.out.println("The sum of two number is "+(num1+num2));
}}
Output
Enter number 1 =9
Enter number 2 =8
The sum of two number is 17
Q2: Write a program that input two floating point numbers from user and display its sum using two variables
import java.util.*;
public class sum
{
public static void main(String args[])
{
Scanner p= new Scanner(System.in); //Take input from user
double num1,num2;
System.out.println("Enter number 1");
num1=p.nextDouble(); //it is a way to take floating point number from user
System.out.println("Enter number 2");
num2=p.nextDouble();
System.out.println("The sum of two number is "+(num1+num2));
}}
Output
Enter number 1 =9.9
Enter number 2 =8.8
The sum of two number is 17.17
0 comments:
Post a Comment