Problem 5(using [Link].
Scanner class)
Source code
import [Link];
public class ScannerAdd {
public static void main(String args [])
{ Scanner keyboard= new Scanner([Link]);
[Link]("Enter two integers: ");
int a=[Link]();
int b=[Link]();
int c=a+b;
[Link]("Sum = "+ c);
}
}
Output
Enter two integers:
5
3
Sum = 8
Program 2(taking input using BufferedReader and
[Link] class combination)
Source Code
import [Link].*;
public class BufferedReader{
public static void main(String[] args) throws IOException{
InputStreamReader in = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(in);
[Link]("Enter two decimal values:");
String s1 = [Link]();
String s2 = [Link]();
Double d1 = [Link](s1);
Double d2 = [Link](s2);
[Link]("First: "+d1+"\nSecond: "+d2);
[Link]("Sum: "+(d1+d2));
[Link]("Sum: %.2f",(d1+d2));
}
}----OUTPUT
Enter two integers:
3
4
Sum = 7
Program 4(Java addition using Basic Input)
Source Code
public class SimpleInput {
public static void main(String[] args) throws Exception{
char ch;// char variable
String str="";//empty string
[Link]("Enter a decimal number(press '@' to
quit):");
while((ch=(char)[Link]()) !='@') {
str+=ch;
}
double num=[Link](str);
[Link]("Number = "+ num);
}
}
Output
Enter a decimal number(press '@' to quit):
123.56@
Number = 123.56
Program 1(Using CommandLineArgument)
Source Code
public class CommandLineArgument {
public static void main(String ar[]){
int a,b,c;
[Link]("Enter two integers:");
a=[Link](ar[0]);
b=[Link](ar[1]);
c=a+b;
[Link]("sum is " +c);
}
}
}
Output
Enter two integers:
4
6
10
Program 3(using Console Class)
Source Code
import [Link];
public class ConsoleTest {
public static void main(String args[]){
Console cs= [Link]();
[Link]("Enter two decimal numbers:");
double a=[Link]([Link]());
double b=[Link]([Link]());
double c=a+b;
[Link]("Sum = "+ c);
}
}
Output
Enter two decimal numbers:
12.35
23.45
SUM= 35.8