Showing posts with label learn java. Show all posts
Showing posts with label learn java. Show all posts

NestedForLoop

 Topic : Nested For Loop

class nestedloops

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=2;i++)

        {

            for(j=1;j<=5;j=j+2)

            {

                System.out.println("i ="+i+" j ="+j);

            }

        }

    }

}

 

class nestedloops2

 {

    public static void main()

    {

        int i,j;

        for (i=14;i>=10;i=i-2)

        {

            for(j=9;j>=1;j=j-4)

            {

                System.out.println("i ="+i+" j ="+j);

            }

        }

    }

}

 Topic Nested For Loop: Pattern

/*

 * WAP to get the following output:

        *

        **

        ***

 */

 

 class floydTriangle

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=3;++i)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

        ****

        ***

        **

        *

 */

 

 class InvertedTriangle

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=4;++i)      //rows

        {

            for(j=4;j>=i;j--)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

        4321

        432

        43

        4

 */

 

 class invertedNum

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=4;++i)      //rows

        {

            for(j=4;j>=i;j--)   //column

            {

                System.out.print(j);

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

        5

        54

        543

        5432

        54321

 */

 

 class triangleNum

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

           

            for(j=5;j>=i;j--)   //column

            {

                System.out.print(j);

               

            

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

        5

        5   4

        5   4   3

        5   4   3   2

        5   4   3   2   1

 */

 

 class triangleNumwithSpace

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

           

            for(j=5;j>=i;j--)   //column

            {

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

               

            

            }

            System.out.println();

        }

    }

}

/*

 * WAP to get the following output:

     ****

     ****

     ****

*/

 class squareSymbol

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=3;i++)      //rows

        {

            for(j=1;j<=4;j++)   //column

            {

                System.out.print("*");

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

5

54

543

5432

54321

*/

 

 class nestedloop4

{

    public static void main()

    {

        int i,j;

        for (i=5;i>=1;--i)      //rows

        {

            for(j=5;j>=i;--j)   //column

            {

                System.out.print(j);

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    A

    AB

    ABC

    ABCD

    ABCDE

A=65 to Z=90

a=97 to z=122

*/

 

 class nestedloop5

{

    public static void main()

    {

        int i,j;

        for (i=65;i<=69;i++)      //rows

        {

            for(j=65;j<=i;j++)   //column

            {

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

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to accept number of lines of floyd triangle required

 * and accordingly display the output.

 * for example the user enter 5, five lines of floyd triangle are:

 *

        1

        23

        456

        78910

        1112131415

 */

import java.util.Scanner;

 class floydTriangleReq

{

    public static void main()

    {

        int i=1,j,c=i,n;

        Scanner ob=new Scanner(System.in);

        System.out.println("Enter no. of lines floyd triangle required:");

        n=ob.nextInt();

        System.out.println("_____Output______");

        for (i=1;i<=n;i++)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                System.out.print(c);

                c++;

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    10  9    8   7

    6   5   4

    3   2

    1

  

*/

 

 class nestedloop7

{

    public static void main()

    {

        int i,j,c=10;

        for (i=4;i>=1;i--)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

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

                c--;

            }

            System.out.println();

        }

    }

}

 

/*

 * WAP to get the following output:

    *

    *#

    *#*

    *#*#

    *#*#*

   

*/

 

 class nestedloop8

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=5;i++)      //rows

        {

            for(j=1;j<=i;j++)   //column

            {

                if(j%2==0)

                {

                    System.out.print("#");

                }

                else

                {

                    System.out.print("*");

                }

               

            }

            System.out.println();

        }

       

    }

}

 

 

/*

 * WAP to get the following output:

        1   

        1    2   

        1    2    3   

        1    2    3    4   

        1    2    3    4    5   

 

 */

 

 class tri_num_invert

 

{

    public static void main()

    {

        int i,j;

        for (i=1;i<=5;i++)      //rows

        {

           

            for(j=1;j<=i;j++)   //column

            {

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

               

            

            }

            System.out.println();

        }

    }                                                                                                                       

}

 


For loop

 For Loop:















switch case

Switch Case  Program 


Write a program to print the output depending upon the input.




Write a program to check the letter and print it is vowel or consonant:

First Method:




Second method:


Write a program to print the day corresponding to the number. If 1 is the number then day is Sunday:



Write a program to print the month corresponding to the number. if 1 is the number then month is January.

class switch_month
{
public static void main()
{
int num=5;
switch(num)
{
case (1):
System.out.println(num+ " is January");
break;
case (2):
System.out.println(num+ " is Feburary");
break;
case (3):
System.out.println(num+ " is March");
break;
case (4):
System.out.println(num+ " is April");
break;
case (5):
System.out.println(num+ " is May");
break;
case (6):
System.out.println(num+ " is June");
break;
case (7):
System.out.println(num+ " is July");
break;
case (8):
System.out.println(num+ " is August");
break;
case (9):
System.out.println(num+ " is September");
break;
case (10):
System.out.println(num+ " is October");
break;
case (11):
System.out.println(num+ " is November");
break;
case (12):
System.out.println(num+ " is December");
break;
default:
System.out.println("The number is out of the range 1 to 12");
}
}
}







If else If

 If Else If Practical:

WAP to accept student name, mathematics marks and check the grade.




WAP to accept customer's name and total purchase amount, depending upon the purchase amount give discount ,calculate and display amount to be paid.


WAP to accept person name ,number of years of investment and accordingly  calculate and display the accumulated amount 


WAP to accept customer's name ,ticket amount and accordingly calculate the discount amount , amount to be paid.



WAP to accept the name, age and gender. if the age is more than 65  and gender is female display message 'wrong category' else calculate the income tax.



WAP to accept name ,roll-on ,marks of physics ,chemistry ,biology. calculate the science marks and give the grade.