Saturday 31 October 2020

A Swift program for to print Multiplication tables using loops

 In this program we print multiplication table using loops


Using For in loop, For in stride loop, While Loop, Repeat while loop


// Tables to avoid 1, 2, 4,7,9, 13, 15, 17, 21, 46, 49, 75, 98, 123, 146, 217, 413, 439, 467, 500

    //Multiples to avoid 1, 2, 3, 4, 7, 15, 23, 45, 54, 73, 86, 91, 92, 93, 95, 97

    //Creating a function

    func forLoops()

    {

        //Creating a for in loop for multiplication tables upto 500

        for i in 1...500

        {

            

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                {

                    continue

                }

               //Condition to skip execution at j values

                if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                {

                    continue

                }

            //Printing the multiplication tables

            print("\(i) * \(j) <<---->> \(i*j)")

            }

        }

    }

    //Creating a function

    func forStrideLoops()

    {

        //for in stride loops for multiplication tables upto 500

        for i in stride(from: 1, to: 500, by: 1)

        {

            for j in stride(from: 1, to: 100, by: 1)

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                 {

                     continue

                 }

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

            //Printing the multiplication tables

            print("\(i) * \(j) = \(i*j)")

            }

        }

    }

    //Creating a function

    func whileLopps()

    {

        //Creating and storing a variable

        var i:UInt16 = 1

        //Creating a while loop for multiplication tables up to 500

        while(i < 501)

        {

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                 {

                     continue

                 }

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

                //Printing the multiplication tables

                print("\(i) multiples of \(j) = \(i*UInt16(j))")

               

            }

            i += 1//Increasing the i value by 1

            

        }

    }

    //Creating a function

    func repeatWhileLopps()

    {

        //Creating and storing a variable

        var i:UInt16 = 1

        //Creating a repeat while loop for multiplication tables up to 500

        repeat

        {

           

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                {

                     continue

                }

                

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

                //Printing the multiplication tables

                print("\(i) times of \(j) = \(i*UInt16(j))")

            }

            i += 1//Increasing the i value by 1

        }while(i < 501)

    }

}


No comments:

Post a Comment