Top 10 loop questions in python For interview

 

Top 10 loop questions in python For interview

 

1.Write a program to find factorial of given number.

fact=1

i=1

n=int(input(“enter the number”))

while i<=n:

    fact=fact*i

    i=i+1

print(fact)

 

Output:

enter the number4

24

 

2.Write a program to find Fibonacci Series.

n=int(input(“how many terms???”))

n1=0

n2=1

count=0

for i in range(1,n):

    print(n1,end=’ ‘)

    count=n1+n2

    n1=n2

    n2=count

Output: how many terms???10

0 1 1 2 3 5 8 13 21 34

3. Write a program to check whether the given number is Armstrong number or not.

n=372

sum=0

p=n

while n>0:

    x=int(n%10)

    sum=sum +x*x*x

    n=n/10

if p==sum:

    print(“%i is Armstrong number “%p)

else:

    print(“%i is not Armstrong number “%p)

Output:

153 is Armstrong number

  1. Write a program to check whether the given number is Prime number or not.

n=int(input(“Enter the number”))

if n>1:

    for i in range(2,n):

        if (n%i==0):

            print(“%i is not prime number “%n)

            break

    else:

             print(“%i is prime number “%n)

 

else:

    print(“%i is not prime number “%n)

Output:

Enter the number97

97 is prime number

 

  1. Write a program to check whether the given number is Perfect  number or not.

n=int(input(“Enter the number”))

sum=0

for i in range(1,n):

    if n%i==0:

        sum=sum+i

print(sum)

if sum==n:

    print(“%i is perfect number “%n)

else:

    print(“%i is not perfect number “%n)

Output:

Enter the number28

28 is perfect number

 

6.Write a program to find Armstrong Numbers in an interval(1 to 1000).

for n in range(1,1001):

    p=n

    sum=0

    while p>0:

       x=int(p%10)

       sum=sum+x*x*x

       p=int(p/10)

    if sum==n:

     print(n)

Output:

1

153

370

371

407

7.Write a program to print following Output:

****

****

****

****

i=1

while i<=4:

    j=1

    while j<=4:

        print(“*”,end=”)

        j=j+1

    print()

    i=i+1

Output:

****

****

****

****

8.Write a program to print following Output:

*

**

***

****

i=1

while i<=4:

    j=1

    while j<=i:

        print(“*”,end=”)

        j=j+1

    print()

    i=i+1

Output:

*

**

***

****

  1. Write a program to print following Output:

      *

    **

  ***

****

i=1

while i<=4:

    k=4

    while k>i:

        print(‘ ‘,end=”)

        k=k-1

    j=1

    while j<=i:

        print(“*”,end=”)

        j=j+1

    print()

    i=i+1

Output:

      *

    **

  ***

****

10.Write a program to print 1 to 10 multiplication table.

i=1

while i<=10:

    j=1

    while j<=10:

        print(i*j ,end=’\t’)

        j=j+1

    print()

    i=i+1

  

Output:

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

 

Request For Call Back

 

Summary
Article Name
Top 10 loop questions in python For interview
Description
Top 10 loop questions in python For interview
Author
Publisher Name
Lotus IT Hub
Publisher Logo