Saturday, January 17, 2015

Getting input several integer and double type data for addition and multiplication

The java program given below will perform two mathematical operations addition and multiplication. The operations will be performed using four integer type data and four double type data.

When you will run the program then you will be asked to provide four integer type data one by one and soon you will get the summation & product of those four numbers.

Similarly you will be asked to provide four double type data and later you will get the result.
java programming,input double type data,several mathematical operations

import java.util.Scanner;
 
public class AddMulin{
 public static void main(String [] args){
  //declaring integer type variables
  int n1=0, n2=0, n3=0, n4=0, r1=0, r2=0;
 
  //getting input from keyboard
  Scanner in = new Scanner(System.in);
  System.out.print("First Integer Number=");
  n1 = in.nextInt();
 
  System.out.print("Second Integer Number=");
  n2 = in.nextInt();
 
  System.out.print("Third Integer Number=");
  n3 = in.nextInt();
 
  System.out.print("Fourth Integer Number=");
  n4 = in.nextInt();
 
  //performing mathematical operation addition
  r1=n1+n2+n3+n4;
  //showing the result
  System.out.println("\nSum of 4 integer numbers is:"+r1);
  //performing mathematical operation multiplication
  r2=n1*n2*n3*n4;
  //showing the result
  System.out.println("Product of 4 integer numbers is:"+r2);
 
  //declaring double type variables
  double num1=0, num2=0, num3=0, num4=0, r3=0, r4=0;
  
  //getting input from keyboard
  System.out.print("\n\nFirst Double Number=");
  num1 = in.nextDouble();
 
  System.out.print("Second Double Number=");
  num2 = in.nextDouble();
 
  System.out.print("Third Double Number=");
  num3 = in.nextDouble();
 
  System.out.print("Fourth Double Number=");
  num4 = in.nextDouble();
 
  //performing mathematical operation addition
  r3=num1+num2+num3+num4;
  //showing the result stored in variable r3
  System.out.println("\nSum of 4 double numbers is:"+r3);
  //performing mathematical operation multiplication
  r4=num1*num2*num3*num4;
  //showing the result stored in variable r4
  System.out.println("Product of 4 double numbers is:"+r4);
 }
}


No comments:

Post a Comment