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

PYTHON - MYSQL CONNECTIVITY CODE

  #INSERTION OF DATA import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="root", database="school" ) print("Successfully Connected") #print(mydb) mycursor=mydb.cursor()   v1=int(input("enter ID:")) v2=input("enter name:") v3=input("enter Gender:") v4=int(input("enter age:")) sql='insert into TEACH values("%d","%s","%s","%s")'%(v1,v2,v3,v4) print(sql) mycursor.execute(sql) mydb.commit() print("record added") #MYSQL Connection code – Deletion on database SOURCE CODE: s=int(input("enter id of TEACHER to be deleted:")) r=(s,) v="delete from TEACH where id=%s" mycursor.execute(v,r) mydb.commit() print("record deleted") MYSQL Connection code – Updation on database SOURCE CODE: import mysql.connector mydb = mysql.connector.c...

GRADE XII CS - VIVA QUESTIONS

  VIVA QUESTIONS GRADE XII CS Dear All Be thorough with your project and practical files, as the viva can be asked from anywhere. Stay calm, don’t get nervous, and be confident in front of the examiner. 1. Tell me about your project. 2. Which concepts you have used for your project? 3. What do you mean by front end and back end? How they are important in developing any such projects? 4  Mention the modules and built-in functions you have used in your project. 5. Which real world problems are solved by your project? 6. Explain the most important feature of your project. 7. Name a few mutable data types of python. Lists, Sets, and Dictionaries 8. Name a few immutable data types of python. Strings, Tuples, Numeric 9. Name ordered and unordered data type of python. Ordered – String, List, Tuples Unordred – Set, Dictionaries 10. What is the significance of a pass statement in python? pass is no operation python statement. This is used where python requires syntax but logic requires...