Sample Programs Using Scanner Class
Sample Programs Using Scanner Class
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// Prints name
System.out.println("My name is " + name);
Output
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// creating a Scanner object
Scanner input = new Scanner(System.in);
input.close();
}
}
Output
Enter an integer:
22
Using nextInt(): 22
import java.util.Scanner;
class Main {
public static void main(String[] args) {
input.close();
}
}
Output
Enter an integer:
22
Using nextInt(): 22
1. class FibonacciExample1{
2. public static void main(String args[])
3. {
4. int n1=0,n2=1,n3,i,count=10;
5. System.out.print(n1+" "+n2);//printing 0 and 1
6.
7. for(i=2;i<count;++i)//loop starts from 2 because 0 and 1 are already printed
8. {
9. n3=n1+n2;
10. System.out.print(" "+n3);
11. n1=n2;
12. n2=n3;
13. }
14.
15. }}
Output:
0 1 1 2 3 5 8 13 21 34
3 int a = Integer.parseInt(args[0]);
4 int b = Integer.parseInt(args[1]);
5 int sum = a + b;
Output:
Fibonacci Series program using command-line arguments
1
class Example3{
2
public static void main(String[] args){
3
int n = Integer.parseInt(args[0]);
4 int t1 = 0;
5 int t2 = 1;
6
7 for(int i = 1; i <=n; i++){
8 System.out.println(t1);
t1 = t2;
10
t2 = sum;
11
}
12
}
13
}
14
Output: