Ticker

6/recent/ticker-posts

Header Ads Widget

Contact Us For Advertisement: 9858091920

Snakify Python Program Solution For Loop with Range

  

Tutor Chetan

Python - Programming Language

Snakify.org - Problem Solution

For Loop with Range


1) Series - 1

Statement: Given two integers A and B (A ≤ B). Print all numbers from A to B inclusively.

 Model solution

  • a = int(input())
  • b = int(input())
  • for i in range(a, b + 1):
  • print(i)

 

2) Series - 2

Statement: Given two integers A and B. Print all numbers from A to B inclusively, in ascending order, if A < B, or in descending order, if A ≥ B.

Model solution

  • a = int(input())
  • b = int(input())
  • if a < b:
  • for i in range(a, b + 1):
  • print(i)
  • else:
  • for i in range(a, b - 1, -1):
  • print(i)

 

3) Sum of ten numbers

Statement: 10 numbers are given in the input. Read them and print their sum. Use as few variables as you can.

 Model solution

  • res = 0
  • for i in range(10):
  • res += int(input())
  • print(res)
OR
  • # sum = 0
  • # for i in range(10):
  • # num = int(input("Input: "))
  • # sum+=num
  • # print(sum)

 

4) Sum of N numbers

Statement: N numbers are given in the input. Read them and print their sum.

The first line of input contains the integer N, which is the number of integers to follow. Each of the next N lines contains one integer. Print the sum of these N integers. 

Model solution

  • n = int(input())
  • res = 0
  • for i in range(n):
  • res += int(input())
  • print(res)
OR
  • # item = int(input("Input: "))
  • # sum = 0
  • # for i in range(item):
  • # num = int(input("Input: "))
  • # sum+=num
  • # print(sum)

 

5) Sum of cubes

Statement: For the given integer N calculate the following sum: 13+23+…+N3

Model solution

  • res = 0
  • for i in range(1, int(input()) + 1):
  • res += i ** 3
  • print(res)
OR
  • # num = int(input("Input: "))
  • # cube = 0
  • # for i in range(num+1):
  • # cube+=(i*i*i)
  • # print(cube)

 

6) Factorial

Statement: In mathematics, the factorial of an integer nn, denoted by n!n! is the following product: n!=1×2×…×nn!=1×2×…×n

For the given integer nn calculate the value n!n!. Don't use math module in this exercise.


Model solution

  • res = 1
  • n = int(input())
  • for i in range(1, n + 1):
  • res *= i
  • print(res)
OR
  • # # num = int(input("Input: "))
  • # # fact = 1
  • # # for i in range(1, num+1):
  • # # fact*=i
  • # print(fact)

 

7) The number of zeros

Statement: Given N numbers: the first number in the input is N, after that N integers are given. Count the number of zeros among the given integers and print it.

You need to count the number of numbers that are equal to zero, not the number of zero digits.

 Model solution

  • num_zeroes = 0
  • for i in range(int(input())):
  • if int(input()) == 0:
  • num_zeroes += 1
  • print(num_zeroes)
OR
  • # num = int(input("Input: "))
  • # zero = 0
  • # for i in range(num):
  • # num = int(input("Input: "))
  • # if num == 0:
  • # zero+=1
  • # else:
  • # pass
  • # print(zero)

  

8) Adding factorials

Statement: Given an integer nn, print the sum 1!+2!+3!+...+n!1!+2!+3!+...+n!.

This problem has a solution with only one loop, so try to discover it. And don't use the math library :)

Model solution

  • n = int(input())
  • partial_factorial = 1
  • partial_sum = 0
  • for i in range(1, n + 1):
  • partial_factorial *= i
  • partial_sum += partial_factorial
  • print(partial_sum)
OR
  • # num = int(input("Input: "))
  • # sum = 0
  • # fact = 1
  • # for i in range (1, num+1):
  • # fact*=i
  • # sum+=fact
  • # print(sum)


Post a Comment

0 Comments