Sum of Inputs – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #JAVA#557 siteicon   siteicon   siteicon  

Sum of Inputs

This is a program to learn different ways of getting the desired console output

Learning Objectives

To learn java concepts related to :

  • understanding simple calculations
  • introduction to variables
  • different ways of passing argument to println()method.

Program Approach

The objective is to demonstrate single argument passing, doing arithmetic calculations while passing the argument and concatenation of two different data types in print or println methods

We will be firstly calculating the sum and storing it in a variable.Then we will be passing that variable as an argument in the println() method.In the next phase we will be doind addition inside the println() method only.Then we will be using the + sign to concatenate two different data types and displaying them on the screen

Source Code

TC++ #557

Source Code

Run Output

30
30
Sum of Inputs :30

Code Understanding

In the above program the variables a, b and c have been used to store integer values.
System.out.println(c);
In this statement we have passed an integer value as an argument in the println() method. println() is a method where we are allowed to pass different kinds of data types such as String, int, float, double, char, boolean etc. Such methods are also known as overloaded methods.
System.out.println(a+b);
In this statement we have calculated the sum of the values stored in the variables a and b and we have passed it directly as an argument in the println() method.
System.out.println(“Sum of Inputs :”+(a+b));
In this statement we have calculated the sum of the values stored in the variables a and b. Then we have passed it directly as an argument in the println() method and we have also concatenated the sum which will be an integer type to a message which will be string type. The expression a+b must be enclosed within round brackets. This allows integer summation before concatenation with the message.


Suggested Filename(s): SumOfInputs



Share

admin| Created: 16-Aug-2016 | Updated: 8-Dec-2017|






Back