วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Functions (Medium)


Exercise 1: Write Java program to allow the user to input two integer values and then the program prints the results of adding, subtracting, multiplying, and dividing among the two values.

See the example below:
Enter value a:30
Enter value b:10
The result of adding is 40.
The result of subtracting is 20;
The result of multiplying is 300.
The result of dividing is 3.
Solution:

void setup()
{
  caculateValues();
}

void caculateValues() {

  int a = 30, b = 10;
  int resulta = 40, results = 20, resultm = 300;
  float resultd = 3;
  print("Enter a:");
  print("Enter b:");
  resulta=a+b;
  results=a-b;
  resultm=a*b;
  resultd=(float)a/b;
  println("The result of adding is "+resulta);
  println("The result of subtracting is "+results);
  println("The result of multiplying is "+resultm);
  println("The result of dividing is "+resultd);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น