Cs Main Practicals

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Program 6-2

to print the positive difference of two numbers


num1=int (input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
diff=num1-num2
else:
diff=num2 – num1

print("The difference of", numl, "and", num2, "is", diff)num1 = 5

num2 = 6

if numl > num2:

print("first number is larger")

print("Bye")

else:

#Block2

print("second number is larger")

Output:

print("Bye Bye")

second number is larger Bye#Print first five natural numbers

print (1)

print (2)

print (3)

print (4)

print (5)

Output:

3
4

5Print the characters in word PYTHON using for loop for letter in 'PYTHON

Output:

print (letter)

NProgram 6-7

#Print the given sequence of numbers count (10,20,30,40,501 using for loop
NCERT

for num in count:

print (num)

Output:

10

20

30

40

505

Program 6-8

Program to print even numbers in a given sequence using for loop.

#Program 6-8

the range have been When all the items in usted, the statements executed; the
control d to the statement ng the for loop. While is known in advance the loop
will execute.

ting the execution of In Figure 6.4.

For Loop
ble> in <sequence/

side body of the

Print even numbers in the given sequence numbers [1,2,3,4,5,6,7,8,9,10] for


num in numbers:

if (num2)==0:

Output:

print (num, 'is an even Number')

2 is an even Number 4 is an even Number6 is an even Number

8 is an even Number

10 is an even NumberProgram 6-9

#Print multiples of 10 for numbers in a given range for num in range(5):

if num > 0:

print (num * 10)

Output:

10

20

30

40#Print first 5 natural numbers using while loop

count = 1

while count <= 5:

print (count)

count += 1

out of the loop fo

some statemen

before continu
the loop. These

can be achieved

and continu

respectively.

these statem

to give more

programmer

flow of execu

Output:

5Program to demonstrate the

#Program 6-12

for num in range (10):

num = num

if num == 8:

break

print('Num has value + str(num))

print('Encountered break!! Out of loop') to

Output:

Num has value 1

Num has value 2

Num has value 3


101

sks

ular

ome

Num has value 4

Num has value 5

Num has value 6

Num has value 7

Encountered break!! Out of loop

Note: When value of num becomes 8, the break statement is executed and the
for loop terminates.

2022-23

use of break statement be republ

in loopPrints values from 0 to 6 except 3 num = 0

for num in range(6):

num num + 1 if num == 3:continue

print('Num has value + str(num))

print('End of loop')

Output:

Num has value 1

Num has value 2

Num has value 4

Num has value 5

Num has value 6

End of loop
ReplyForward
Add reaction

You might also like