+ 1

Write a program in java to calculate and display sum of three numbers??

how to write coding

4th Nov 2016, 1:54 PM
aja
1 Answer
+ 2
It depends. There is many ways to do that. All example I will show are to put in the main function, except the last. The simpliest way to calculate the sum of three numbers is: System.out.print(3 + 5 + 9); So, it is really easy. Other way is to put sum in variable. int sum = 3 + 5 + 9; And use System.out.print(sum). You can also create three separates variables: int a = 3, b = 5, c = 9; int sum = a + b + c; System.out.print(sum); An other way, to calculate how many numbers you want, is to create a function, under or over the main, it is an algorithm. public static int sum(int... args) { int s = 0; for(int arg : args) { s += arg; } return s; } With this, you can do simply: System.out.print(sum(3, 5, 9, -10, 6)); for example in the main function. Hope I could help you :)
4th Nov 2016, 9:05 PM
Volts
Volts - avatar