Java Programming Construct

 

JAVA

Programming Construct



Answer the Following Questions

                                                                (4 marks)

1.Write a program for if else

class IfDemo2{

            public static void main(String args[]){

               int a,b,c;

               a=20;

               b=34;

               c=25;

               if (a>b&&a>c){

                        System.out.println("A is bigger");

          }

               else{

                      if(b>c){

                                 System.out.println("B is bigger");

             }

                      else{

                                 System.out.println("C is bigger");

                 }

          }

         }

      }

2.Write a program for else if ladder

class ElseIfLadder{

           public static void main(String args[]){

                      int mark=65;

                      if(mark>=90){

                                 System.out.println("A Grade");

          }

                      else if(mark >=80){

                              System.out.println("B Grade");

          }       

                    else if(mark >=70){

                         System.out.println("C Grade");

          }

                else if (mark >=60){

                     System.out.println("D Grade");

          }

          else{ 

                     System.out.println("F Grade");

          }

 

     }

}

3.Write a program for switch case using Arithmetic Operator

import java.util.*;

class SwitchDemo

{

public static void main(String args[])

{

       int val1,val2;

        char opr;

        Scanner sc=new Scanner(System.in);

           val1 = sc.nextInt();

         val2 = sc.nextInt();

        opr = sc.next().charAt(0);

       switch (opr)

       {

case '+':

        System.out.println("Sum = " + (val1+val2));

        break;

case '-':

        System.out.println("Difference = "+ (val1-val2));

        break;

case '*':

        System.out.println("Product = " + (val1*val2));

        break;

case '/':

        System.out.println("Quotient = " + (val1/val2));

        break;

default:

         System.out.println("Invaild choice");

       }

}

}

4.Write a program for break and continue

Break

class BreakDemo

{

 public static void main(String args[])

{

int i=0;

while(i<10)

{

if(i==5)

{

 break;

}

else

{

System.out.println(i);

}

i++;

 }

  }

}

Continue

class ContinueDemo

{

 public static void main(String args[])

{

int i=0;

for(i=0;i<10;i++)

{

if(i==5)

{

 continue;

}

System.out.println(i);

 }

  }

}

 

5.Write a program for labeled loop

class BreakLoopDemo{

                  public static void main(String args[]){

                 boolean t =true;

                 first: {

                               second: {

                                           third: {

                        System.out.println("Before the break.");

                                                    if(t)

                                                                             break second;

                                                                             System.out.println("This won't execute");

                             }

                            

                             System.out.println("This won't execute");

                                                 }

System.out.println("This is after second block.");

                     }

          }

}

6.Write a program for Floyd Triangle

class FloydTriangle{

public static void main(String args[]){

int i,j,r;

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

r=i%2;

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

r=(r==0)?1:0;

System.out.print(r+”\t”);

}

System.out.println(“\n”);

}

}

}

7.Write a program for Reverse the Number

class ReverseNum{

public static void main(String args[]){

int num=12345;

int revnum=0;

System.out.println(“Number:”+num);

while(num>0){

revnum=revnum*10+num%10;

num/=10;

}

System.out.println(“Reversed Number:”+revnum);

}

}

8.   Write a program for below condition

                   1

                   2      3

                   4       5       6

                   7       8       9       10

class NumberTable{

public static void main(String args[]){

byte i,j,k;

for(i=1,k=1;k<=5;k++)

{

for(j=1;j<=k;i++,j++)

System.out.println(i+”\t”);

System.out.println(”\n”);

}

}

}


9.Write a program for sum of even number and sum of odd number

class SumOddEven{

public static void main(String args[]){

int OddSum=0,EvenSum=0;

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

if(i%2==0)

EvenSum+=i;

else

OddSum+=i;

}

System.out.println(“Sum of Odd Numbers:”+OddSum+”\nSum of Even Numbers:”+EvenSum);

}

}

10.Write a program to print 1 to 100 using for loop

class ForDemo

{

public static void main (String args[])

{

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

  {

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

  }

}

}

 

Answer the Following Questions

                                                                (10 marks)

1.Write a Program for Looping statement

o  For

o  While

o  Do while

For:

The for loop, repeats a statement or block of statements some number of times until a condition is matched. for loops are frequently used for simple iteration in which you repea a block of statements a certain number of times and then stop, but you can use for loops for just about any kind of loop.

The for loop in Java looks roughly like this:

Syntax

for(initialization; condition checking;increment/decrement){

   statement;

}

class ForDemo

{

public static void main (String args[])

{

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

  {

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

  }

}

}

While loop

Syntax: 

While (condition){

     bodyOfLoop;

}

 

File name: WhileDemo.java

class WhileDemo{

public static void main (String args[]){

          int nos = 1;

          int x = 1;

           System.out.println("First five odd nos:\n");

while(nos<=5)

{

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

    x=x+2; 

 nos = nos+1;

   }

}

}

 

DO........WHILE LOOPS

 The do loop is just like a while loop, except that do executes a given statement or block until the condition is false. The main difference is that while loops test the condition before looping, making it possible that the body of the loop will never execute if the condition is false the first time it's tested.  do loops run the body of the loop at least once before testing the condition. because the condition  to evaluate is given at the end. do loops look like this:

Syntax:

do{

          bodyOfLoop;

}while(condition);

Example

File name: DoDemo.java

class DoDemo

{

          public static void main (String args[])

{

int x=1;

do{

   System.out.println("Looping,round"+x);

    x++;

     }while (x<=10);

}

}

2.Write a Program for if statement

§  If

§  If else

§  Nested if

§  Else if ladder


 If

if (condition)

        statement1;

if (condition)

        statement2;

 

Example 1:

File name:IfDemo.java

class IfDemo{

     public static void main(String args[]){

    int a=10;

    if(a>0)

    {

         System.out.println("A is positive");

     }

    if(a<0)

   {

         System.out.println("A is Negative");

    }

   }

}

The output appears as given below:



If else

if (condition)

        statement1;

else

        statement2;

Example2:

File name: IfDemo2.java

class IfDemo2{

            public static void main(String args[]){

               int a,b,c;

               a=20;

               b=34;

               c=25;

               if (a>b&&a>c){

                        System.out.println("A is bigger");

          }

               else{

                      if(b>c){

                                 System.out.println("B is bigger");

             }

                      else{

                                 System.out.println("C is bigger");

                 }

          }

         }

      }

Nested if's

A nested if is if statement that is the target of another if or else. Nested if's are very common in programming.

File name : NestedIf.java

class NestedIf{

            public static void main(String args[]){

               int m=74;

              

               if (m>=50){

          if(m>=80){

                        System.out.println("A Grade");

             }

          else{

           System.out.println("B Grade");

          }

          }

               else{

                      if(m>=30){

                                 System.out.println("C Grade");

             }

                      else{

                                 System.out.println("Fail");

                 }

          }

         }

      }


The if-else-if Ladder

A common programming construct that is based upon a sequence of nested if's is the if-else-if.

It looks like this:

       if(condition)

                  statement;

       else if (condition)

                  statement;

        else if (condition)

                 statement;

                 ...

        else

               statement;

 Example 3:

File name: ElseIfLadder.java

class ElseIfLadder{

           public static void main(String args[]){

                      int mark=65;

                      if(mark>=90){

                                 System.out.println("A Grade");

          }

                      else if(mark >=80){

                              System.out.println("B Grade");

          }       

                    else if(mark >=70){

                         System.out.println("C Grade");

          }

                else if (mark >=60){

                     System.out.println("D Grade");

          }

          else{ 

                     System.out.println("F Grade");

          }

 

     }

}

 

 

 

No comments:

Post a Comment