Blog

For loop program in python - Bharosewale

Posted By Naveen Singh on 18 Apr 2023

python-program
for-loop-program-in-python

For loop program in python

 

1) Write a python program to display the first 10 natural numbers.

 

for i in range(1,11):

   print(i,end=” “)

Output-:

1 2 3 4 5 6 7 8 9 10

 

2) Write a python program to calculate the sum of the  first 10 natural numbers.

 

sum = 0

for i in range(1,11):

   sum = sum+i

print("sum of first ten natural numbers:", sum)

 

Output-:

55

 

3) Write a python program to display n terms of natural numbers and their sum.

 

sum = 0

n = int(input("Enter number of terms:"))

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

   print(i, end=" ")

   sum = sum+i

print("sum:",sum)

 

Output-:

Enter number of terms: 6

1 2 3 4 5 6

sum: 21

 

4) Write a python program to read 10 numbers from keyboard and find their sum and average.

 

sum=0

for i in range(1,11):

   sum = sum+i

avg = sum/10

print("sum:", sum)

print("average:", avg)

 

Output-:

sum: 55

average: 5.5

 

5) Write a python program to display the cube of the number upto given an integer.

 

num = int(input("Enter number of terms:"))

for i in range(1,num+1):

   cube = i**3

   print("cube of",i,"is",cube)

 

Output-:

cube of 1 is 1

cube of 2 is 8

cube of 3 is 27

cube of 4 is 64

cube of 5 is 125

 

6) write a python program to display the multiplication table of an given integer.

 

a = int(input("Enter any number:"))

for i in range(1,11):

   table= a*i

   print(a,"x",i,"=",table)

 

Output-:

Enter any number: 5

5x1=5

5x2=10

5x3=15

5x4=20

5x5=25

5x6=30

5x7=35

5x8=40

5x9=45

5x10=50

 

7) python program to display the multiplier table vertically from 1 to n.

 

n = int(input("Enter number of terms:"))

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

   print()

   for j in range(1,11):

      table = i*j

      print(i,"x",j,"=",table)

 

Output-:

Enter number of terms:4

1 x 1 = 1

1 x 2 = 2

1 x 3 = 3

1 x 4 = 4

1 x 5 = 5

1 x 6 = 6

1 x 7 = 7

1 x 8 = 8

1 x 9 = 9

1 x 10 = 10

 

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 x 1 = 3

3 x 2 = 6

3 x 3 = 9

3 x 4 = 12

3 x 5 = 15

3 x 6 = 18

3 x 7 = 21

3 x 8 = 24

3 x 9 = 27

3 x 10 = 30

 

4 x 1 = 4

4 x 2 = 8

4 x 3 = 12

4 x 4 = 16

4 x 5 = 20

4 x 6 = 24

4 x 7 = 28

4 x 8 = 32

4 x 9 = 36

4 x 10 = 40

 

8) Write a python program to display the n terms of odd natural numbers and their sum.

 

n = int(input("Enter number of terms:"))

sum=0

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

   if(i%2!=0):

      print(i,end=" ")

      sum=sum+i

print("sum of odd natural numbers is:",sum)

 

Output-:

Enter number of terms: 10

1 3 5 7 9

sum of odd natural numbers is: 25

 

14) Write a python program to make such a pattern as a pyramid with an asterisk.

 

   *

  * *

 * * *

* * * *

 

n = int(input("Enter number of rows:"))

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

   for j in range(n,i,-1):

      print(" ",end=" ")

   for k in range(1,i+1):

      print("*"," ",end=" ")

   print()

 

Output-:

Enter number of rows: 4

  

   *

  * *

 * * *

* * * *

 

15) Write a python program to calculate the factorial of a given number.

 

x =int(input("Enter number:"))

for i in range(1,x+1):

   fact = fact * i

print("factorial of a given number is:", fact)

 

Output-:

Enter number: 5

factorial of a given number is: 120

 

16) Write a python program to display the sum of n terms of even natural numbers.

 

n = int(input("Enter number of terms:"))

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

   sum = sum+2*i

print("sum of series:", sum)

 

Output-:

Enter number of terms: 5

sum of series: 30

 

17) Write a python program to make such a pattern like a pyramid with a number which will repeat the number in the same row.

 

    1

   2 2

  3 3 3

 4 4 4 4

 

n = int(input("Enter number of terms:"))

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

   for j in range(n,i,-1):

      print(" ",end=" ")

   for k in range(1,i+1):

      print(i," ",end=" ")

   print()

 

Output-:

 

Enter number of terms:4

        1

      2   2

    3   3   3

  4   4   4   4

 

18) Write a python program to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].

 

n = int(input("Enter number of terms:"))

x = int(input("Enter any number:"))

sq = 1

sum = 0

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

   fact = 1

   for j in range(1,2*i+1):

      fact = fact * j

   sq = (-1)*i * x*(2*i)

   sum += sq/fact

print("sum of series:", sum)

 

Output-:

Enter number of terms: 5

Enter any number: 2

sum of series: -0.416155

 

19) Write a python program to display the n terms of a harmonic series and their sum.

1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms

 

sum = 0

n = int(input("Enter number of terms:"))

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

   sum += 1/i

   print("1/",i,"+",end=" ")

print()

print("sum of series:", sum)

 

Output-:

Enter number of terms: 5

1/1 + 1/2 + 1/3 + 1/4 + 1/5+

sum of series: 2.2833333

 

20) Write a python program to display the pattern as a pyramid using asterisks, with each row containing an odd number of asterisks. Go to the editor

           *

      *   *   *

  *   *   *   *   *

 

n = int(input(" Enter number of terms:"))

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

   for j in range(n+1,i,-1):

         print(" ",end= " ")

   for k in range(1,i+1):

         if(i%2!=0):

                 print("*"," ",end=" ")

   print()

  

Output-:

Enter number of terms:5

           *

      *   *   *

  *   *   *   *   *

 

21) Write a python program to display the sum of the series [ 9 + 99 + 999 + 9999 ...].

 

n = int(input("Enter number of terms:"))

sum=0

total =0

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

   sum = sum*10+9

   total = total + sum

print("sum of series:", total)

 

Output-:

Enter number of terms: 5

sum of series: 111105

 

22. Write a python program to print Floyd's Triangle. Go to the editor

 

1

01

101

0101

10101

 

n = int(input("Enter number of rows:"))

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

   if(i%2==0):

      x = 0

   else:

      x = 1

   for j in range(1,i+1):

      print(x,end=" ")

      if(x==0):

         x = 1

      else:

         x = 0

   print()

 

Output-:

Enter number of rows: 5

1

01

101

0101

10101

 

24) Write a python program to find the sum of the series [x - x^3 + x^5 + ......].

 

n = int(input ("Enter number of terms:"))

x = int(input("enter any value:"))

sq= 1

sum = x

m=-1

print("values of series are:")

print(x)

for i in range(1, n):

      sq = 2*i+1

      val = x**sq

      new = val*m

      print(new)

      sum = sum +new

      m=m*(-1)

print("sum of series:",sum)

 

Output-:

Enter number of terms:5

Enter any number: 2

value of series are:

2

-8

32

-128

512

sum of series: 410

 

25. Write a python program that displays the n terms of square natural numbers and their sum.

1 4 9 16 ... n terms

 

 

n = int(input("Enter number of terms:"))

sum=0

print("square natural numbers are :")

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

   sq = i**2

   sum = sum+sq

   print(sq,end= " ")

print()

print("sum of square natural numbers are:",sum)

 

Output-:

Enter number of terms: 5

Square natural numbers are: 1,4,9,16,25

sum of square natural numbers are: 55

 

26. Write a python program to find the sum of the series 1 +11 + 111 + 1111 + .. n terms.

 

n = int(input("Enter number of terms:"))

sum = 0

total = 0

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

   sum = sum*10+1

   total = total + sum

   print(sum,"+",end = " ")

print("sum of series:", total)

 

Output-:

Enter number of terms:5

1 + 11 + 111 + 1111 + 11111 +

sum of series: 12345

 

27. Write a python program to check whether a given number is a 'Perfect' number or not.

 

a = int(input("Enter any number:"))

sum=0

for i in range(1,a):

         if(a%i==0):

                 sum=sum+i

                 print(i)

if(sum==a):

         print("perfect number:",sum)

else:

         print("not a perfect number")

 

Output-:

Enter any number:28

1

2

4

7

14

perfect number:28

 

28) Write a python program to find the 'Perfect' numbers within a given number of ranges.

 

n = int(input(“Enter range:”))

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

   sum= 0

   for j in range(1,i):

      if( i%j==0):

         sum=sum+j

   if( sum==i):

      print(“perfect number”,i)

Output-:

Enter range: 50

Perfect number:6

Perfect number:28

 

29)  Write a python program to check whether a given number is an Armstrong number or not.

 

num = int(input(“Enter any number:”))

temp = num

sum=0

for i in range(1,temp+1):

   rem = temp%10

   sum += rem**3

   temp //=10

if( sum == num):

   print(“Armstrong number”)

else:

   print(“not a Armstrong number”)

 

Output-:

Enter any number: 153

Armstrong number

 

30) Write a python program to find the Armstrong number for a given range of number.

 

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

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

   temp  = i

   sum = 0

   for j in range(1,i+1):

       rem = temp%10

       sum += rem**3

       temp//=10

   if(sum == i):

       print("Armstrong number",i)     

 

Output-:

Enter range: 1000

Armstrong number 1

Armstrong number 153

Armstrong number 370

Armstrong number 371

Armstrong number 407

 

31)  Write a python program to display a pattern like a diamond.

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

 

for i in range(1,5):

    for j in range(4-i):

        print(end="  ")

    for j in range(1,i*2):

        print("*",end=" ")

    print()

for i in range(3,0,-1):

    for j in range(4-i):

        print(end="  ")

    for j in range(1,i*2):

        print("*",end=" ")

    print()

 

Output-:

         *

      * * *

   * * * * *  

 * * * * * * *

   * * * * *

     * * *

        *

 

32. Write a python program to determine whether a given number is prime or not.

 

num = int(input("Enter any number:"))

if(num%1==0 and num%num==0):

         print("prime number")

else:

         print("not a prime number")

 

Output-:

Enter any number: 13

prime number

 

33)  Write a python program to display Pascal's triangle.

 

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

c=1

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

    for j in range(1,(n-i)+1):

        print(" ",end=" ")

    for k in range(0,i):

        if k==0 or i==0:

            c=1

        else:

            c=c*(i-k)//k

        print(c," ",end=" ")

    print()      

 

Output-:

Enter rows: 5

         1
       1   1 
     1   2   1 
   1   3   3   1
 1   4   6   4   1 

 

34) Write a python program to find the prime numbers within a range of numbers.

 

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

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

        sum = 0

        for j in range(2,i):

               if(i%j==0):

                       sum=1

                       break

        if(sum==0):

               print(i,end=" ")

 

Output-:

Enter range:20

1 2 3 5 7 11 13 17 19

 

35) Write a python program to display the first n terms of the Fibonacci series.
Fibonacci series 0 1 2 3 5 8 13 .....

 

n = int(input("Enter number of terms:"))

x=0

y=1

print(x, end=" ")

print(y, end=" ")

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

    z = x + y

    print(z, end=" ")

    x = y

    y = z     

 

Output-:

Enter number of terms: 5

0 1 1 2 3 5 8

 

36) Write a python program to display a such a pattern for n rows using a number that starts with 1 and each row will have a 1 as the first and last number.

    1

  121

 12321

 

n = int(input("Enter number of terms:"))

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

         for j in range(1,(n-i)+1):

                 print(" ",end=" ")

         for j in range(1,i+1):

                 print(j,end=" ")

         for j in range(i-1,0,-1):

                 print(j,end=" ")

         print()

 

Output-:

Enter number of terms:5

           1

        1 2 1

      1 2 3 2 1

    1 2 3 4 3 2 1        

 

37) Write a program in C to display the number in reverse order.                             

 

import math

num = int(input("Enter any number for reversing:"))

sum = 0

l=int(math.log10(num))+1

for i in range(1,l+1):

    r = num%10

    sum = sum*10+r

    num = num//10

print(sum)

    

Output-:

Enter any number for reversing:12345

54321

 

38) Write a C program to check whether a number is a palindrome or not.  

 

import math

temp = int(input("Enter number:"))

sum = 0

num = temp

l=int(math.log10(num))+1

for i in range(1,l+1):

    r = num%10

    sum = sum*10+r

    num = num//10

if(sum==temp):

    print("palindrome number:",sum)

else:

    print("not a palindrome number")

 

Output-:

Enter number:121

palindrome number: 121           

 

39) Write a program in C to find the number and sum of all integers between 100 and 200 which are divisible by 9. 

 

start = int(input("Enter starting number:"))

end = int(input("Enter ending number:"))

sum=0

for i in range(start,end+1):

    if(i%9==0):

        print(i,end=" ")

        sum = sum+i

print()

print("sum of numbers:",sum)

 

Output-:

Enter starting number:100

Enter ending number:200

108 117 126 135 144 153 162 171 180 189 198

sum of numbers: 1683

 

40. Write a C program to display the pyramid pattern using the alphabet.

        A

      A B A

    A B C B A

  A B C D C B A

 

for i in range(65,70+1):

    for sp in range(70,i,-1):

        print(" ",end=" ")

    for j in range(65,i+1):

        print(chr(j),end=" ")

    for k in range(j-1,64,-1):

        print(chr(k),end=" ")

    print()

Output-:

        A
      A B A 
    A B C B A
  A B C D C B A 

 

41. Write a python program to convert a decimal number into binary without using an array.

bn = 0

a=1

n = int(input("Enter decimal value:"))

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

    dec = n%2

    bn = bn+dec*a

    a = a*10

    n = int(n/2)

print("Binary number is:",bn)

 

Output-:

Enter decimal value:10

Binary number is: 1010


 

Comments


Priyanshu

Easy program to understand and very helpful

26/04/2023