Download presentation
Presentation is loading. Please wait.
Published byFrancis Pope Modified over 9 years ago
1
For loop
2
Exercise 1 Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive of f and t. For example, if the input is f == 2, t == 26, and i == 4, the program would output: 2, 6, 10, 14, 18, 22, 26.
3
solution >>> f=input('enter the number form:') enter the number form:2 >>> t=input('enter the number to:') enter the number to:26 >>> i=input('enter the number of increments:') enter the number of increments:4 >>> for i in range(f,t,i):... print i... 2 6 10 14 18 22
4
Exercise 2 Using the for loop, write a program that calculates the square and cubes of the numbers from o to 10 inclusively and display the output as stated below: The number = 0, the square is =0, the cube is=0 The number =1, the square is =1, the cube is =1 The number =2, the square is =4, the cube is 8
5
Solution >>> for i in range(0,11):... square=i*i... cube=i*i*I >>> print'the number is =',i,'the square is=',square,'the cube is=',cube
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.