You are on page 1of 18

NESTED IF: Sample program

#Accepting Userid and Password to print a message using


Nested if

uid=input('enter the value of uid ')


pwd=int(input('enter the password '))
if(uid == 'iglobal'):
if(pwd == 123):
print('hello and good day')
else:
print('wrong password')
else:
print('wrong username')
If-Elif: sample program
#building a calculator using if-elif
a=int(input('enter the value of a '))
b=int(input('enter the value of b '))
op=input('enter the value of 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')
For Loop:
General Syntax
-----> for (variable) in range('count'):
#basic 'for Loop' syntax
n=int(input('enter the value of n '))
for i in range(n+1):
print(i)
Extended Syntax for For Loop

For (variable) in range(start, end, jump)

#sample program for complete syntax of For Loop

n=int(input('enter the value of n '))

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

print(i)
Adding of numbers in for loop

#sample program for adding numbers using for loop

n=int(input('enter the value of n '))

a=0

for i in range(n+1):

a=a+i

print(a)
#Sample program for adding numbers using for loop having print statement
inside loop

n=int(input('enter the value of n '))

a=0

for i in range(n+1):

a=a+i

print(a)
#Sample program for finding factorial of a number

n=int(input('enter the value of n '))

a=1

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

a=a*i

print(a)
#To find the factors of a number

n=int(input('enter the value of n '))

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

if(n%i==0):

print(i)

O/P:
#Finding the factorial of all the numbers until N

n=int(input('enter the value of n '))

for i in range(n+1):

a=1

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

a=a*k

print('the factorial of',i,'is',a)

O/P:
Nested - for Loop

#Sample Program for Nested For Loop

n=int(input('enter the value of n '))

for i in range(n+1):

for k in range(i+1):

c=i*k

print(i,'*',k, '=',c)
#Find the biggest of two numbers

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

if(a>b):

print('the biggest number is',a)

else:

print('the biggest number is',b)

O/P:
#Find the biggest of three numbers using nested if

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

c=int(input('enter the value of c '))

if(a>b):

if(a>c):

print('the biggest number is',a)

else:

print('the biggest number is',c)

else:

if(b>c):

print('the biggest number is',b)

else:

print('the biggest number is',c)


#Biggest of three numbers using if-elif

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

c=int(input('enter the value of c '))

if(a>b and a>c):

print('the biggest number is',a)

elif(b>a and b>c):

print('the biggest number is',b)

else:

print('the biggest number is',c)

O/P:
#Print the prime numbers upto N

n=int(input('enter the value of n '))

count=0

for i in range(n+1):

a=0

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

if(i%k==0):

a=a+1

if(a==2):

count=count+1

print('number',i,'is prime')

print(count,'prime numbers are existing')


#Print whether a given number is even and odd

n=int(input('enter the value of n '))

if(n%2==0):

print('the given number is even')

else:

print('the given number is odd')


#Swapping of two values

a=int(input('enter the value of a '))

b=int(input('enter the value of b '))

a,b=b,a

print('value of a is',a,'value of b is',b)

O/P:

You might also like