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

Simple Arithmetic Calculator Aim: Algorithm

The document describes SQL commands to perform operations on two tables - Stationary and Consumer. The commands include creating the tables, inserting data, selecting data based on conditions like address, price range, grouping, joining tables and performing aggregation. Operations like updating price, deleting rows, altering tables are also demonstrated. The SQL commands are executed successfully to retrieve, manipulate and manage data from the tables.

Uploaded by

moses
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Simple Arithmetic Calculator Aim: Algorithm

The document describes SQL commands to perform operations on two tables - Stationary and Consumer. The commands include creating the tables, inserting data, selecting data based on conditions like address, price range, grouping, joining tables and performing aggregation. Operations like updating price, deleting rows, altering tables are also demonstrated. The SQL commands are executed successfully to retrieve, manipulate and manage data from the tables.

Uploaded by

moses
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SIMPLE ARITHMETIC CALCULATOR

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 FOR THE RELATION STUDENT


Aim:
To write SQL commands for the following on the basis of given table student.
STUDENT
No. Name Dept Admn Age Fee Gender
1 Rakesh Computer 20/02/2000 23 4000 M
2 Jeni History 10/01/1998 22 2500 M
3 Asha Tamil 12/12/1996 20 2000 F
4 Zulu Computer 05/09/2000 19 4500 M
5 Mala History 02/02/1998 25 3000 F
6 Maria Tamil 03/07/2000 22 3075 F
a) Create table STUDENT , with the following fields.
Field Type
No Integer
Name Char(20)
Dept Char(10)
Admn Date
Age Integer
Fee Integer
Gender Char(2)
b) Insert data in the table STUDENT.
c) Alter the table STUDENT with the new attribute, father’s name with the type of Char(10).
d) Modify the attribute, gender with type of Char(7) in the table STUDENT.
e) Drop the attribute father’s name from the table STUDENT.
f) Update the table STUDENT, and modify the data.
g) Use Order by to display data in ascending / descending order
h) Use delete command to remove the last field from the student table.
i) Group the table STUDENT by dept.
j) Give the Output of following:
i. Select COUNT(distinct dept) from student.
ii. Select MAX(age) from the student where Gender=’F’.
iii. Select MIN(age) from the student where Gender=’M’.
iv. Select AVG(fee) from the student where Admn=’01/01/2000’.
v. Select SUM(age) from the student where Admn>’05/09/2000’.

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;

mysql> select * from student order by name desc;

h) mysql> delete from student where no=6;


Query Ok
i) mysql> select name,admn from student group by dept;

j)
i. mysql>select count(distinct dept) from student;
ii. mysql>select max(age) from student where gender=’f’;

iii. mysql> select min(age),name from student where gender=’m’;

iv. mysql>select avg(fee) from student where admn=’01/01/2000’;

v. mysql>select sum(fee) from student where admn>’05/09/2000;

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

i. mysql> select *from consumer where address=’delhi’;

ii. mysql>select * from stationary where price between 8 and 15;

iii. mysql> select consumername,address,company,price from consumer,stationary where


consumer.s_id=stationary.s_id;
iv. mysql>update stationary set price=price+2;
Query Ok
v. mysql>select distinct address from consumer;

vi. mysql>select company,max(price),min(price),count(*) from stationary group by


company;

vii. mysql>select consumer.consumername,stationary.stationaryname,stationary.price from


stationary,consumer where consumer.s_id=stationary.s_id;

viii. mysql>select stationaryname,price*3 from stationary;

Result:
Thus the SQL commands are verified and executed successfully.

SQL COMMANDS FOR THE RELATIONS DOCTOR AND SALARY


Aim:
To create the following relations DOCTOR and SALARY. On the basis of these two
tables, write SQL commands for (i) to (v) and show the outputs for the command (vi) and (vii).
Table : Doctor
ID NAME DEPT SEX EXPERIENCE

101 John ENT M 12

104 Smith ORTHO M 5

107 George Cardio M 10

114 Lara Skin F 3

109 K George Medicine F 9

105 Johnson Ortho M 10

117 Lucy ENT F 3


111 Bill Medicine F 12

130 Morphy Ortho M 15

Table: Salary
ID BASIC ALLOWANCE CONSULTATION

101 12000 1000 300

104 23000 2300 500

107 32000 4000 500

114 12000 5200 100

109 42000 1700 200

105 18900 1690 300

130 21700 2600 300

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

mysql> insert into doctor values(107,'george','cardio','m',10);


Query OK
mysql> insert into doctor values(114,'lara','skin','f',3);
Query OK
mysql> insert into doctor values(109,'K George','Medicine','F',9);
Query OK
mysql> insert into doctor values(105,'Johnson','Ortho','M',10);
Query OK
mysql> insert into doctor values(117,'Lucy','ENT','F',3);
Query OK
mysql> insert into doctor values(111,'Bill','Medicine','f',12);
Query OK
mysql> insert into doctor values(130,'Morphy','Ortho','M',15);
Query OK
mysql> insert into salary values(101,12000,1000,300);
Query OK
mysql> insert into salary values(104,23000,2300,500);
Query OK
mysql> insert into salary values(107,32000,4000,500);
Query OK
mysql> insert into salary values(114,12000,5200,100);
Query OK
mysql> insert into salary values(109,42000,1700,200);
Query OK
mysql> insert into salary values(105,18900,1690,300);
Query OK
mysql> insert into salary values(130,21700,2600,300);
Query OK
i) mysql> select name from doctor where dept='medicine' and experience>10;

ii) mysql> select id,name,experience from doctor where name like 'l%';

iii) mysql> select avg(basic+allowance) from doctor,salary where dept='ent' and


doctor.id=salary.id;

iv) mysql> select min(allowance) from doctor,salary where sex='f' and


doctor.id=salary.id;

v) mysql> select max(consultation) from doctor,salary where sex='m' and


doctor.id=salary.id;

vi) mysql> select count(*) from doctor where sex='f';

vii) mysql> select name,dept,basic from doctor,salary where dept='ent' and


doctor.id=salary.id;

Result:
Thus the SQL commands are verified and executed successfully.

SQL COMMANDS FOR THE RELATIONS FLIGHTS AND FARES


Aim:
To create the following relations FLIGHTS and FARES. On the basis of these two tables,
write SQL commands for (i) to (v) and show the outputs for the command (vi).
TABLE:FLIGHT

FL_NO STARTINGFLIGHT ENDING NO_FLIGHT NO_STOPS


IC301 MUMBAI DELHI 8 0
C799I BANGALORE DELHI 2 1
MC101 INDORE MUMBAI 3 0
IC302 DELHI MUMBAI 8 0
AM812 KANPUR BANGLORE 3 1
IC899 MUMBAI KOCHI 1 4
AM501 DELHI TRIVENDRUM 1 5
MU499 MUMBAI MADRAS 3 3
IC701 DELHI AHMEDABAD 4 0

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

i) Display FL_NO and NO_FLIGHTS from “KANPUR” TO “BANGALORE” from the


table FLIGHTS.
ii) Arrange the contents of the table FLIGHTS in the ascending order of FL_NO.
iii) Display the FL_NO and fare to be paid for the flights from DELHI to MUMBAI using
the tables FLIGHTS and FARES, where the fare to be paid = FARE+FARE+TAX%/100.
iv) Display the minimum fare “Indian Airlines” is offering from the tables FARES.
v) To display the detail fares of Indian airlines.
vi) Select FL_NO,NO_FLIGHTS,AIRLINES from FLIGHTS, FARES Where STARTING =
“DELHI” AND FLIGHTS.FL_NO = FARES.FL_NO

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

mysql> INSERT INTO FLIGHTS VALUES('IC301','MUMBAI','DELHI',8,0);


Query OK
mysql> INSERT INTO FLIGHTS VALUES('C799I','BANGALORE','DELHI',2,1);
Query OK
mysql> INSERT INTO FLIGHTS VALUES('MC101','INDORE','MUMBAI',3,0);
Query OK
mysql> INSERT INTO FLIGHTS VALUES('IC302','DELHI','MUMBAI',8, 0 );
Query OK
mysql> INSERT INTO FLIGHTS VALUES('AM812','KANPUR','BANGLORE',3, 1 );
Query OK
mysql> INSERT INTO FLIGHTS VALUES('IC899','MUMBAI','KOCHI', 1, 4);
Query OK
mysql> INSERT INTO FLIGHTS VALUES('AM501','DELHI','TRIVENDRUM', 1, 5);
Query OK
mysql> INSERT INTO FLIGHTS VALUES('MU499','MUMBAI','MADRAS',3,3 );
Query OK
mysql> INSERT INTO FLIGHTS VALUES('IC701','DELHI','AHMEDABAD',4,0 );
Query OK

mysql> CREATE TABLE FARES(FL_NO VARCHAR(20),AIRLINES


VARCHAR(30),FARE INT,TAX INT);
Query OK
mysql> INSERT INTO FARES VALUE('IC701','INDIAN AIRLINES',6500,10);
Query OK
mysql> INSERT INTO FARES VALUE('MU499','SAHARA',9400,5);
Query OK
mysql> INSERT INTO FARES VALUE('AM501','JET AIRWAYS',13450,8);
Query OK
mysql> INSERT INTO FARES VALUE('IC899','INDIAN AIRLINES',8300,4);
Query OK
mysql> INSERT INTO FARES VALUE('IC302','INDIAN AIRLINES',4300,10);
Query OK
mysql> INSERT INTO FARES VALUE('IC799','INDIAN AIRLINES',10500,10);
Query OK
mysql> INSERT INTO FARES VALUE('MC101','DECCAN AIRLINES',3500,4);
Query OK
i. mysql> Select FL_NO, NO_FLIGHTS from FLIGHTS where
Startingflight="KANPUR" AND ENDING="BANGLORE" ;

ii. mysql> select * from flights order by fl_no;

iii. mysql> Select flights.fl_no, FARE+FARE+(TAX/100) from FLIGHTS, FARES where


Startingflight="DELHI" AND Ending="MUMBAI;

iv. mysql> Select min(FARE) from FARES Where AIRLINES="Indian Airlines" ;


v. mysql> select * from fares where airlines="indian airlines";

vi.

Result:
Thus the SQL commands are verified and executed successfully.

10. INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO


CREATE DATABASE,TABLE AND INSERT VALUE IN THE TABLE

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.

INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO


SELECT VALUE FROM THE TABLE
AIM:
To connect with database and select first three 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')
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.

11. INTEGRATE SQL WITH PYTHON BY IMPORTING THE MYSQL MODULE TO


UPDATE VALUE FROM THE TABLE

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.

You might also like