Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 77 - 2607_Traclpad_Pro_5.1_flipbook_C8
P. 77

    Program 9: To calculate the sum of numbers from 1 to 100.

                          Program9.py
                       File  Edit  Format    Run   Options   Window    Help

                        #Print the sum of numbers from 1 to 100
                        sum = 0
                        for i in range(1, 101):
                            sum += i
                        print("Sum:", sum)

                      On running the above program, we get the following output:

                          Output

                       Sum: 5050

                      Program 10: To calculate the sum of all even numbers between 1 and 50.

                          Program10.py
                       File  Edit  Format    Run   Options   Window    Help

                        # Find the sum of all even numbers between 1 and 50
                        sum_even = 0
                        for i in range(2, 51, 2):
                            sum_even += i
                        print("Sum of even numbers from 1 to 50:", sum_even)

                      On running the above program, we get the following output:

                          Output

                       Sum of even numbers from 1 to 50: 650

                      Program 11: To display numbers from 1 to 10 but skip multiples of 5.
                          Program11.py

                       File  Edit  Format    Run   Options   Window    Help

                        #Print numbers from 1 to 10, but skip multiples of 5
                        for i in range(1, 11):
                            if i % 5 == 0:
                                continue
                            print(i)

                       76   Premium Edition-VIII

| 72 73 74 75 76 77 78 79 80 81 82 |