Showing posts with label Looping Statement. Show all posts
Showing posts with label Looping Statement. Show all posts

Looping Statement Test 6

 

Looping Statement Test 6

Q1. Write a program to print the following pattern

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Q2. Find errors in the following code:

a = int(“Enter any number”)

for i in Range[2,6]

    if a=i

      print(“A”)

    else

      print(“B”)

Q3. Write a program to accept decimal number

    and display its binary number.

Q4. Accept a number and check whether it is

    palindrome or not.

Q5. Write a program to accept a number and check whether it is a perfect number or not.

(Perfect number is a positive integer which is equal to the sum of its divisors like divisors of 6 are 1,2,3, and sum of divisors is also 6, so 6 is the perfect number)

Q6. Write a program to find the sum of the following series(accept values of x and n from user)

1 + x/1! + x2/2! + ……….xn/n!

Q7. Write a program to print the following pattern

A

B C

D E F

G H I J

K L M N O

Q8. Write a program to find the sum of following (Accept values of a, r, n from user)

a + ar + ar2 + ar3 + ………..arn

Q9. for x in range(10,20):

      if (x%2==0):

           continue

       print(x)

Q10. Write a function to display prime numbers below 25.

Looping Statement Test 5

  

Looping Statement Test 5

Q1. Write program to print the following pattern.

a.     1

1 2

1 2 3

1 2 3 4

 

b.     * * * *

* * *

* *

*

Q2. Accept 10 numbers from the user and display

    their average.

Q3. Write a program to print all prime numbers 

    that fall between two numbers(accept two 

    numbers from the user)

Q4. Write a program to display sum of odd numbers

    and even numbers that fall between 12 and 37

    (including both numbers)

Q5. Write a program to display all the numbers 

    which are divisible by 11 but not by 2 between

    100 and 500.

Q6. How many times the following loop execute?

c = 0

while c < 20:

           c += 2

Q7. Write the output of the following.

c = -9

while c < 20:

               c += 3

               print(c)

 

Q8. Write a program to print first 20 natural

    numbers except multiple of 2 & 3.

 

Q9. Write a program to print table of a number

    (accepted from user) in the following format.

    Like : input number is 7, so expected output  

    is

    7 * 1 = 7

    7 * 2 = 14 and so on

 

Q10. Write a program that keep on accepting number

     from the user until user enters Zero. 

     Display the sum and average of all the 

     numbers.

 

 

Python : Looping Statement Test 4

Python : Looping Statement Test 4

Q1. What is the use of range function in python?

Q2. Name three jump statements in python and explain any one.

Q3. Write the output of the following program.

Code

Output

x = "123"

for i in x:

  print(i)

 

 

i=8

while True:

  if i%3==0:

    break

  print("A")

 

 

i=9

while True:

  if i%3==0:

    break

  print("A")

 

 

i=0

while i<3:

  print(i)

  i=i+1

else:

  print(0)

 

 

i=0

while i<3:

  print(i)

  i=i+1

print(0)

 

 

i=0

while i<3:

  print(i)

  i=i+1

  print(0)

 

 

i=2

for x in range(i):

  i+=1

  print(i)

  print(i)

 

 

i=2

for x in range(i):

  x+=1

  print(x)

print(x)

 

 

i=2

for x in range(i):

  x+=1

  print(x)

  print(x)

 

 

i=100

while i<57:

  print(i)

  i+=5

 

 

i=100

while i>57:

  print(i+1)

  i+=5

 

 

 

Q4. Write the output and convert it into for loop.

i=100

while i>57:

               print(i)

               i-=5

 

 

Solution of Q3.

Code

Output

x = "123"

for i in x:

  print(i)

 

1

2

3

i=8

while True:

  if i%3==0:

    break

  print("A")

 

Infinite loop

i=9

while True:

  if i%3==0:

    break

  print("A")

 

No Output

i=0

while i<3:

  print(i)

  i=i+1

else:

  print(0)

 

0

1

2

0

i=0

while i<3:

  print(i)

  i=i+1

print(0)

 

0

1

2

0

i=0

while i<3:

  print(i)

  i=i+1

  print(0)

 

0

0

1

0

2

0

i=2

for x in range(i):

  i+=1

  print(i)

  print(i)

 

3

3

4

4

i=2

for x in range(i):

  x+=1

  print(x)

print(x)

 

1

2

2

i=2

for x in range(i):

  x+=1

  print(x)

  print(x)

 

1

1

2

2

i=100

while i<57:

  print(i)

  i+=5

 

No Output

i=100

while i>57:

  print(i+1)

  i+=5

 

Infinite loop

 


Python : Nested Loop Test 1

 

Python Nested Loop Test-1

Q1. Write a program to print the following pattern.

S. No

Pattern

Code

1.

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

 

 

2.

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

 

 

3.

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

 

 

4.

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

 

 

5.

5

5 4

5 4 3

5 4 3 2

5 4 3 2 1

 

 

6.

1

2 3

3 4 5

4 5 6 7

5 6 7 8 9

 

 

7.

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

 

 

8.

*

* *

* * *

* * * *

* * * * *

 

 

 

 

Solutions :

S. No

Pattern

Code

1.

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

 

2.

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

 

3.

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

 

4.

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

 

5.

5

5 4

5 4 3

5 4 3 2

5 4 3 2 1

 

6.

1

2 3

3 4 5

4 5 6 7

5 6 7 8 9

 

7.

1

2 1

3 2 1

4 3 2 1

5 4 3 2 1

 

8.

*

* *

* * *

* * * *

* * * * *

 

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet