Skip to main content

XI CS - FOR LOOP PRACTICE QUESTIONS

 

                                                  FOR LOOP PRACTICE QUESTIONS


1. WAP TO PRINT 10 8 6 4 2 AND SUM OF SERIES 

2.  WAP TO PRINT FIRST 20 EVEN NUMBERS AND PRINT THEIR SUM

3. WAP TO PRINT FACTORIAL OF A GIVEN NUMBER

SOLUTION:

n=int(input("Enter a number -"))

res =1

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

    res *= i

print(n," = ",res)

4. WAP TO PRINT EVEN NOS BETWEEN 2 GIVEN NOS

5. WAP TO INPUT NUMBERS & PRINT ITS FACTORS

SOLUTION:

n = int(input(“Enter a number -”))

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

    if (n%i==0):

        print("\t -",i)

6. WAP TO PRINT FIBONACCI  SERIES  : 0  1  1  2  3  5  8.....N TERMS

SOLUTION:

f, s = 0,1

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

print(f, s , sep="\t ", end=" ")

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

    t = f + s

    print(t , end=" ")

    f, s = s, t                                        # f = s and s=t


7. Write a Python program to accept n number of integers from user. 

Count and display the number of odd, even and zero entered by the user. Accept n from user

SOLUTION:



8. WAP to input the value of n and calculate sum of the series:

1 / x2 + 2 / x3 + 3 / x4 + …… n /x n+1

SOLUTION:

n = int(input("Enter Value of n -"))

x = int(input("Enter Value of x -"))

sum = 0

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

sum + = i/ x ** (i+1)          # OR    t=x**i/i+1      sum+=t

else:

print("sum of series -",sum)


9. Write a program to input the value of n and calculate sum of the series:

1 + 22 + 32 + …… n2

SOLUTION:

n = int(input("Enter Value of n -"))

sum = 0

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

sum += i** 2

else:

print("sum of series -",sum)


10. Write code to check for perfect square . 

A perfect square is an integer that is the square of another integer 

is a perfect square because 3×3=93 \times 3 = 9    Eg: 25 (5x5) , 36(6x6)

Solution:

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

i = 1  

sq=False

while i * i <= n:  

        if i * i == n:  

           print("perfect square") 

           sq=True

           break

        i += 1      

if not sq:

    print("No") 


11. #WAP TO PRINT SUM OF DIGITS

num=int(input("Enter No"))

#123

ds=0

while num>0:

    ds=ds+num%10

    num=num//10

print("Sum of Digits:",ds)


12. Write a program to enter a number and check if it is a Harshad Number. A Harshad number is divisible by the sum of its digits. Eg.21,111,153

n = int(input("Enter Value of n -"))   

sum=0  

c=n                                                     

while n>0:                                          

     sum+= n%10                                 

     n//=10                                          

if c%sum==0:                                  

     print("Harshad number")

else:

    print("Not a Harshad number")


13. # Python program to check if the number is an Armstrong number or not
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.


# take input from the user
num = int(input("Enter a number: "))
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10
# display the result
if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")

14.  # Python program to check if the number is a Palindrome number or not
eg: 12321 , 1234321

15. Mersenne numbers are a special class of integers that are one less than a power of two. 

Mn​=2n−1

Where n is a positive integer .

Eg:

If n=2 , 22-1 , then 4-1=3

If n=3 , 23-1, then 8-1=7

If n=4, 24-1 , then 16-1=15

#Mersenne Number

n = int(input("Enter number"))

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

    mersenne = 2**i - 1

    print("Mersenne number :",mersenne)


Comments

Popular posts from this blog

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XI | DESIGN THINKING & INNOVATION (848) | INTRO | CH-1

  DESIGN THINKING &  INNOVATION (848)  INTRODUCTION LINK : INTRODUCTION PPT PPT LINK Notes: Design Thinking and Innovation Grade XI Introduction Section   I. What is Design? (0.1.1) Definitions from Experts: John Maeda: "Design is solution to a problem". Saul Bass: "Design is thinking made visual". Charles Eames:   “Design is a plan for arranging elements in such a way as best to accomplish a particular purpose.” Steve Jobs:  "Design is not just what it looks like and feels like. Design is how it works". Prof. Sudhakar Nadkarni:  "Essentials of design are— purity, precision, details". Design is a way of understanding needs, identifying problems, and creating appropriate and innovative solutions. It is not only about appearance, but also about usefulness and sustainability. Design is explained as something that helps solve problems and make a positive difference. Key ...