Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python » Python programs

Python | Some of the Examples of loops



Python some of the examples of loop: Here, we are going to implement some of the examples based on loops in python.
Submitted by Pankaj Singh, on October 09, 2018

1. Print all the no. between 1 to n

n=int(input("Enter N: "))

for i in range(1,n+1):
    print(i)

Output

Enter N: 5 
1
2
3
4
5 

2. Print table of number

n=int(input("Enter N: "))

for i in range(1,11):
    print(n,"x",i,"=",i*n)

Output

Enter N: 2 
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10 
2 x 6 = 12 
2 x 7 = 14 
2 x 8 = 16 
2 x 9 = 18 
2 x 10 = 20	

3. Print sum of n number

n=int(input("Enter N: "))
s=0
for i in range(1,n+1):
    s=s+i
print("Sum = ",s)

Output

Enter N: 10
Sum = 55 

4. Print factorial of n

n=int(input("Enter N: "))
f=1
for i in range(n,0,-1):
    f=f*i
print("Factorial = ",f)

Output

Enter N: 4
Factorial =  24 

5. Check prime number

n=int(input("Enter N: "))
c=0
for i in range(1,n+1):
    if n%i==0:
        c=c+1
if c==2:
    print(n,"is Prime")
else:
    print(n,"is Not Prime")

Output

Enter N: 131
131 is Prime

6. Check palindrome number

n=int(input("Enter Number: "))
m=n
rev=0
while(n>0):
    dig=n%10
    rev=rev*10+dig
    n=n//10
if rev==m:
    print(m,"is Palindrome")
else:
    print(m,"is not Palindrome")

Output

Enter Number: 12321
12321 is Palindrome





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.