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 XII - CSV FILE

CSV FILE (COMMA SEPERATED VALUE) CSV NOTES LINK :  CSV NOTES - CLICK HERE Writing data to a CSV file involves the following steps: Import the csv module Open the CSV file in write mode ( "w" ) using open() Create the writer object Write the data into the file using the writer object Close the file using close() writerow() Method This method is used to write a single row to a CSV file. It takes a sequence (list or tuple) as its parameter, and writes the items as a comma-separated line in the file. You do not need to add a newline character (\n) — it automatically places the end-of-line marker.     EXAMPLE 1: import csv  # Importing the csv module # CSV file opened using relative path csv_fobj = open("emp.csv", "w")   # Writer object created wtr_obj = csv.writer(csv_fobj) # Record with field heading is written wtr_obj.writerow(["Empno", "Name", "Salary"]) # Records with data are...