Coding Assignments ( Python For Loop & While Loop )- Problems and Solutions

Coding Assignments ( Python For Loop & While Loop )- Problems and Solutions

This article contains coding exercises related to python for loop.



NOTE : The solutions are given at the last of this article. But try to write your code before taking the help of the solutions.


Problem 1 - Sum of 1st N natural numbers


Statement

Take an input n (integer) from the user. Find the sum of 1st n natural numbers including n.

 

Example input

10

 

Example output

55



Problem 2 - Sum of elements of a list


Statement

Given a list (define a list). Write a program to find out the sum of elements of the list.



Problem 3 - Number of odd integers in a list 


Statement 


Given a list that has integers(define a list). Write a program to count the number of odd numbers in the list.




Problem 4 (Factorial of a number)


Statement

Take an input n (integer) from the user.  Find the factorial of n

 

Example input

5

 

Example output

120



Problem 5 (Odd Numbers in given range)


Statement 


Take two integers (start and end) from the user. Print all the odd numbers between start and end.




Problem 6 (Sum of digits)


Statement 

Given an integer (take value from user). Find the sum of its digits.


Example input

123

 

Example output

6



Problem 7 (Palindrome Number)

Statement 

Let's call an integer a palindrome if it remains the same after reading

its digits from right to left. Given an integer, print "YES" if it's a palindrome and print "NO" otherwise.


Example input #1

1221

 

Example output #1

YES

 

Example input #2

1234

 

Example output #2

NO



Problem 8 (Fibonacci number)

Statement

Fibonacci numbers are the numbers in the integer sequence starting with 1, 1 where every number after the first two is the sum of the two preceding ones:

 

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

 

Given a positive integer n, print the nth Fibonacci number.

 

Example input

6

 

Example output

8



Problem 9 (Ascending digits)

Statement

Given an integer X, print "YES" if its digits are going in an ascending order from left to right and print "NO" otherwise.

 

Example input #1

179

 

Example output #1

YES

 

Example input #2

197

 

Example output #2

NO



Solutions

1) Sum of 1st N natural numbers - https://replit.com/@rhtm123/pythonloop01

2) Sum of elements of a given list - https://replit.com/@rhtm123/pythonloop02

3) Odd integers in a list - https://replit.com/@rhtm123/pythonloop03

4) Factorial number of a number -  https://replit.com/@rhtm123/pythonloop04

5) Odd numbers in given range - https://replit.com/@rhtm123/pythonloop05

6) Sum of digits - https://replit.com/@rhtm123/pythonloop06 

7) Palindrome number - https://replit.com/@rhtm123/pythonloop07

8) Fibonacci numbers - https://replit.com/@rhtm123/pythonloop08 

9) Ascending digits - https://replit.com/@rhtm123/pythonloop09