Page 78 - TP_V5.1_C8_fb
P. 78

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 1 in range(2, 51, 2):
                        sum_even += 1
                    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:
                    print(1)








                   76   Premium Edition-VIII
   73   74   75   76   77   78   79   80   81   82   83