1/8/2024 0 Comments Odd numbers in python![]() ![]() Now let us modify all three above programs and write them by taking input from the user. Call a function and pass variable n ( print_odd(n)).Outside of the function Take variable n = 100. ![]() Inside the for-loop, check if i % 2 != 0: then do step-4.Inside function body takes a for-loop from 1 to num.Define a function that will take one argument ( def print_odd(num)).Outside of if-block, increment num by 1 ( num = num + 1).Inside the while-loop check if num % 2 != 0then do step-4.Iterate using while-loop as long as num is less or equal to 100 ( while num Take one variable called “ num” initialize it with 0 ( num = 0).Let’s see it with the help of an example: Method 2: Using list comprehension This is the. Auxiliary space: O (m), where m is the number of odd elements in the list. step argument helps us to skip the iteration in the for-loop, so we will skip one iteration and print odd numbers. print('Indices list Odd elements is : ' + str(res)) Output : The original list is : 5, 6, 10, 4, 7, 1, 19 Indices list Odd elements is : 0, 4, 5, 6 Time complexity: O (n), where n is the number of elements in the list. As we know range function takes three arguments ( start: end: step). There is an alternate approach using a for-loop where you can use the step argument of the range function. # The range function does not include the end value, so only numbers from 1 to 100 are considered. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |