Simple Arithmetic Calculator Aim: Algorithm
Simple Arithmetic Calculator Aim: Algorithm
AIM :
To write a program to make a simple Arithmetic Calculator
ALGORITHM :
Step 1 : GET the option from the user for + , - , * , / , // , %
Step 2 : Perform arithmetic operation based on user choice
Step 3 : Display the output
CODING :
# define functions
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def floordiv(x, y):
return x // y
def moddiv(x, y):
return x % y
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Floor Division")
print("6.Modulo Division")
choice = input("Enter choice(1/2/3/4/5/6):")
while choice<'7':
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
elif choice=='5':
print(num1,"//",num2,"=",floordiv(num1,num2))
elif choice=='6':
print(num1,"%",num2,"=",moddiv(num1,num2))
choice = input("Enter choice(1/2/3/4/5/6):")
print("Exited")
OUTPUT:
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
5.Floor Division
6.Modulo Division
Enter choice(1/2/3/4/5/6):1
Enter first number: 10
Enter second number: 20
10 + 20 = 30
Enter choice(1/2/3/4/5/6):2
Enter first number: 30
Enter second number: 15
30 - 15 = 15
Enter choice(1/2/3/4/5/6):3
Enter first number: 30
Enter second number: 10
30 * 10 = 300
Enter choice(1/2/3/4/5/6):4
Enter first number: 30
Enter second number: 4
30 / 4 = 7.5
Enter choice(1/2/3/4/5/6):5
Enter first number: 30
Enter second number: 4
30 // 4 = 7
Enter choice(1/2/3/4/5/6):6
Enter first number: 30
Enter second number: 4
30 % 4 = 2
Enter choice(1/2/3/4/5/6):7
Exited
>>>
RESULT :
Thus the program has been executed successfully for designing a simple arithmetic
calculator and output was verified
SQL Commands:
mysql> create database lab;
Query Ok
mysql>use lab;
database changed
a) mysql> create table student(name char(20),dept char(10), Admn date, age
integer,fee integer, gender integer);
Query Ok
b) mysql> insert into student
values(1,’Rakesh’,’computer’,’2000/02/20’,23,4000,’m’);
Query Ok
mysql> insert into student values(2,’jeni’,’history’,’1998/01/10’,22,2500,’m’);
Query Ok
mysql> insert into student values(3,’asha,’tamil’,’1996/12/12’,20,2000,’f’);
Query Ok
mysql> insert into student values(4,’Zulu’,’computer’,’2000/09/05’,19,4500,’m’);
Query Ok
mysql> insert into student values(5,’mala’,’history’,’1998/02/02’,25,3000,’f’);
Query Ok
mysql> insert into student values(6,’maria’,’tamil’,’2000/07/03’,22,3075,’f’);
Query Ok
c) mysql>alter table student add fathername char(10);
Query Ok
d) mysql> alter table student modify gender char(7);
Query Ok
e) mysql> alter table student drop column fathername;
Query Ok
f) mysql> update student set dept=’english’ where name=’asha’;
Query Ok
g) mysql> select * from student order by admn;
j)
i. mysql>select count(distinct dept) from student;
ii. mysql>select max(age) from student where gender=’f’;
Result:
Thus the SQL commands are verified and executed successfully.
SQL COMMANDS FOR THE RELATIONS STATIONARY AND CONSUMER
AIM:
To write SQL commands for the following on the basis of given relations.
Table: Stationary
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel Pen ABC 15
Table: Consumer
C_ID ConsumerName Address S_ID
01 Good Learner Delhi PL00
06 Write Well Mumbai GP02
12 Topper Delhi DP01
15 Write & Draw Delhi PL02
16 Motivation Bangalore PL01
a) To display the details of those consumers whose Address is Delhi.
b) To display the details of Stationary whose Price is in the range of 8 to 15.
c) To display the ConsumerName, Address from Table consumer, and Company and price
from table stationary ,with their corresponding matching S_ID.
d) To increase the Price of all stationary by 2.
e) Select Distinct Address from Consumer.
f) Select company MAX(price),MIN(price),COUNT(*) from stationary GROUPBY
company.
g) Select Consumer. ConsumerName, Stationary. StationaryName , Stationary. Price from
stationary,Consumer where Consumer.S_ID =Stationary.S_ID
h) Select Stationary Name, Price*3 from Stationary
SQL Commands:
mysql>use lab;
database changed
mysql>create table stationary(s_id varchar(20),stationaryname char(20),company char(10),price
integer);
Query Ok
mysql>create table consumer(c_id integer,consumername char(20),address char(10),s_id
varchar(10);
Query Ok
mysql>insert into stationary values(‘DP01’,’ DotPen’,’ ABC’,10);
Query Ok
mysql>insert into stationary values(‘PL02’,’ Pencil’,’ XYZ’,6);
Query Ok
mysql>insert into stationary values(‘ER05’,’Eraser’,’ XYZ’,7);
Query Ok
mysql>insert into stationary values(‘PL01’,’Pencil’,’ CAM’,5);
Query Ok
mysql>insert into stationary values(‘GP02’,’ GelPen’,’ ABC’,15);
Query Ok
mysql>insert into consumer values(01,’ goodlearner’,’ delhi’,’pl00’);
Query Ok
mysql>insert into consumer values(06,’writewell’,’mumbai’,’gp02’);
Query Ok
mysql>insert into consumer values(12,’topper’,’ delhi’,’dp0l’);
Query Ok
mysql>insert into consumer values(15,’write&draw’,’ delhi’,’pl02’);
Query Ok
mysql>insert into consumer values(16,’motivation’,’ bangalore’,’pl01’);
Query Ok
Result:
Thus the SQL commands are verified and executed successfully.
Table: Salary
ID BASIC ALLOWANCE CONSULTATION
i) Display name of all medicine specialists having more than 10 years of experience from
the table Doctor.
ii) Display the name of the Doctors who is having first letter as ‘L’.
iii) Display the average salary of all doctors working in ENT department using the tables
Doctor and Salary( salary=Basic + Allowance)
iv) Display the minimum allowance of female doctors.
v) Display the name of male doctors having highest consultation fee.
vi) Select count(*) from Doctor where sex=’F’;
vii) Select Name, Dept, Basic from Doctor,Salary where dept=’ENT’ and
Doctor.ID=Salary.ID;
SQL COMMANDS:
mysql> use lab;
Database changed
mysql> create table doctor (id integer,name char(20),dept char(20),sex char(10),experience
integer);
Query OK
mysql> create table salary(id integer,basic integer,allowance integer,consultation integer);
Query OK
mysql> insert into doctor values(101,'john','ent','m',12);
Query OK
mysql> insert into doctor values( 104,'smith','ortho','m',5);
Query OK
ii) mysql> select id,name,experience from doctor where name like 'l%';
Result:
Thus the SQL commands are verified and executed successfully.
TABLE: FARE
FL_NO AIRLINES FARE TAX%
IC701 INDIAN AIRLINES 6500 10
MU499 SAHARA 9400 5
AM501 JET AIRWAYS 13450 8
IC899 INDIAN AIRLINES 8300 4
IC302 INDIAN AIRLINES 4300 10
IC799 INDIAN AIRLINES 10500 10
MC101 DECCAN AIRLINES 3500 4
SQL COMMANDS:
mysql> create database lab;
mysql> use lab;
mysql> create table flights(fl_no varchar(20),startingflight varchar(30),ending
varchar(30),no_flight int,no_stops int);
Query OK
vi.
Result:
Thus the SQL commands are verified and executed successfully.
AIM:
To connect with database and create database and create table and insert value in the table.
ALGORITHM:
Step 1: Create the connector object sqltor for the class mysql connector.
Step 2: Create Connection Object mycon using the connector Object sqltor.
Step 3: Create Cursor object ‘cursor’ through mycon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
Coding:
import mysql.connector as sqltor
con=sqltor.connect(host='localhost',user='root',password='admin')
if con.is_connected()==False:
print("error connecting to MySQL database")
cursor=con.cursor()
cursor.execute("create database lab3")
cursor.execute("use lab3")
cursor.execute("create table emp(name varchar(20))")
for i in range(5):
name=input("enter the name")
st="insert into emp(name) values('{}')".format(name)
cursor.execute(st)
con.commit()
con.close()
output:
enter the name alma
enter the name jerry
enter the name riya
enter the name kala
enter the name Fatima
RESULT :
Thus the program is executed successfully for connecting with database.
CODING:
import mysql.connector as sqltor
con=sqltor.connect(host='localhost',user='root',password='admin')
if con.is_connected()==False:
print("error connecting to MySQL database")
cursor=con.cursor()
cursor.execute("create database lab5")
cursor.execute("use lab5")
cursor.execute("create table emp(name varchar(20))")
for i in range(3):
name=input("enter the name")
st="insert into emp(name) values('{}')".format(name)
cursor.execute(st)
con.commit()
cursor.execute("select * from emp")
data=cursor.fetchmany(3)
count=cursor.rowcount
for row in data:
print(row)
con.close()
OUTPUT:
enter the name ALMA
enter the name ANNIE
enter the name RIMO
(' ALMA',)
(' ANNIE',)
(' RIMO',)
RESULT :
Thus the program is executed successfully for connecting with database.
INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO
DELETE VALUE FROM THE TABLE
AIM:
To connect with database and delete the record from the table.
ALGORITHM:
Step 1: Create the connector object sqltor for the class mysql connector.
Step 2: Create Connection Object mycon using the connector Object sqltor.
Step 3: Create Cursor object ‘cursor’ through mycon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
CODINGS:
import mysql.connector as sqltor
con=sqltor.connect(host='localhost',user='root',password='admin')
if con.is_connected()==False:
print("error connecting to MySQL database")
cursor=con.cursor()
cursor.execute("create database lab5")
cursor.execute("use lab5")
cursor.execute("create table emp(name varchar(20))")
for i in range(3):
name=input("enter the name")
st="insert into emp(name) values('{}')".format(name)
cursor.execute(st)
con.commit()
cursor.execute("delete from emp where name='alma' ")
con.commit()
con.close()
OUTPUT:
enter the name alma
enter the name annie
enter the name rimo
RESULT :
Thus the program is executed successfully for connecting with database.
AIM:
To connect with database and update the record of the student.
ALGORITHM:
Step 1: Create the connector object sqltor for the class mysql connector.
Step 2: Create Connection Object mycon using the connector Object sqltor.
Step 3: Create Cursor object ‘cursor’ through mycon.
Step 4: Execute the query using cursor.execute()
Step 5: Close the connection object as mycon.close()
CODING:
import mysql.connector as sqltor
con=sqltor.connect(host='localhost',user='root',password='admin',database='student')
cursor=con.cursor()
cursor.execute("update student set mark1=90 where name='raja' ")
con.commit()
print(cursor.rowcount,"record(s) affected")
con.close()
OUTPUT:
1 record(s) affected
RESULT :
Thus the program is executed successfully for connecting with database.