0% found this document useful (0 votes)
15 views

Update Notes 2024-25

Chapter 5 covers conditional and looping constructs in programming, explaining concepts like the pass statement, break and continue statements, and providing various programming exercises. It includes examples of programs that check conditions, calculate areas, determine grades, and generate series using loops. The chapter also discusses nested loops and the importance of updating loop control variables to avoid infinite loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Update Notes 2024-25

Chapter 5 covers conditional and looping constructs in programming, explaining concepts like the pass statement, break and continue statements, and providing various programming exercises. It includes examples of programs that check conditions, calculate areas, determine grades, and generate series using loops. The chapter also discusses nested loops and the importance of updating loop control variables to avoid infinite loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter -5

Conditional and Looping Constrruct


Q-1 What is pass statement?
A-1 Pass Statement:The pass statement is an empty statement which does nothing.It iss is
useful in situations where syntax of language requires presence of statement but where
logic of program does not.It is also call null statement.
Example:
if a==0:
Pass
else:
print(‘No is not zero’)
Q-2 Differentiate between break and continue with the help of an example?
A-2 break & continue both are jump statements.
The break statement is used to exit or terminates a loop prematurely, and control
immediately transfers to the statement following the loop.
Example:
for i in range(1,6):
if i==3:
break
print(i)
The continue"statement is used to halt the current iteration of the loop and immediately
proceed to the next iteration, skipping any remaining statements within the loop's body for
the current iteration.
Example:
for i in range(1,6):
if i==3:
continue
print(i)

Programming-
selection
3.Write a program that takes a number and checks if the given number is greater than
100.(donot give else condition)
num=int(input("Enter any no:"))
if num>100:
print(num,“ is greater then 100")
4.Write a program that takes a number and checks whether the given number is odd or
even.
num=int(input("Enter any no:"))
if num%2==0:
print("Even no")
else:
print("Odd no")
5.Write a program to input a number and check whether the entered no is positive,
negative or zero.
num=int(input("Enter any no:"))
if num>0:
print(num,"is positive no")
elif num<0:
print(num,"is negative no")
else:
print("No is zero")
6.Write a program to accept three number and print the maximum(largest) of the three.
a=int(input(“Enter first no:”))
b=int(input(“Enter second no:”))
c=int(input(“Enter third no:”))
max=a
if b>max:
max=b
if c>max:
max=c:
print(“Maximum no=“, max)
7.Write a program to calculate and display area of circle and triangle.
print(“1.Area of circle”)
print(“2.Area of triangle”)
choice=int(input(“Enter your choice:”))
if choice==1:
r=float(input(“Enter radius:”))
print(3.14*r*r)
else:
b=float(input(“Enter base:”))
h=float(input(“Enter height:”))
print(0.5*b*h)
8. Write a program to input a character and check whether a given character is an
uppercase or a lowercase or a digit or any other character.
ch=input("Enter any character:")
if ch>='A' and ch<='Z':
print("Its an Uppercase character")
elif ch>='a' and ch<='z':
print("Its an Lowercase character")
elif ch>='0' and ch<='9':
print("Its a digit")
else:
print("Any other character")
9.Write a program to input two numbers and an arithmetic operator and display the
computed result.(make four function calculator.)
a=float(input("Enter first no:"))
b=float(input("Enter second no:"))
op=input("Enter an operator(+,-, /, *):")
if op=='+':
print(a+b)
elif op=='-':
print(a-b)
elif op=='/':
print(a/b)
elif op=='*':
print(a*b)
else:
print("Wrong operator entered")
10.Write a program to read day of week in number and display its name on the screen.
dow=int(input(‘Enter day of week in no:’))
if dow==1:
print(‘Sunday’)
elif dow==2
print(‘Monday’)
elif dow==3:
print(‘Tuesday’)
elif dow==4:
print(‘Wednesday’)
elif dow==5:
print(‘Thursday’)
elif dow==6:
print(‘Friday’)
elif dow==7:
print(‘Saturday’)
11.Write a program to find the grade of a student when grades are allocated as given in the
table
given below:
Percentage of Marks Grade
Above 90% A
81% to 90% B
71% to 80% C
61% to 70% D
Below & equal to 60% E
perc=float(input(“Enter percentage marks:”))
if perc >90:
grade=‘A’
elif perc>80 and perc<=90:
grade=’B’
elif perc>70 and perc<=80:
grade=’C’
elif perc>60 and perc<=70:
grade=’D’
elif perc<=60:
grade=’E’
print(“For percentage”, perc , ”Grade is “,grade)

Loops
12.Write a program to print first five even nos.
for i in range(2,11, 2):
print(i, end=’ ‘)
13.Write a program to print first five odd nos.
for i in range(1,11, 2):
print(i, end=’ ‘)
14. Write a program that inputs a number and prints its multiplication table
n=int(input(‘Enter any no:’))
for i in range(1,11):
print(n,”x”, i, “=”, n*i )
15. Write a program to input a number n and print its factorial.
n=int(input(‘Enter any no:’))
fact=1
for i in range(1, n+1):
fact=fact*i
print(fact)
16. Write a Python program that inputs n numbers using a for loop and prints whether
each number is even or odd.
n=int(input('How many no you want to enter’))
for i in range(1, n+1):
num=int(input(‘Enter a no:’))
if num%2==0:
print(num,”is even no.”))
else:
print(num,”is odd no.”))
17.Write a program to find sum of n natural numbers.
n=int(input(input(‘How many nos you want to add?’))
sum=0
for i in range(1, n+1):
sum=sum+i
print(‘Sum=’,sum)
18.Write a program to generate a fibonacii series of n number of terms.
0 1 1 2 3 5 8………………………. n terms
n=int(input("Enter no of terms you want to generate:"))
f,s=0,1
print(f,s,end=' ')
for i in range(3,n+1):
t=f+s
print(t,end=' ')
f=s
s=t
19 .Write a program to find sum of series given below:
1+4+9+16+………+n2
n=int(input(“How many terms you want to add?”))
sum=0
for i in range(1,n+1):
sum=sum+i*i
print(sum)
20. Write a program to find sum of series given below:
2+4+6+8+………+2n
n=int(input(“How many terms you want to add?”))
sum=0
for i in range(1, (2*n)+1, 2):
sum=sum+i
print(sum)
21.Write a program to find sum of series given below:
1+3+5+7+………+2n-1
n=int(input(“How many terms you want to add?”))
sum=0
for i in range(1, (2*n-1)+1, 2):
sum=sum+i
print(sum)
22.Write a program to find sum of series given below:
x/1+x/2+x/3+x/4+………+x/n
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
for i in range(1, n+1):
sum=sum+x/i
print(sum)
23.Write a program to find sum of series given below:
x/2+x/4+x/6+………+x/2n
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
for i in range(1, (2*n)+1, 2):
sum=sum+x/i
print(sum)
24 .Write a program to find sum of series given below:
x/1+x/3+x/5+………+x/2n-1
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
for i in range(1, (2*n-1)+1, 2):
sum=sum+x/i
print(sum)
25.Write a program to find sum of series given below:
x1/1+x2/2+x3/3+x4/4+………+xn/n
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
p=1
for i in range(1, n+1):
sum=sum+x**p/i
p=p+1
print(sum)
26.Write a program to find sum of series given below:
X0/1+x1/2+x2/3+x3/4+………+xn-1/n
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
p=0
for i in range(1, n+1):
sum=sum+x**p/i
p=p+1
print(sum)
27.Write a program to find sum of series given below:
x1/1+x2/3+x3/5+x4/7+………+xn/2n-1
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
p=1
for i in range(1, (2* n-1)+1, 2):
sum=sum+x**p/i
p=p+1
print(sum)
28.Write a program to find sum of series given below:
x1/1+x3/2+x5/3+x7/4+………+x2n-1/n
n=int(input(“How many terms you want to add?”))
x=int(input(“Enter value of x:”))
sum=0
p=1
for i in range(1, n+1):
sum=sum+x**p/i
p=p+2
print(sum)

Nested Loops

29.Write a program to print the following pattern


1
12
123
1234
12345
for i in range(1,6):
for j in range(1,i+1):
print(j,end=' ')
print()

30.Write a program to print the following pattern:


A
AB
ABC
ABCD
ABCDE
for i in range(65,70):
for j in range(65,i+1):
print(chr(j),end=' ')
print()

31.Write a program to print the following pattern:


*
**
***
****
*****
for i in range(1,6):
for j in range(1,i+1):
print(‘*’ ,end=' ')
print()
32 What is nested loop? Explain with example?
A loop may contain another loop as a part of its body. This loop inside a loop is called a
nested loop. The inner loop runs completely every time the outer loop runs once.
Example:
for i in range(1,6):
for j in range(1,6):
print(j, end=’ ‘)
print()
It gives output:
12345
12345
12345
12345
33. What is the significance of updating loop control variable in while statements?
In a while loop, updating the loop control variable is crucial to prevent an infinite loop. If
the control variable is not updated, the condition remains true forever, causing the loop to
run endlessly.
34.Write a program to find the sum of digits of an integer number input by the user.
n=int(input("Enter anyno:"))
sum=0
while n!=0:
q=n//10
r=n%10
sum=sum+r
n=q
print(sum)
35.Write a program to find the reverse of digits of an integer number input by the user.
n=int(input("Enter anyno:"))
rev=0
while n!=0:
q=n//10
r=n%10
rev=rev*10+r
n=q
print(rev)
36.Write a program to check whether the an input number by the user is palindrome or no.
n=int(input("Enter anyno:"))
num=n
rev=0
while n!=0:
q=n//10
r=n%10
rev=rev*10+r
n=q
if num==rev:
print(num,"is palindrome no")
else:
print(num,"is not palindrome no")

You might also like