JAVA Practical

SIMEPLE IF...ELSE , SWITCH ETC... 

 1.) Print Hello World!

class HW {

public static void main(String args[]){

System.out.println("Hello World!");

}

}

OUTPUT :-

Hello World!

---------------------------------------------------------------------------------------------------------------------------------------

2.) Print the value of the variable.

 class valueOfVariable {

public static void main(String args[]){

int a=10;

float f = 1.5f;

System.out.println(a);

System.out.println(f);

}

}

OUTPUT :-

10

1.5

---------------------------------------------------------------------------------------------------------------------------------------

3.) Print addition/sum, Subtraction, Multiplication, Division of two numbers.

class AO{

public static void main(String args[]){

int a=20, b=5;

int sum = a+b;

int sub = a-b;

int mul = a*b;

int div = a/b;

System.out.println("The addition is : " + sum);

System.out.println("The subtraction is : "+ sub);

System.out.println("The Multiplication is : "+mul);

System.out.println("The Division is : "+div);

}

}

OUTPUT :-

The addition is : 25

The subtraction is : 15

The Multiplication is : 100

The Division is : 4

---------------------------------------------------------------------------------------------------------------------------------------

4.) Print a number from 1 to 10 which is divisible by 2.

class AO{

public static void main(String args[]){

for(int i=1;i<=10;i++){

if(i%2==0){

System.out.print(i+" ");

}

}

}

}

OUTPUT :-

2 4 6 8 10

---------------------------------------------------------------------------------------------------------------------------------------

5.) Take two values and print maximum number.

class max{

public static void main(String args[]){

int i=10, j=20;

if(i>j){

System.out.println("The Maximum number is : "+i);

} else {

System.out.println("The Maximum number is : "+j);

}

}

}

OUTPUT :-

The Maximum number is : 20

---------------------------------------------------------------------------------------------------------------------------------------

6.) Enter employee basic salary and add the da which experience greater than 3 year.

class emp{

public static void main(String args[]){

int basicsalary = 10000, year = 5, da = 2000;

if(year > 3){

System.out.println("Salary is : "+ (basicsalary+da));

} else {

System.out.println("Salary is : "+basicsalary);

}

}

}

OUTPUT :-

Salary is : 12000

---------------------------------------------------------------------------------------------------------------------------------------

7.) Print given number is odd or even using ternary operator.

class ternary{

public static void main(String args[]){

int n=8;

String str = (n%2==0)?"Even":"ODD";

System.out.println("The Number is : "+str);

}

}

OUTPUT :-

The Number is : Even

---------------------------------------------------------------------------------------------------------------------------------------

8.) Print the given number is positive ,negative or zero using if else.

class ifelse{

public static void main(String args[]){

int n=8;

if(n>0){

System.out.println("Positive");

} else if(n<0){

System.out.println("Negative");

} else {
                        System.out.println("Zero");

                }

}

}

OUTPUT :- 

Positive

---------------------------------------------------------------------------------------------------------------------------------------

9.) Enter age and check you are eligible for driving or not or require parents permission.

class ifelse{

public static void main(String args[]){

int age=19;

if(age>18){

System.out.println("You are Eligible");

} else if(age<=18 && age>16){

System.out.println("Require parents permission");

} else {

System.out.println("You are not eligible");

}

}

}

OUTPUT :-

You are Eligible

---------------------------------------------------------------------------------------------------------------------------------------

10.) Switch case print day.

class Switch {

public static void main(String args[]){

                int i=1;

switch(i) {

case 1:

System.out.println("Monday");

break;

case 2 :

System.out.println("Tuesday");

break;

case 3:

System.out.println("Wednesday");

break;

case 4:

System.out.println("Thursday");

break;

case 5:

System.out.println("Friday");

break;

case 6:

System.out.println("Saturday");

break;

case 7:

System.out.println("Sunday");

break;

default:

System.out.println("Invalid day number");

}

}

}

OUTPUT :-

Monday

---------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------------------------

MORE IF...ELSE SWITCH PROGRAM FOR PRACTICE

LOOPS

1.) Print 0 to 4 using for loop.

class forloop {

public static void main(String args[]){

for(int i=0;i<=4;i++){

System.out.print(i+" ");

}

}

}

OUTPUT :- 

0 1 2 3 4

---------------------------------------------------------------------------------------------------------------------------------------

2.) Print 4 to 0 using for loop.

class forloop {

public static void main(String args[]){

for(int i=4;i>=0;i--){

System.out.print(i+" ");

}

}

}

OUTPUT :- 

4 3 2 1 0

---------------------------------------------------------------------------------------------------------------------------------------

3.) Patttern 

0 1 2 3 4

0 1 2 3 

0 1 2 

0 1 

0

class pattern {

public static void main(String args[]){

for(int i=4;i>=0;i--){

for(int j=0;j<=i;j++){

System.out.print(j+" ");

}

System.out.println();

}

}

}

OUTPUT :- 

0 1 2 3 4

0 1 2 3

0 1 2

0 1

0

---------------------------------------------------------------------------------------------------------------------------------------

4.) Pattern

1

1 2

1 2 3 

1 2 3 4 

1 2 3 4 5

class pattern {

public static void main(String args[]){

for(int i=1;i<=5;i++){

for(int j=1;j<=i;j++){

System.out.print(j+" ");

}

System.out.println();

}

}

}

OUTPUT :-

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

---------------------------------------------------------------------------------------------------------------------------------------

5.) Pattern

1

2 2

3 3 3 

4 4 4 4 

5 5 5 5 5

class pattern {

public static void main(String args[]){

for(int i=1;i<=5;i++){

for(int j=1;j<=i;j++){

System.out.print(i+" ");

}

System.out.println();

}

}

}

OUTPUT :-

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

---------------------------------------------------------------------------------------------------------------------------------------

6.) Pattern

1

1 2 

1 2 1 

1 2 1 2

1 2 1 2 1

class pattern {

public static void main(String args[]){

for(int i=1;i<=5;i++){

for(int j=1;j<=i;j++){

if(j%2==0){

System.out.print(2+" ");

} else {

System.out.print(1+" ");

}

}

System.out.println();

}

}

}

OUTPUT :- 

1

1 2

1 2 1

1 2 1 2

1 2 1 2 1

---------------------------------------------------------------------------------------------------------------------------------------

7.) Pattern 

a

a b

a b c

a b c d

class pattern {

public static void main(String args[]){

for(int i=97;i<=100;i++){

for(int j=97;j<=i;j++){

System.out.print((char)j+" ");

}

System.out.println();

}

}

}

OUTPUT :- 

a

a b

a b c

a b c d

---------------------------------------------------------------------------------------------------------------------------------------

8.) Pattern 

a b c d

a b c

a b

a

class pattern {

public static void main(String args[]){

for(int i=100;i>=97;i--){

for(int j=97;j<=i;j++){

System.out.print((char)j+" ");

}

System.out.println();

}

}

}

OUTPUT :-

a b c d

a b c

a b

a

---------------------------------------------------------------------------------------------------------------------------------------

9.) While loop print o to n.

class whileloop {

public static void main(String args[]) {

int i=0,n=10;

while(i<=n){

System.out.print(i+" ");

i++;

}

}

}

OUTPUT :-

0 1 2 3 4 5 6 7 8 9 10

---------------------------------------------------------------------------------------------------------------------------------------

10.) Nested While Loop

class nestedwhileloop {

public static void main(String args[]) {

int i=0, j=5;

while(i<=5){

while(i<=j){

System.out.print("\t"+j);

j--;

}

System.out.print("\n"+i);

i++;

}

}

}

OUTPUT :-

        5       4       3       2       1       0

0

1

2

3

4

5

---------------------------------------------------------------------------------------------------------------------------------------

11.) do...while loop

class dowhileloop {

public static void main(String args[]) {

int i=0;

do{

System.out.print(i+" ");

i++;

} while(i<=10);

}

}

OUTPUT :-

0 1 2 3 4 5 6 7 8 9 10

---------------------------------------------------------------------------------------------------------------------------------------

12.) Pattern using while loop

1

12

123

1234

12345

1234

123

12

1

class whileloop {

public static void main(String args[]) {

int i=1;

while(i<=5) {

int j=1;

while(j<=i) {

System.out.print(j+" ");

j++;

}

System.out.println();

i++;

}

int a=4;

while(a>=1){

int b=1;

while(b<=a) {

System.out.print(b+" ");

b++;

}

System.out.println();

a--;

}

}

}

OUTPUT :-

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

---------------------------------------------------------------------------------------------------------------------------------------

13.) Pattern using while loop

1

1 2

1 2 3

1 2 3 4 5

5 4 3 2

5 4 3

5 4

5

class whileloop {

public static void main(String args[]) {

int i=1;

while(i<=5) {

int j=1;

while(j<=i) {

System.out.print(j+" ");

j++;

}

System.out.println();

i++;

}

int a=2;

while(a<=5){

int b=5;

while(b>=a) {

System.out.print(b+" ");

b--;

}

System.out.println();

a++;

}

}

}

OUTPUT :-

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

5 4 3 2

5 4 3

5 4

5

---------------------------------------------------------------------------------------------------------------------------------------

14.) Pattern

1

1 2

2 3 4

4 5 6 7

7 8 9 10 11

public class pattern {

    public static void main(String[] args) {

        int k=1, l;

        for (int i = 1; i <= 5; i++) {

            for (l = k; l < k+i; l++) {

                System.out.print(l+" ");

            }

            k=l-1;

            System.out.println();

        }

    }

}

OUTPUT :-

1

1 2

2 3 4

4 5 6 7

7 8 9 10 11

---------------------------------------------------------------------------------------------------------------------------------------

15.) Pattern 

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

public class pattern {

    public static void main(String[] args) {

        for (int i = 5; i >= 1; i--) {

            for (int j = 1; j <= i; j++) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

16.) Pattern

1 0 1 0 1

0 1 0 1

1 0 1

0 1

1

public class pattern {

    public static void main(String[] args) {

        for (int i = 5; i >= 1; i--) {

            for (int j = i; j >= 1; j--) {

                if(j%2==0) {

                    System.out.print(0+" ");

                } else {

                    System.out.print(1+" ");

                }

            }

            System.out.println();

        }

    }

}

OUTPUT :-

1 0 1 0 1

0 1 0 1

1 0 1

0 1

1

---------------------------------------------------------------------------------------------------------------------------------------

17.) Pattern

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=1;i<=5;i++) {

            for(j=5;j>=i;j--) {

                System.out.print("  ");

            }

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

OUTPUT :-

              1

           2 1

        3 2 1

     4 3 2 1

  5 4 3 2 1

---------------------------------------------------------------------------------------------------------------------------------------

18.) Pattern

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

public class pattern {

    public static void main(String[] args) {

        int j;

        for (int i = 1; i <= 5; i++) {

            for(j=5;j>=i;j--){

                System.out.print(" ");

            }

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

OUTPUT :-

     1

    1 2

   1 2 3

  1 2 3 4

 1 2 3 4 5

---------------------------------------------------------------------------------------------------------------------------------------

19.) Pattern

       *

     * *

   * * *

 * * * *

* * * * *

public class pattern {

    public static void main(String[] args) {

        int j;

        for (int i = 1; i <= 5; i++) {

            for(j=5;j>=i;j--) {

                System.out.print(" ");

            }

            for(j=1;j<=i;j++) {

                System.out.print("*"+" ");

            } 

            System.out.println();

        }

    }   

}

OUTPUT :-
        *
      * *
     * * *
   * * * *
 * * * * *
---------------------------------------------------------------------------------------------------------------------------------------

20.) Pattern

            1

         1 2 1

      1 2 3 2 1

   1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

class pattern {

    public static void main(String[] args) {

        int j;

        for (int i =1; i <= 5; i++) {

            for(j=5;j>=i;j--) {

                System.out.print("  ");

            }

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            for(j=i-1;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

OUTPUT :-

              1

           1 2 1

        1 2 3 2 1

     1 2 3 4 3 2 1

  1 2 3 4 5 4 3 2 1

---------------------------------------------------------------------------------------------------------------------------------------

21.) Pattern

1 2 3 4 5 4 3 2 1

   2 3 4 5 4 3 2

      3 4 5 4 3

        4 5 4

          5

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=1;i<=5;i++){

            for(j=1;j<=i;j++) {

                System.out.print("  ");

            }

            for(j=i;j<=5;j++) {

                System.out.print(j+" ");

            }

            for(j=5-1;j>=i;j--) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }   

}


OUTPUT :-

  1 2 3 4 5 4 3 2 1

     2 3 4 5 4 3 2

       3 4 5 4 3

          4 5 4

             5

---------------------------------------------------------------------------------------------------------------------------------------

22.) Pattern

A B C D E D C B A

A B C D     D C B A

A B C            C B A

A B                    B A

A                           A

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=69;i>=65;i--){

            for(j=65;j<=i;j++) {

                System.out.print((char)j+" ");

            }

            for(j=69;j>=i;j--){

                System.out.print("    ");

            }

            System.out.print("\b\b\b\b\b\b");

            for(j=i;j>=65;j--) {

                System.out.print((char)j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

23.) Pattern

1 2 3 4 5 4 3 2 1

1 2 3 4    4 3 2 1

1 2 3          3 2 1

1 2               2 1

1                     1

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=5;i>=1;i--) {

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            for(j=5;j>=i;j--) {

                System.out.print("    ");

            }

            System.out.print("\b\b\b\b\b\b");

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

24.) Pattern

A                             A

A B                     B A

A B C             C B A

A B C D     D C B A

A B C D E D C B A

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=65;i<=69;i++){

            for(j=65;j<=i;j++) {

                System.out.print((char)j+" ");

            }

            for(j=69;j>=i;j--) {

                System.out.print("    ");

            }

            System.out.print("\b\b\b\b\b\b");

            for(j=i;j>=65;j--) {

                System.out.print((char)j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

25.) Pattern

            1

         1 2 1

      1 2 3 2 1

   1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

   1 2 3 4 3 2 1

      1 2 3 2 1

         1 2 1

            1

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=1;i<=5;i++) {

            for(j=5;j>i;j--) {

                System.out.print("  ");

            }

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            System.out.print("\b\b");

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.println("");

        }

        for(int i=4;i>=1;i--) {

            for(j=4;j>=i;j--) {

                System.out.print("  ");

            }

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }   

            System.out.print("\b\b");

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

26.) Pattern

            1

         2 1 2

      3 2 1 2 3

   4 3 2 1 2 3 4

5 4 3 2 1 2 3 4 5

   4 3 2 1 2 3 4

      3 2 1 2 3

         2 1 2

            1

public class pattern {

    public static void main(String[] args) {

        int j;

        for(int i=1;i<=5;i++) {

            for(j=5;j>i;j--) {

                System.out.print("  ");

            }

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.print("\b\b");

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

        for(int i=4;i>=1;i--) {

            for(j=5;j>i;j--) {

                System.out.print("  ");

            }

            for(j=i;j>=1;j--) {

                System.out.print(j+" ");

            }

            System.out.print("\b\b");

            for(j=1;j<=i;j++) {

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

27.) Pattern

                A

            A B A

        A B C B A

    A B C D C B A

A B C D E D C B A

    A B C D C B A

        A B C B A

            A B A

                A

public class pattern {

    public static void main(String[] args) {

        int j;

        for (int i = 65; i < 69; i++) {

            for(j=69;j>=i;j--){

                System.out.print("  ");

            }

            for(j=65;j<=i;j++){

                System.out.print((char)j+" ");

            }

            System.out.print("\b\b");

            for(j=i;j>=65;j--){

                System.out.print((char)j+" ");

            }

            System.out.println();

        }

        for(int i=69;i>=65;i--){

            for(j=69;j>=i;j--){

                System.out.print("  ");

            }

            for(j=65;j<=i;j++){

                System.out.print((char)j+" ");

            }

            System.out.print("\b\b");

            for(j=i;j>=65;j--){

                System.out.print((char)j+" ");

            }

            System.out.println();

        }

    }

}

---------------------------------------------------------------------------------------------------------------------------------------

28.) Pattern

1 2 3 4 5 4 3 2 1

    1 2 3 4 3 2 1

       1 2 3 2 1

          1 2 1

            1

         1 2 1

      1 2 3 2 1

   1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

public class pattern {

    public static void main(String[] args) {

        int j;

        for (int i = 5; i >= 1; i--) {

            for(j=5;j>=i;j--){

                System.out.print("  ");

            }

            for(j=1;j<=i;j++){

                System.out.print(j+" ");

            }

            System.out.print("\b\b");

            for(j=i;j>=1;j--){

                System.out.print(j+" ");

            }

            System.out.println();

        }

        for(int i=2;i<=5;i++){

            for(j=5;j>=i;j--){

                System.out.print("  ");

            }

            for(j=1;j<=i;j++){

                System.out.print(j+" ");

            }

            System.out.print("\b\b");

            for(j=i;j>=1;j--){

                System.out.print(j+" ");

            }

            System.out.println();

        }

    }  

}

---------------------------------------------------------------------------------------------------------------------------------------

29.) Pattern 
1
1 2
2 3 4
4 5 6 7
7 8 9 10 11
public class p99 {
    public static void main(String[] args) {
        int i, j, count=1;
        for (i = 1; i<= 5; i++) {
            for(j=count;j<count+i;j++){
                System.out.print(j+" ");
            }
            System.out.println();
            count = j-1;
        }
    }
}
---------------------------------------------------------------------------------------------------------------------------------------
30.) Print Table
class printTable{

public static void main(String args[]){

for(int i=1;i<=10;i++){

System.out.println(n + " * " + i+" = "+i*n);

}

}

}

OUTPUT :-

javac printTable.java // For Compile

java printTable 5 // For Run

5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50
---------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------
Series Printing Using Loop
1.) 1, 2, 3, 4, 5, 6, 7, 8, 9....…..…………………n
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
System.out.print(i+" ");
}
}
}
INPUT :-
javac series.java // for compile
java series 10 // for run
OUTPUT :-
1 2 3 4 5 6 7 8 9 10
---------------------------------------------------------------------------------------------------------------------------------------
2.) Write a program to print n to 1 numbers.
class series {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
for(int i=n;i>=1;i--){
System.out.print(i+" ");
}
}
}
Input n=10;
OUTPUT :-
10 9 8 7 6 5 4 3 2 1
---------------------------------------------------------------------------------------------------------------------------------------
3.) Write a Program to print 1 to n odd numbers
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i =1;i<=n;i++){
if(i%2!=0){
System.out.print(i+" ");
}
}
}
}
INPUT :-  n=10
OUTPUT :-
1 3 5 7 9
---------------------------------------------------------------------------------------------------------------------------------------
4.) Write a Program to print 1 to n even numbers
class series {
public static void main(String args[]){
int n=Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
if(i%2==0){
System.out.print(i+" ");
}
}
}
}
INPUT :- 10
OUTPUT :-
2 4 6 8 10
---------------------------------------------------------------------------------------------------------------------------------------
5.) Write a Program to print n to 1 odd numbers
class series {
public static void main(String args[]){
int n=Integer.parseInt(args[0]);
for(int i=n;i>=1;i--){
if(i%2!=0)
System.out.print(i+" ");
}
}
}
INPUT :- n=10
OUTPUT :-
9 7 5 3 1
---------------------------------------------------------------------------------------------------------------------------------------
6.) Write a Program to print n to 1 even numbers
class series {
public static void main(String args[]){
int n=Integer.parseInt(args[0]);
for(int i=n;i>=1;i--){
if(i%2==0)
System.out.print(i+" ");
}
}
}
INPUT :- n=10
OUTPUT :-
10 8 6 4 2
---------------------------------------------------------------------------------------------------------------------------------------
7.)   1+2+3+4+5+6+7+8+9.…………………………n
class series {
public static void main(String args[]){
int n= Integer.parseInt(args[0]);
int sum=0;
for(int i=1;i<=n;i++){
sum +=i;
System.out.print(i+" + ");
}
System.out.println("\nThe Sum is : "+sum);
}
}
INPUT :- n=10
OUTPUT :-
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +
The Sum is : 55
---------------------------------------------------------------------------------------------------------------------------------------
8.) 1, 4, 9, 16, 25, 36………………………………n
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
System.out.print(i*i +", ");
}
}
}
INPUT :- 10
OUTPUT :-
1, 4, 9, 16, 25, 36, 49, 64, 81, 100,
---------------------------------------------------------------------------------------------------------------------------------------
9.) 1+4+9+16+25+36....……………………………n
class series {
public static void main(String args[]){
int n=  Integer.parseInt(args[0]);
int sum=0;
for(int i=1;i<=n;i++){
sum+=(i*i);
System.out.print(i*i +" + ");
}
System.out.println("\nThe Sum is : "+sum);
}
}
INPUT :- n=10
OUTPUT :-
1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100 +
The Sum is : 385
---------------------------------------------------------------------------------------------------------------------------------------
10.) 1, 1, 2, 3, 5, 8, 13, 21.…………………………n
class series{
public static void main(String args[]){
int sum=0, j=0,k=1;
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
sum = k+j;
System.out.print(sum+" ");
k=j;
j=sum;
}
}
}
INPUT :- n=10
OUTPUT :-
1 1 2 3 5 8 13 21 34 55
---------------------------------------------------------------------------------------------------------------------------------------
11.) 1, 2, 4, 7, 11, 16…………………………………n
class series{
public static void main(String args[]){
int n= Integer.parseInt(args[0]);
int ans = 1;
for(int i=0;i<=n;i++){
ans +=i;
System.out.print(ans+" ");
}
}
}
INPUT :- n=10
OUTPUT :-
1 2 4 7 11 16 22 29 37 46 56
---------------------------------------------------------------------------------------------------------------------------------------
12.) Write a Program to Generate a Series like 2 4 8 16 32…2048
class series {
public static void main(String args[]){
int a=1;
for(int i=1;i<=11;i++){
a*=2;
System.out.print((a)+" ");
}
}
}
OUTPUT :-
2 4 8 16 32 64 128 256 512 1024 2048
---------------------------------------------------------------------------------------------------------------------------------------
13.) Write a Program to Generate a Series like 1 11 20 28 35 41 46 50 53 55 56
class series {
public static void main(String args[]){
int sum=1;
System.out.print(sum+" ");
for(int i=10;i>=1;i--){
sum+=i;
System.out.print(sum +" ");
}
}
}
OUTPUT :-
1 11 20 28 35 41 46 50 53 55 56
---------------------------------------------------------------------------------------------------------------------------------------
14.) Write a Program to Generate a Series like 1 10 2 9 3 8 4 7 5 6
class series{
public static void main(String args[]){
for(int i=0;i<5;i++){
System.out.print(i+1+" ");
System.out.print(10-i+" ");
}
}    
}
OUTPUT :-
1 10 2 9 3 8 4 7 5 6
---------------------------------------------------------------------------------------------------------------------------------------
15.) Write a program to generate series like 2 5 10 17 26………..n (Square + 1 Series)
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
System.out.print(((i*i)+1)+" ");
}
}
}
INPUT :- java series 10
OUTPUT :-
2 5 10 17 26 37 50 65 82 101
---------------------------------------------------------------------------------------------------------------------------------------
16.) Write a Program to Calculate the Series like 1-2+3-4…. –10 (Result = -5)
class series{
public static void main(String args[]){
int ans=0;
for(int i=1;i<=10;i++){
if(i%2==0){
ans = ans-i;
System.out.print(i+"-");
} else {
ans = ans+i;
System.out.print(i+"+");
}
}
System.out.println("\nThe Answer is : "+ans);
}
}
OUTPUT :-
1+2-3+4-5+6-7+8-9+10-
The Answer is : -5
---------------------------------------------------------------------------------------------------------------------------------------
17.) Write a Program to Calculate a Series like 1/1! + 2/2! +. …. + 10/10!
class series {
public static void main(String args[]){
double sum = 0;
for(int i=1;i<=10;i++){
double fact =1, cal=0;
for(int j=1;j<=i;j++){
fact*=j;
}
cal = i/fact;
System.out.print(cal+" ");
sum = sum+cal;

}
System.out.println();
System.out.println("\nThe Sum is : "+sum+"  ");
}
}
OUTPUT :-
1.0 1.0 0.5 0.16666666666666666 0.041666666666666664 0.008333333333333333 0.001388888888888889 1.984126984126984E-4 2.48015873015873E-5 2.7557319223985893E-6

The Sum is : 2.7182815255731922
---------------------------------------------------------------------------------------------------------------------------------------
18.)  Write a program following series. 2 3 5 9 17 33 65 …….n
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int a=1, b=2;
for(int i=1;i<=n;i++){
System.out.print(b+" ");
b = b+a;
a = a*2;
}
}
}
INPUT :- 10
OUTPUT :-
2 3 5 9 17 33 65 129 257 513
---------------------------------------------------------------------------------------------------------------------------------------
19.) Write a program to print first 10 numbers of fibonacci series, which are prime numbers.
class series{
public static void main(String args[]){
int a=1, b=1, sum=0, temp;
for(int i=1;i<=30;i++){
sum = a+b;
b=a;
a=sum;
temp = 0;
for(int j=2;j<=sum-1;j++){
if(sum%j==0){
temp=1;
}
}
if(temp == 0){
System.out.print(sum+" ");
}
}
}
}
OUTPUT :-
2 3 5 13 89 233 1597 28657 514229
---------------------------------------------------------------------------------------------------------------------------------------
20.) Write a Program to Calculate and print the sum of prime numbers between 1 to 25. (Result = 101)
class series{
public static void main(String args[]){
long sum=0;
for(int i=1;i<=25;i++){ // 4 5
long temp =0;
for(int j=2;j<=i-1;j++){ // 
if(i%j==0){
temp = temp + 1;
}
}
if(temp == 0){
System.out.print(i+" ");
sum+=i;
}
}
System.out.println("\nThe Sum is : " + sum);
}
}
OUTPUT :-
1 2 3 5 7 11 13 17 19 23
The Sum is : 101
---------------------------------------------------------------------------------------------------------------------------------------
21.) Write a Program to Calculate the sum of first 50 odd numbers. ( Result = 625 )
class series{
public static void main(String args[]){
int sum = 0;
for(int i=1;i<=50;i++){
if(i%2!=0){
sum+=i;
System.out.print(i+" ");
}
}
System.out.println("\nThe Sum of first 50 odd number is : "+sum);
}
}
OUTPUT :-
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
The Sum of first 50 odd number is : 625
---------------------------------------------------------------------------------------------------------------------------------------
22.) Write a Program to Calculate the Series like 1/2 + 2/3 + 3/4 + …+9/10 (Result = 7.071)
class series {
public static void main(String args[]){
double sum = 0;
for(double i=1;i<=10;i++){
double cal=0;
for(double j=2;j<=i+1;j++){
cal = i/j;
}
sum = sum + cal;
System.out.print(cal+" ");
}
System.out.println("\nThe Sum is : " +sum);
}
}
OUTPUT :-
0.5 0.6666666666666666 0.75 0.8 0.8333333333333334 0.8571428571428571 0.875 0.8888888888888888 0.9 0.9090909090909091
The Sum is : 7.980122655122655
---------------------------------------------------------------------------------------------------------------------------------------
23.) Write a program to read any integer number and print it in reverse.
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
long sum = 0, rem=0;
for(int i=n;n!=0;i--){
rem = n %10;
sum = sum+rem;
n = n/10;
System.out.print(rem);
}
}
}
INPUT :- 1234
OUTPUT :-
4321
---------------------------------------------------------------------------------------------------------------------------------------
24.) Write a program to read any number and print the sum of all values.
 For ex. Input - 3564 Output - 3+5+6+4 = 18
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
long sum = 0, rem = 0;
for(int i=n;n!=0;i--){
rem = n % 10;
sum = sum+rem;
n=n/10;
}
System.out.println("The Sum is : " + sum);
}
}
INPUT :- 3564
OUTPUT :-
The Sum is : 18
---------------------------------------------------------------------------------------------------------------------------------------
25.) Write a program to determine whether a number is prime or not.
 (A prime number is one, which is divisible only by 1 or itself only.)
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int temp = 0;
for(int i = 2; i<=n-1;i++){
if(n%i==0){
temp+=1;
}
}
if(temp == 0){
System.out.print(n+" is Prime");
} else {
System.out.println(n+" is Not Prime");
}
}
}
INPUT :- 13
OUTPUT :- 13 is Prime
---------------------------------------------------------------------------------------------------------------------------------------
26.) Write a Program to find out whether a given no. is an Armstrong no. or not.
 (Hint : (1*1*1) + (5*5*5) + (3*3*3) = 153, is an Armstrong no.
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int sum = 0, rem = 0, cal = n; // cal = n: because n is calculate and chege its value
for(int i=1;i<=3;i++){
rem = n % 10;
sum = (rem*rem*rem)+sum;
n /= 10;
}
if(sum == cal){
System.out.println("armstrong no");
} else {
System.out.println("Not armstrong no");
}
}
}
INPUT :- 153
OUTPUT :- armstrong no
---------------------------------------------------------------------------------------------------------------------------------------
27.) Write a program to find out Factorial of a given number. (e.g. 5! = 120)
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int fact = 1;
for(int i=1;i<=n;i++){
fact*=i;
}
System.out.println("The Factorial is : "+fact);
}
}
INPUT:- 5
OUTPUT :- The Factorial is : 120
---------------------------------------------------------------------------------------------------------------------------------------
28.) 1! + 2! + 3! + 4! + 5!..…………………………n
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
double fact = 1, sum=0;
for(int i=1;i<=n;i++){
fact*=i;
sum +=fact;
System.out.print(i+"! + ");
}
System.out.println("\n\nThe sum of n factorial is : "+sum);
}
}
INPUT :- 10
OUTPUT :-
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! +

The sum of n factorial is : 4037913.0
---------------------------------------------------------------------------------------------------------------------------------------
29.) Write a Program to generate following series:
 1/2! + 2/3! + 3/4! + 4/5! ... 10 terms
class series{
public static void main(String args[]){
double sum = 0;
for(int i=1;i<=10;i++){
double fact = 1, cal = 0;
for(int j = 2;j<=i+1;j++){
fact *= j;
}
cal = i/fact;
System.out.print(cal+ " ");
sum = sum+cal;
}
System.out.println("\n\nThe Sum is : "+sum);
}
}
OUTPUT :-
0.5 0.3333333333333333 0.125 0.03333333333333333 0.006944444444444444 0.0011904761904761906 1.7361111111111112E-4 2.2045855379188714E-5 2.48015873015873E-6 2.505210838544172E-7

The Sum is : 0.9999999749478916
---------------------------------------------------------------------------------------------------------------------------------------
30.) Write a Program to generate the following output
 1+2+3+4+5+6+7+8+9+10=55
class series{
public static void main(String args[]){
int sum=0;
for(int i=1;i<=10;i++){
sum += i;
System.out.print(i+" + ");
}
System.out.println("\nThe sum is : " + sum);
}
}
OUTPUT :-
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +
The sum is : 55
---------------------------------------------------------------------------------------------------------------------------------------
31.) Write a Program to generate the following output
1
123
12345
1234567
123456789
class series{
public static void main(String args[]){
for(int i=1;i<=9;i++){
if(i==1){
System.out.println(i);
}
if(i%2==0){
i++;
for(int j=1;j<=i;j++){
System.out.print(j);
}
System.out.println();
}
}
}
}


Second method:-
class series{
public static void main(String args[]){
for(int i=1;i<=5;i++){
for(int j=1;j<=i*2-1;j++){
System.out.print(j+" ");
}
System.out.println();
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
32.) Write a Program to print out all Armstrong numbers between 1 to 500.
class series {
public static void main(String args[]){
int count, rem = 0;
for(int n =1;n<=500;n++){
count = n;
int sum=0;
while(count != 0){
rem = count % 10;
sum = sum+(rem*rem*rem);
count /= 10;
}
if(n == sum){
System.out.print(n + " ");
}
}
}
}
OUTPUT :-
1 153 370 371 407
---------------------------------------------------------------------------------------------------------------------------------------
33.) Write a program and to accept any integer number and print the individual number in words.
 For ex. Input - 546 Output – Six Four Five
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int rem=0;
while(n != 0){
rem = n % 10;
n /= 10;

switch(rem){
case 0:
System.out.print("Zero ");
break;
case 1:
System.out.print("One ");
break;
case 2:
System.out.print("Two ");
break;
case 3:
System.out.print("Three ");
break;
case 4:
System.out.print("Four ");
break;
case 5:
System.out.print("Five ");
break;
case 6:
System.out.print("Six ");
break;
case 7:
System.out.print("Seven ");
break;
case 8:
System.out.print("Eight ");
break;
case 9:
System.out.print("Nine ");
break;
}
}
}
}
INPUT :- 546
OUTPUT :- Six Four Five
---------------------------------------------------------------------------------------------------------------------------------------
34.) Write a program and to accept any integer number and print the individual number in words.
 For ex. Input - 546 Output – Five Four Six
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int rem = 0;
int num=0;
while(n != 0){
num = (num*10)+(n % 10);
n /= 10;
}
while(num != 0){
switch(num % 10){
case 0:
System.out.print("Zero ");
break;
case 1:
System.out.print("One ");
break;
case 2:
System.out.print("Two ");
break;
case 3:
System.out.print("Three ");
break;
case 4:
System.out.print("Four ");
break;
case 5:
System.out.print("Five ");
break;
case 6:
System.out.print("Six ");
break;
case 7:
System.out.print("Seven ");
break;
case 8:
System.out.print("Eight");
break;
case 9:
System.out.print("Nine");
break;
}
num /= 10;
}
}
}
Input :- 546 
Output :- Five Four Six
---------------------------------------------------------------------------------------------------------------------------------------
35.) Write a program that accept an integer number and determine whether the inputted number is palindrome or not?
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int rem = 0, rev=0, t=n;
while(n != 0){
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if(rev == t){
System.out.println(t+" is Palidrom");
} else {
System.out.println(t+" is not Palidrom");
}
}
}
INPUT :- 121
OUTPUT :- 121 is Palidrom
---------------------------------------------------------------------------------------------------------------------------------------
36.) Write a program to do the addition of the first n terms of the fibonacci series.
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int a = 1, b=0, sum=0, cal = 1;
for(int i=1;i<=n;i++){
sum = a+b;
if(sum == 1){
System.out.print(sum+" ");
}
System.out.print(sum+" ");
cal = cal+sum;
b=a;
a=sum;
}
System.out.println("\n\n"+cal);
}
}
INPUT :- 10
OUTPUT :- 
1 1 2 3 5 8 13 21 34 55 89

232
---------------------------------------------------------------------------------------------------------------------------------------
37.) Write a program to find first N prime number.
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);

for(int j=2;j<=n;j++){
int temp = 0;
for(int i=2;i<=j-1;i++){
if(j%i == 0){
temp = 1;
}
}
if(temp == 0){
System.out.print(j+" ");
}
}
}
}
INPUT :- 100
OUTPUT :-
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
---------------------------------------------------------------------------------------------------------------------------------------
38.) Display the following series :
 1 2 2 4 8 32 256……..up to a given range.
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
long i=1, j=2, ans=1;
System.out.print(i+" "+j+" ");
for(int a=1;a<=n;a++){
i = i*j;
System.out.print(i+" ");
long t = i;
i=j;
j=t;
}
}
}
INPUT :- 5
OUTPUT :-
1 2 2 4 8 32 256
---------------------------------------------------------------------------------------------------------------------------------------
39.) Write a program to take an integer and find the sum of first and last digit.
 Ex. Input: 1234 Output: 5
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int sum = 0;
int b = n % 10;
while(n >= 10){
n /= 10;
}
int a = n;
sum = a+b;
System.out.print(sum);
}
}
INPUT :- 1234
OUTPUT :- 5
---------------------------------------------------------------------------------------------------------------------------------------
40.) Write a program to do the addition of the first n terms of the fibonacci series.
class series{
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int a = 1, b = 0, sum = 0, cal=0;
System.out.print(a+b + " ");
for(int i=1;i<=n;i++){
sum = a+b;
System.out.print(sum+" ");
cal = cal+sum;
b=a;
a=sum;
}
System.out.println("\nSum is : "+(cal+1));
}
}
INPUT :- 10
OUTPUT :-
1 1 2 3 5 8 13 21 34 55 89
Sum is : 232
---------------------------------------------------------------------------------------------------------------------------------------
41.) Write a program to find first N prime number.
class series {
public static void  main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i=1;i<=n;i++){
int temp=0;
for(int j=2;j<=i-1;j++){
if(i%j==0){
temp=1;
}
}
if(temp == 0){
System.out.print(i+" ");
}
}
}
}
INPUT :- 100
OUTPUT :-
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
---------------------------------------------------------------------------------------------------------------------------------------
42.) Write a program, which will read an integer value for a base, then read a positive integer raised to that base and print its value.
class series {
public static void main(String args[]){
int b = Integer.parseInt(args[0]);
int r = Integer.parseInt(args[1]);
int p=1;
for(int i=1;i<=r;i++){
p= p*b;
}
System.out.println("The Power of " + b + " raised to " + r + " is : " +p);
}
}
INPUT :-  b=2 r=3
OUTPUT :- The Power of 2 raised to 3 is : 8
---------------------------------------------------------------------------------------------------------------------------------------
43.) Write a Program to input any number and count the no. of digits in that number. 
class series {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
int count=0, num = n;
while(n != 0){
n /= 10;
count++;
}
System.out.println("The Number " + num + " is " + count + " digit");
}
}
INPUT :- 1234321
OUTPUT :-
The Number 1234321 is 7 digit
---------------------------------------------------------------------------------------------------------------------------------------
44.) Write a program to print first 10 numbers of fibonacci series, which are prime numbers.
class series {
public static void main(String args[]){
long a=1, b=1, sum=0, temp;
for(int i=1;i<=40;i++){ 
sum=a+b; 
// System.out.print(sum+" "); 
b=a; 
a=sum; 
temp=0;
for(int j=2;j<sum-1;j++){   
if(sum%j==0){  
temp=1; 
}
}
if(temp == 0){
System.out.print(sum+ " ");
}
}
}
OUTPUT :-
2 3 5 13 89 233 1597 28657 514229
---------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------

        ARRAY

1.) Single Dimension Array.

class sarray{

public static void main(String args[]) {

int a [] = new int [5];

for(int i=0;i<5;i++){

a[i] = i;

System.out.print(a[i]+" ");

}

}

}

OUTPUT :-

0 1 2 3 4

---------------------------------------------------------------------------------------------------------------------------------------

2.) Single Dimension Array (Static)

class sarray{

public static void main(String args[]) {

int a [] = {10, 20, 30, 40, 50};

for(int i=0;i<5;i++){

System.out.print(a[i]+" ");

}

}

}

OUTPUT :-

10 20 30 40 50

---------------------------------------------------------------------------------------------------------------------------------------

3.) Double Dimension Array(2-D array)

class darray{

public static void main(String args[]) {

int a [][] = new int[2][2];

for(int i=0;i<2;i++){

for(int j=0;j<2;j++){

System.out.print(a[i][j]+" ");

}

System.out.println();

}

}

}

OUTPUT :- 

0 0

0 0

---------------------------------------------------------------------------------------------------------------------------------------

4.) Jagged Array

class jarray{

public static void main(String args[]) {

int a [][] = new int[2][];

a[0] = new int [3];

a[1] = new int [2];

int count=0;


for(int i=0;i<a.length;i++){

for(int j=0;j<a[i].length;j++){

a[i][j] = count++;

}

}


for(int i=0;i<a.length;i++){

for(int j=0;j<a[i].length;j++){

System.out.print(a[i][j]);

}

System.out.println();

}

}

}

OUTPUT :- 

012

34

---------------------------------------------------------------------------------------------------------------------------------------

5.) Write a program to read an array of 10 values and print it.

import java.util.Scanner;

class array{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int a [] = new int[10];

System.out.println("Enter Array Elements : ");

for(int i=0;i<a.length;i++){

a[i] = sc.nextInt();

}

System.out.println("\nThe Value of an array is : ");

for(int i=0;i<a.length;i++){

System.out.print(a[i]+" ");

}

}

}

INPUT :- Enter Array Elements :

1

2

3

4

5

6

7

8

9

10

OUTPUT :-

The Value of an array is :

1 2 3 4 5 6 7 8 9 10

---------------------------------------------------------------------------------------------------------------------------------------

6.) Write a program to read an array of 10 values and print the numbers in reverse order.

import java.util.Scanner;

class array{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int a [] = new int[10];

System.out.println("Enter Array Elements : ");

for(int i=0;i<a.length;i++){

a[i] = sc.nextInt();

}

System.out.println("\nValues of an array is : ");

for(int i=a.length-1;i>=0;i--){

System.out.print(a[i]+" ");

}

}

}

INPUT :-

Enter Array Elements :

1

2

3

4

5

6

7

8

9

10

OUTPUT :-

Values of an array is :

10 9 8 7 6 5 4 3 2 1

---------------------------------------------------------------------------------------------------------------------------------------

7.) Get the 10 numbers from users & calculate Total and Average.

import java.util.Scanner;

class array{

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

int a [] = new int[10];

int sum = 0;

System.out.println("Enter Array Elements : ");

for(int i=0;i<a.length;i++){

a[i] = sc.nextInt();

sum = sum + a[i];

}

float avg = sum / 10f;

System.out.println("\nThe Sum is : "+sum);

System.out.println("The Average is : "+avg);

}

}

INPUT :-

Enter Array Elements :

1

2

3

4

5

6

7

8

9

10

OUTPUT :-

The Sum is : 55

The Average is : 5.5

---------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------------------------

CLASS AND OBJECT

1.) Simple Class and Object

class cd {

int a;

cd(){

a=10;

}

void print(){

System.out.println(a);

}

}

class demo{

public static void main(String args[]){

cd obj = new cd();

obj.print();

}

}

OUTPUT :- 

10

---------------------------------------------------------------------------------------------------------------------------------------

2.) Get Value from user and print them

class cd {

void print(int a){

System.out.println(a);

}

}


class demo{

public static void main(String args[]){

int i = Integer.parseInt(args[0]);

cd obj = new cd();

obj.print(i);

}

}

INPUT :- 8

OUTPUT :- 8

---------------------------------------------------------------------------------------------------------------------------------------

3.) Declaration of Method

class cd {

public void print(int i){

System.out.println("The Value is : "+i);

}

}

class demo{

public static void main(String args[]){

cd obj = new cd();

obj.print(10);

}

}

OUTPUT :-

    10

---------------------------------------------------------------------------------------------------------------------------------------

4.) Method Overloading

class cd {

public void print(int i){

System.out.println("Value is : "+i);

}

public void print(){

System.out.println("Hello");

}

}

class demo{

public static void main(String args[]){

cd obj = new cd();

obj.print();

obj.print(10);

}

}

OUTPUT :-

Hello

Value is : 10

---------------------------------------------------------------------------------------------------------------------------------------

5.) Default Constructor.

class demo {

int i;

demo() {

i=10;

}

void print() {

System.out.println("The Value is : "+i);

}

}

class defaultcon {

public static void main(String args[]){

demo obj = new demo();

obj.print();

}

}

OUTPUT :- 

The Value is : 10

---------------------------------------------------------------------------------------------------------------------------------------

6.) Constructor Overloading.

class demo {

int a, b;

demo() {

System.out.println("Default Constructor");

}

demo(int i) {

a=i;

System.out.println("Parameterized Constructor is : "+a);

}

demo(float f) {

System.out.println("Parameterized Constructor is : "+f);

}

}

class constructoroberloading {

public static void main(String args[]) {

demo obj = new demo();

demo obj1 = new demo(Integer.parseInt(args[0]));

demo obj2 = new demo(1.5f);

}

}

INPUT :- 10

OUTPUT :-

Default Constructor

Parameterized Constructor is : 10

Parameterized Constructor is : 1.5

---------------------------------------------------------------------------------------------------------------------------------------

7.) Get student information from user like rollno, name, city, mobile number and print this information.

class demo {

int r;

String name, city1;

Long mno;

demo(int rno, String nm, String city, Long mo) {

r = rno;

name = nm;

city1 = city;

mno = mo;

}

public void print() {

System.out.println("The Roll No is : " + r);

System.out.println("The Name is : " + name);

System.out.println("The City is : " + city1);

System.out.println("The Mobile No is : " + mno);

}

}

class constructor {

public static void main(String args[]){

demo obj = new demo(Integer.parseInt(args[0]),

args[1],

args[2],

Long.parseLong(args[3]));

obj.print();

}

}

INPUT :-

javac constructor.java

java constructor 1 abc abcd 1234567890

OUTPUT :-

The Roll No is : 1

The Name is : abc

The City is : abcd

The Mobile No is : 1234567890

---------------------------------------------------------------------------------------------------------------------------------------

8.) Static Member

class sd {

static int i;

sd() {

i=10;

}

void print() {

System.out.println(i);

}

}

class staticmember {

public static void main(String args[]) {

sd obj = new sd();

obj.print();

}

}

OUTPUT :-

10

---------------------------------------------------------------------------------------------------------------------------------------

9.) Static Method

class sd {

static int i;

sd() {

i=10;

}

static void print() {

System.out.println(i);

}

}

class staticmethod {

public static void main(String args[]) {

sd obj = new sd();

sd.print();

}

}

OUTPUT :-

10

---------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------

2 comments: