[go: up one dir, main page]

0% found this document useful (0 votes)
28 views14 pages

Cad22 3 Lab Programs

The document describes programs for: 1) Performing basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers. 2) Printing the Fibonacci sequence up to a given number. 3) Printing numbers from 1 to a given number, replacing multiples of 3 with "Fizz", 5 with "Buzz", and both with "FizzBuzz". 4) Displaying the reverse of a user-input string. 5) Displaying a multiplication table for a user-input number.

Uploaded by

Abi Veera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views14 pages

Cad22 3 Lab Programs

The document describes programs for: 1) Performing basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers. 2) Printing the Fibonacci sequence up to a given number. 3) Printing numbers from 1 to a given number, replacing multiples of 3 with "Fizz", 5 with "Buzz", and both with "FizzBuzz". 4) Displaying the reverse of a user-input string. 5) Displaying a multiplication table for a user-input number.

Uploaded by

Abi Veera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ADDITION, SUBTRACTION, MULTIPLICATION AND DIVISION

1. Write a program for addition, subtraction, multiplication and division of two numbers

ALGORITHM:

STEP 1: Display choice of operation in screen, “Choice 1: Add, 2: Sub, 3: Div, 4: Mul, 5: Quit “.
STEP 2: Receive input choice from the user.
STEP 3: Convert the input to integer (n = int (n1)).
STEP 4: If choice of option is 1 then receive input for A and B, Initiate operation c = A + B.
STEP 5: If choice of option is 2 then receive input for A and B, Initiate operation c = A - B.
STEP 6: If choice of option is 3 then receive input for A and B, Initiate operation c = A / B.
STEP 7: If choice of option is 4 then receive input for A and B, Initiate operation c = A * B.

PROGRAM:

print ("Choice 1: Add, 2: Sub, 3: Div, 4: Mul, 5: Quit")


n1 = input("Enter the choice of operation :")
n = int(n1)
if (n == 1):
print (" You have chosen addition Option :")
a1 = input(" Enter the value for A :")
b1 = input(" Enter the value for B :")
a = int(a1)
b = int(b1)
c=a+b
print ("The Result",a,"+",b,"=",c)
elif (n == 2):
print (" You have chosen subtraction Option :")
a1 = input(" Enter the value for A :")
b1 = input(" Enter the value for B :")
a = int(a1)
b = int(b1)
c=a-b
print ("The Result",a,"-",b,"=",c)
elif (n == 3):
print (" You have chosen division Option :")
a1 = input(" Enter the value for A :")
b1 = input(" Enter the value for B :")
a = int(a1)
b = int(b1)
c=a/b
print ("The Result",a,"/",b,"=",c)
elif (n == 4):
print (" You have chosen multiplication Option :")
a1 = input (" Enter the value for A :")
b1 = input (" Enter the value for B :")
a = int (a1)
b = int (b1)
c=a*b
print ("The Result”, a,"*", b,"=",c)
elif (n==5):
print (“You have given a wrong option”, n)
exit ()
SAMPLE OUTPUT:

If option 1 -> Then Addition

C = A + B --> C = 5 + 5 --> C = 10.

If option 2 -> Then Subtraction

C = A - B --> C = 5 - 2 --> C = 03.

If option 3 -> Then Division

C = A / B --> C = 6 / 2 --> C = 03.

If option 4 -> Then Multiplications

C = A * B --> C = 5 * 3 --> C = 15.

If option 5 -> You have given a wrong option

Exit.
FIBONACCI SERIES

2. Write a program to print Fibonacci number series

ALGORITHM:

“Fibonacci Series is a number series, where each number is a addition of preceding two number” (ie .
if n= 5 then Fibonacci number series are : 0,1,1,2,3.).

STEP 1: Display “ Fibonacci Series “

STEP 2: Prompt the user to enter the number (ie. 1 to n , any integer number).

STEP 3: Since python accept any input in string format, change the input n1 from string to
integer n (ie. n = int(n1)).

STEP 4: If received input is greater than zero then:

Initialize f=0,s=1 ,I =0 and next = 0


(ie. f-means first value , s-means second value & next means next value)

STEP 5: While the value of i is in the range of 0 to n+1 then


Check whether I <=1 : if yes, then
Print the value of I (ie : 0).
If I is not less than 1 then
Next value is addition of f and s.
Swap the value for f and s ie. f = s, s = next
Print the value in next variable
Increment the I = I +1
Break.
STEP 6: If received input is not integer then display “You have entered wrong input”.

PROGRAM:

print (" Fibonacci Series")


n1 = input("Enter the number : ")
n = int(n1)
if (n>0):
f=0
s=1
i=0
next = 0
while (1):
print ("\n\n*******************************************")
for i in range (n+1):
if (i <= 1):
print(i)
else:
next = f + s
f=s
s = next
print (next)
i=i+1
break
else:
print (" You have entered wrong input", n)

SAMPLE OUTPUT:

Fibonacci Series

Enter the number : 5

3
FIZZ BUZZ PROGRAM

3. Write a program to incorporate FIZZ for any number divisible by 3 and Buzz for any
number

divisible for 5 and FIZZBUZZ for any number divisible by 3 and 5 as well.

ALGORITHM:

STEP 1: Print “FizzBuzz Program”

STEP 2: prompt for user input for value N1

STEP 3: Convert the received value N1 to integer value N

STEP 4: Create a loop which will execute from 0 to N+1 times.

If I modules 3 and I modules 5 is zero then

Print I value + “= FIZZBUZZ”

If I modules 3 is zero then

Print I value + “= FIZZ”

If I modules 5 is zero then

Print I value + “= Buzz”

else

Print I Value

PROGRAM:

print ("Fizz Buzz Program :")


n1 = input("Enter the number : ")
n = int(n1)
i=0
for i in range (n+1):
if (i % 3 == 0 and i % 5 == 0):
print (str(i) + "= Fizz Buzz")
elif (i % 3 == 0):
print (str(i) + "= Fizz ")
elif (i % 5 == 0):
print (str(i) + "= Buzz ")
else:
print(i)
OUTPUT:

FizzBuzz Program

Enter the number: 15

3 = FIZZ

5 = BUZZ

9 = FIZZ

10 = BUZZ

11

12 = FIZZ

13

14

15 = FIZZBUZZ

4. Write a Python program to display reverse string.

AIM:
To write a Python program to display reverse string.

ALGORITHM:

STEP1: Display a message to the user to enter the string to reverse

STEP2: Store the string in a variable called string

STEP3: Create an empty variable named string 2

STEP4: Count the characters in the string variable and subtract with -1.
Store
the result in the variable i.

STEP5: While variable “i” is greater than and equal to zero then

string2 = string2 + string[i]

i=i–1

STEP7: Print original string and reversed string

PROGRAM:

# Python Program to Reverse String

string = input("Please enter your own String : ")


string2 = ''

i = len(string) - 1

while(i >= 0):

string2 = string2 + string[i]

i = i - 1

print("\nThe Original String = ", string)

print("The Reversed String = ", string2)

OUTPUT:

Please enter your own String : Python

The Original String = Python

The Reversed String = nohtyP

5. Write a Python program to display a multiplication table

AIM:
To write a Python program to display a multiplication table.

ALGORITHM:

STEP 1: Receive input from user that the number he/she wants to multiply
and store it in a variable m.

STEP 2: Receive input from user that the number times he/she wants to
multiply the number and stored it in variable n.

STEP 3: Initialize i = 1

STEP 4: Create for loop to execute the given statements n times


Multiply m * i and store the result in variable c (i.e , c= m* i)
Print the result on the screen.
Increment i with 1
Repeat the loop till i reach to the value stored in variable n.

FLOW CHART:

START

Receive input from user that the number he/she wants to multiply and store it in a variable
m.
Receive input from user that the number times he/she wants to multiply the number and
stored it in variable n.
Initialize i = 1

For i in range (1, n+1) NO

YES

Multiply m * i and store the result in variable c (i.e , c= m* i)


Print the result on the screen.
Increment i with 1
Repeat the loop till i reach to the value stored in variable n.

STOP

PROGRAM:

print ("\n\t\t MULTIPLICATION TABLE");

print ("\t\t**********************");

m=int(input("Enter a number to multiply:"))

n=int(input("Enter the number of times to multiply the


number :"))

print ("\n\n");

print ("\t Multiplication Table for ",m,",",n,"times")

i = 1

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


c = m * i

print ("\t\t",m,"x",i,"=",c);

i = i+1

OUTPUT:
MULTIPLICATION TABLE

**********************

Enter a number to multiply:7

Enter the number of times to multiply the number :20

Multiplication Table for 7 , 20 times

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
7 x 13 = 91
7 x 14 = 98
7 x 15 = 105
7 x 16 = 112
7 x 17 = 119
7 x 18 = 126
7 x 19 = 133
7 x 20 = 140
6. Write a Python program to display all prime numbers between 1 to 1000.

AIM:
To write a Python program to display all prime numbers between 1 to 1000.
ALGORITHM:

STEP 1: Receive input for lower range value from user

STEP 2: Receive input for upper range value from user

STEP 3: Create outer for loop to i value execute from lower range to upper
range value times

STEP 4: Initialize count variable to zero

STEP 5: Create a inner for loop to j value to execute from 1 to i+1 times

STEP 6: Increment count variable to 1 if (i%j) == 0

STEP 7: If count variable have value 2 then print i as prime number

FLOW CHART:
START

Receive input for lower range value from user


Receive input for upper range value from user

for i in range NO
(lower,upper + 1)
YES

count = 0

for j in range (1,i+1): NO

YES
NO if (i % j) == 0:

YES
count = count + 1

NO if (count == 2):

YES

print(i)

STOP

PROGRAM:

lower = int(input("Enter lower range: "))

upper = int(input("Enter upper range: "))


for i in range(lower,upper + 1):

count = 0

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

if (i % j) == 0:

count = count + 1

if (count == 2):

print(i)

OUTPUT:

Enter lower range: 1

Enter upper range: 1000


2 47 … …
3 53 … 977
5 59 … 983
7 61 … 991
11 67 … 997
13 71 …
17 73 …
19 79 …
23 83 …
29 89 …
31 97 …
37 … …
41 … …
43 … …

7. Write a Python program to demonstrate the Array Operations and Methods.

You might also like