[Java Programming] Programs using java programming constructs [Science Tutor]

1.1: Program on if statement.
Import java.io.*;
Class If
{
                Public static void main(String s[ ]) throws IOExcepiton
{
                InputStreamReader read=new InputStreamReader(System.in);
                DufferedReader in=new BufferedReader(read);
                Int a;
                System.out.println(“Enter the Number”);
                a=Integer.parseInt(in.readLine());
                if(a>0)
                                Sytem.out.println(“Number is Positive”);
}
}


Output is
Enter Number 5
Number is Positive



-

1.2: Program on if – else statement
Import java.io.*;
Class IfElse
{
                Public static void main(String s[ ]) throws IOExcepiton
{
                InputStreamReader read=new InputStreamReader(System.in);
                DufferedReader in=new BufferedReader(read);
                Int a;
                System.out.println(“Enter the Number”);
                a=Integer.parseInt(in.readLine());
                if(a>0)
                                Sytem.out.println(“Number is Positive”);
                Else
                                Sytem.out.println(“Number is Negative”);
}
}


Output is
Enter the Number 7
Number is Positive
Or
Enter the Number -8
Number is Negative



1.3: Program on Nested – if Statement
Import java.io.*;
Class Nested
{
Public static void main(String s[ ]) throws IOException
{
                InputStreamReader read=new InputStreamReader(System.in);
                BufferedReader read=new InputStreamReader(read);
                Int a,b,c;
                System.out.println(Enter three numbers”);
                a=integer.parseInt(in.readLine( ));
                b=integer.parseInt(in.readLine( ));
                c=Integer.parseInt(in.readLine( ));
                                if(a>b)
                {
                                                If(a>b)
                                {
                                                System.out.println(a+” is greatest”);
                                }
                                                Else       // c>a
                                {
                                                System.out.println(c+” is greatest”);
                                }
                }
                                Else       // b>a
                {
                                                If (b>c)
                                {
                                                System.out.println(b+” is greatest”);
                                }
                                                Else       // c>b
                                {
                                                System.out.println(c+” is greatest”);
                                }
                }
}
}

Output is
Enter three numbers 54  80  45
80 is greatest



-

1.4. Program on Ladder – if (if else if ) statement
Import java.io.*;
Class Ladder
{
Public static void main(String s[ ]) throws IOException
{
                InputStreamReader read=new InputStreamReader(System.in);
                BufferedReader in=new BufferedReader(read);
                Int marks, per;
                System.out.println(“Enter the marks”);
                Marks=Integer.parseInt(in.readLine( ));
                Per=(marks/500)*100;
                                If(per>90)
{
                                                System.out.println(“A Grade”);
}
                                Elseif(per>80 && per<=90)
{
                                                System.out.println(“B grade”);
}
                                Elseif(per>70 && per<=80)
                {
                                                System.out.println(“C grade”);
                }
                                Elseif(per>60 && per<=70)
                {
                                                System.out.println(“D grade”);
                }             
                                Else
                {
                                                System.out.println(“Fail”);
                }
}
}


Output is
Enter the marks 300
D grade




-

1.5. Program on switch statement
Import java.io.*;
Class Switch
{
Public static void main(String s[ ]) throws IOException
{
                inputStreamReader Read= new Input StreamReader(System.in);
                BufferedReader in= new BufferedReader(read);
                Int n;
                System.out.println(“Enter the choice”);
                n=integer.parseInt(in.readLine( ));
                                Switch(n)
                {
                                Case 1:
                                                System.out.println(“it is Sunday”);
                                                Break;
                                Case 2:
                                                System.out.println(“It is Monday”);
                                                Break;
                                Case3:
                                                System.out.println(“It is Tuesday”);
                                                Break;
                                Case4:
                                                System.out.println(“It is Wednesday”);
                                                Break;
Case5:
                                                System.out.println(“it is Thursday”);
                                                Break;
                                Case6:
                                                System.out.println(“It is Friday”);
                                                Break;
                                Case7:
                                                System.out.println(“It is Saturday”);
                                                Break;
                                Default:
                                                System.out.println(“Wrong choice “);
                }
}
}

Output is
Enter the choice 4
It is Wednesday.



-

1.6. Program on for loop
Import java.io.*;
Class for
{
Public static void main(String s[ ])
{
                                For(int i=1; i<=10;i++)
                {
                                System.out.println(“Number=”+i);
                }
}
}


Output is
Number = 1
Number =2
Number =3
Number =4
Number =5
Number =6
Number =7
Number =8
Number =9
Number =10



-


1.7. Program on while loop
Import java.io.*;
Class while
{
Public static void main(String s[ ])
{
Int i=!;
                                While(i<=10)
                {
                                                System.out.println(“Number=+i);
                                                i++;
                }
}
}


Output is
Number =1
Number =2
Number =3
Number =4
Number =5
Number =6
Number =7
Number =8
Number =9
Number =10


-



1.8. Program on do – while loop
Import java.io.*;
Class Dowhile
{
Public static void main(String s[ ])
{
                Int i=1;
                                Do
                {
                                                System.out.println(“Number=”+i);
                                                i + +;
                }
                                While(i<=10);
}
}

Output
Number =1
Number =2
Number =3
Number =4
Number =5
Number =6
Number =7
Number =8
Number =9
Number =10



-

1.9. Program on break statement
Import java.io.*;
Class Break
{
Public static void main(String s[ ])
{
                                For(int i=1; i<=10; i++)
                {
                                If(i= =3)
                                Break;
                                                System.out.println(“Number=”+i);
                }
}
}


Output is
Number =1
Number =2



-


1.10. Program on continue statement
Import java.io.*;
Class continue
{
Public static void main(String s[ ])
{
                                For(int i=1; i<=10; i++)
                {
                                If(i= =3)
                                Continue;
                                                System.out.println(“Number = “+i);
                }
}
}

Output is
Number =1
Number =2
Number =3
Number =4
Number =5
Number =6
Number =7
Number =8
Number =9

Number =10















Science Tutor - Smart Study Anytime Anywhere !!

Post a Comment

0 Comments