Structure
Structure
Types of functions
Advantages of functions
1
The program can be developed in a short period of time.
int arm(int);
void main()
int a;
scanf("%d",&a);
arm(a);
getch();
int arm(int a)
int r,sum=0,t;
t=a;
while(a!=0)
r=a%10;
sum=sum+r*r*r;
a=a/10;
2
if(t==sum)
printf("%d is an armstrong.",t);
else
Output screen :-
3
C program to print whether a number is
positive, negative or zero
#include<stdio.h>
void posit(int);
void main()
int n;
printf("enter a number");
scanf("%d",&n);
posit(n);
void posit(int n)
if(n>0)
else if(n<0)
else
printf("%d is zero",n);
4
Output screen:-
5
C program to swap any two numbers
#include<stdio.h>
#include<string.h>
int main()
int a,b;
scanf("%d%d",&a,&b);
swap(a,b);
return(0);
int temp;
temp=a;
a=b;
b=temp;
6
Output screen:-
7
C program to display 1 5 9 upto nth
term
#include<stdio.h>
void main()
int n;
scanf("%d",&n);
series(n);
void series(int n)
int i,j=1;
for(i=1;i<=n;i++)
printf("%5d",j);
j=j+4;
Output screen:-
8
C program to find the factorial of a
positive integer
#include<stdio.h>
void fact(int);
void main()
int n,f;
scanf("%d",&n);
fact(n);
void fact(int n)
int fact=1;
while(n!=0)
fact=fact*n;
n=n-1;
Output screen:-
9
C program to print the multiplication
table of a given number
10
#include<stdio.h>
int mtable(int);
int main()
int n,j;
printf("Enter a number:");
scanf("%d",&n);
mtable(n);
int mtable(int n)
int j;
for(j=1;j<=10;j++)
printf("\n%d x %d = %d ",n,j,n*j);
Output screen:-
11
#include<stdio.h>
void main()
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
if(prime(i)==1)
printf("%5d",i);
int prime(int n)
int i;
for(i=2;i<n;i++)
if(n%i==0)
break;
if(i==n)
return 1;
else
12
return 0;
Output screen:-
13
C program to calculate y to the power x
#include<stdio.h>
void main()
int x,y;
scanf("%d",&x);
scanf("%d",&y);
int p=1,i;
for(i=1;i<=x;i++)
p=p*y;
return(p);
Output screen:-
14
C program to find the largest and the
smallest number among 10 numbers in
an array
#include<stdio.h>
void array();
void main()
array();
getch();
void array()
int i,j,n[10],temp;
for(i=0;i<10;i++)
scanf("%d",&n[i]);
15
for(i=0;i<9;i++)
for(j=i+1;j<10;j++)
{if(n[i]>n[j])
temp=n[i];
n[i]=n[j];
n[j]=temp;
Output screen:-
16
C program to sort the elements of an
array in ascending order
#include<stdio.h>
void sort();
void main()
sort();
getch();
void sort()
int i,j,a,n[100],temp;
17
printf("Enter number of terms in an array(less than 100) :");
scanf("%d",&a);
for(i=0;i<a;i++)
scanf("%d",&n[i]);
for(i=0;i<a-1;i++)
for(j=i+1;j<a;j++)
{if(n[i]>n[j])
temp=n[i];
n[i]=n[j];
n[j]=temp;
for(i=0;i<a;i++)
printf("%d\t",n[i]);
18
Output screen:-
int fact(int);
void main()
int n,f;
19
printf("Enter a number: ");
scanf("%d",&n);
f=fact(n);
printf("\nfactorial of %d is %d",n,f);
int fact(int n)
if(n<1)
return(1);
else
return(n*fact(n-1));
Output screen:-
int sum(int);
20
void main()
int n,f;
scanf("%d",&n);
f=sum(n);
int sum(int n)
if(n<=0)
return 0;
else
return (n+sum(n-1));
Output screen:-
21
C program to print the fibonacci series
upto nth term
#include<stdio.h>
void main()
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\t",fibo(i));
int fibo(int n)
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fibo(n-1)+fibo(n-2));
Output screen:-
22
Conclusion
This project explored the role of functions in C programming, highlighting
their benefits in improving code modularity, reusability, and
maintainability. We demonstrated how functions break complex problems
into manageable units, enhance code reuse, and simplify updates and
debugging. By addressing key practices and common pitfalls, we
emphasized the importance of proper function use for developing efficient
and scalable programs. The skills and concepts gained are crucial for
tackling more advanced programming challenges. I would like to thank
our computer sir Raja Ram Thapa who gave us this new opportunity to
learn and our lab superviser Om sir who gave us pointers and hint when I
was stuck on a problem.
23
Structure
As we know that an array is the collection of homogeneous data items
that means similar data type such as int or float. It is not possible to hold
different data items with different data types in an array, so structure is
the best solution to hold dissimilar data types as a single unit. It is just like
record in database system. On other hand, it is defined as user defined
data type because users are capable to define their own data type.
Syntax of structure
struct tag_name
data_type member1;
data_type member2;
24
…………..data_type memberN;
25
C program to calculate the sum of time in
hour minutes and seconds
#include<stdio.h>
struct time
int hh;
int mm;
int ss;
}t1,t2,t3;
void main()
int r;
scanf("%d%d%d",&t1.hh,&t1.mm,&t1.ss);
scanf("%d%d%d",&t2.hh,&t2.mm,&t2.ss);
t3.ss=(t1.ss+t2.ss)%60;
t3.mm=((t1.mm+t2.mm)+(t1.ss+t2.ss)/60)%60;
t3.hh=t1.hh+t2.hh+((t1.mm+t2.mm)+(t1.ss+t2.ss)/60)/60;
26
Output screen
27
C program to put information of 10
students and display them
#include<stdio.h>
struct student
int r;
char f[30];
char n[20];
}s[10];
void main()
int i;
for(i=0;i<10;i++)
scanf("%d",&s[i].r);
scanf("%s",s[i].f);
scanf("%s",s[i].n);
printf("\nDisplaying recordes:");
28
for(i=0;i<10;i++)
printf("\n%d\t%s\t%s\t",s[i].r,s[i].f,s[i].n);
Output screen
29
C program to input information and total
marks of 5 student and put them in
descending order
#include<stdio.h>
struct student
int r;
int t;
char f[30];
char n[20];
}s[5];
void main()
int i=0,j=0;
for(i=0;i<5;i++)
scanf("%d",&s[i].r);
scanf("%s",s[i].f);
scanf("%s",s[i].n);
30
scanf("%d",&s[i].t);
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(s[i].t<s[j].t)
temp=s[i];
s[i]=s[j];
s[j]=temp;
for(i=0;i<5;i++)
printf("\n%d\t%s\t%s\t%d",s[i].r,s[i].f,s[i].n,s[i].t);
Output screen
31
C program to input record of employees
and display the records of employee whose
salary ranges from 15000 to 25000
#include<stdio.h>
32
#include<conio.h>
struct employee
int id;
int sal;
char n[20];
}s[100];
void main()
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter id : ");
scanf("%d",&s[i].id);
scanf("%s",s[i].n);
scanf("%d",&s[i].sal);
printf("\nDisplaying records");
for(i=0;i<n;i++)
33
if(s[i].sal>=15000,s[i].sal<=25000)
printf("\n%d\t%s\t%d",s[i].id,s[i].n,s[i].sal);
Output screen
#include<string.h>
typedef struct
34
{
int roll;
char f[30];
char a[20];
student;
void main()
student s[100],temp;
int i,n,j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i].roll);
scanf("%s",s[i].f);
scanf("%s",s[i].a);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
35
{
if(strcmp(s[j].a,s[i].a)>0)
temp=s[i];
s[i]=s[j];
s[j]=temp;
for(i=0;i<n;i++)
printf("\n%d\t%s\t%s",s[i].roll,s[i].f,s[i].a);
Output screen
36
37
C program to input the information of
student and display the record of student
with the highest percentage
#include<stdio.h>
#include<conio.h>
struct employee
int id;
int sal;
char n[20];
char ad[20];
}s[100];
void main()
int i,n;
char j[10]="bagmati";
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter id : ");
scanf("%d",&s[i].id);
scanf("%s",s[i].n);
38
printf("Enter salary : ");
scanf("%d",&s[i].sal);
scanf("%s",s[i].ad);
printf("\nDisplaying records");
for(i=0;i<n;i++)
if(strcmp(s[i].ad,j)==0)
printf("\n%d\t%s\t%d\t%s",s[i].id,s[i].n,s[i].sal,s[i].ad);
Output screen
39
40
C program to input the information of
employees and display the record to
employee whose address is “bagmati”
#include<stdio.h>
#include<conio.h>
struct employee
int id;
int sal;
char n[20];
char ad[20];
}s[100];
void main()
int i,n;
char j[10]="bagmati";
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Enter id : ");
scanf("%d",&s[i].id);
scanf("%s",s[i].n);
41
printf("Enter salary : ");
scanf("%d",&s[i].sal);
scanf("%s",s[i].ad);
printf("\nDisplaying records");
for(i=0;i<n;i++)
if(strcmp(s[i].ad,j)==0)
printf("\n%d\t%s\t%d\t%s",s[i].id,s[i].n,s[i].sal,s[i].ad);
Output screen
42
C program to input details of book like title,
author and price and display the highest
and lowest priced book
#include<stdio.h>
struct book
char title[30];
char author[30];
int price;
}s[5];
void main()
int i,j;
for(i=0;i<5;i++)
scanf("%s",s[i].title);
scanf("%s",s[i].author);
scanf("%d",&s[i].price);
for(i=0;i<4;i++)
43
{
for(j=i+1;j<5;j++)
if(s[i].price<s[j].price)
temp=s[i];
s[i]=s[j];
s[j]=temp;
printf("\n%s\t%s\t%d",s[0].title,s[0].author,s[0].price);
printf("\n%s\t%s\t%d",s[4].title,s[4].author,s[4].price);
}Output screen
44
Pointers
A pointer is a variable that points to another variable which means it
contains the memory address of another variable and is declared as
pointer type (it doesn’t take the value of the variable). As an example of
one variable is data type and second variable is pointer type which points
to the first variable type, then the contents of the second variable is the
address of second variable. A pointer is a special type of variable that
represents memory address of a variable rather than its value.
Syntax
Data_type *Variable_name;
Example
char *p;
float *q;
45
C program to show a simple pointer
program
#include<stdio.h>
void main()
int *p;
int q;
p=&q;
*p=10;
Output screen:-
46
C program to show a simple character
pointer
#include<stdio.h>
void main()
char ch='p';
char *p1,*p2;
p1=&ch;
p2=p1;
Output screen:-
47
C program to calculate sum of two number
using pointer
#include<stdio.h>
void main()
int a,b,c,*x,*y;
scanf("%d%d",&a,&b);
x=&a;
y=&b;
c=*x+*y;
Output screen:-
48
C program to calculate factorial of a
number using pointers
#include<stdio.h>
void main()
int n,i,f=1,*x;
scanf("%d",&n);
x=&n;
for(i=1;i<=*x;i++)
f=f*i;
printf("\nFactorial of %d is %d",n,f);
Output screen:-
49
C program to input 3 number and print the
largest among them using pointer
#include<stdio.h>
int main()
int a[3],i;
int *ptr=a;
int l;
for(i=0;i<3;i++)
scanf("%d",ptr+i);
l=*ptr;
for(i=0;i<3;i++)
if(*(ptr+i)>l)
l=*(ptr+i);
return 0;
50
Output screen:-
File management
51
The term file management in C programming can be defined as the
process of creating, opening, reading, writing and closing files with the
help of C standard library functions. C has a rich set of features for
efficient file management that includes the ability to read, write, and
perform other operations on the files.
52
C program to write and read characters
using putc() and getc()
#include<stdio.h>
void main()
FILE *f;
char ch;
f=fopen("data.txt","w");
while((ch=getchar())!=EOF)
putc(ch,f);
fclose(f);
fopen("data.txt","r");
while((ch=getc(f))!=EOF)
putchar(ch);
fclose(f);
53
Output screen:-
54
C program to write and read integer to/from
file using putw() and getw()
#include<stdio.h>
void main()
FILE *f;
int x,i=0,n;
scanf("%d",&n);
f=fopen("num.txt","w");
printf("Input numbers");
while(i<n)
scanf("%d",&x);
putw(x,f);
i++;
fclose(f);
f=fopen("num.txt","r");
while((x=getw(f))!=EOF)
printf("\n%d",x);
Output screen:-
55
56
C program to write name and roll of 3
student in a file
#include<stdio.h>
void main()
FILE *f;
char n[30];
int r,i=1;
f=fopen("std.txt","w");
while(i<=3)
scanf("%s",n);
scanf("%d",&r);
fprintf(f,"\n%d\t%s",r,n);
i++;
fclose(f);
Ouput screen :-
57
C program to read roll and name of 3
students in a file
#include<stdio.h>
void main()
FILE *f;
char n[30];
int r;
f=fopen("std.txt","r");
while((fscanf(f,"\n%d\t%s",&r,n))!=EOF)
printf("\n%d\t%s",r,n);
fclose(f);
Output screen:-
58
C program to write “Welcome to Nepal”
#include<stdio.h>
void main()
FILE *f;
f=fopen("nepal.txt","w");
fprintf(f,"%s",str);
fclose(f);
Output screen:-
59
C program to rename a file using rename
command
#include<stdio.h>
void main()
FILE *f;
fclose(f);
rename("oldfile.txt","newfile.txt");
Output screen:-
60
C program to add record in std.txt
#include<stdio.h>
void main()
FILE *f;
char n[30];
int r;
f=fopen("std.txt","a");
printf("Enter roll");
scanf("%d",&r);
printf("Enter name");
scanf("%s",n);
fprintf(f,"\n%d\t%s",r,n);
fclose(f);
Output screen :-
61
C program to display appended records of
std.txt
#include<stdio.h>
void main()
FILE *f;
char n[30];
int r;
f=fopen("std.txt","r");
while((fscanf(f,"\n%d\t%s",&r,n))!=EOF)
printf("\n%d\t%s",r,n);
fclose(f);
Ouput screen:-
62
Conclusion
To sum up, the lab on pointers and file management in C has given an
insight in handling files and memory management. Through the exercises,
we have seen how pointers provide better access to memory and how
their use can make the programs much more flexible and efficient.
Likewise, file handling issues have brought up the relevance of file
input/output such as reading a file, writing it, operating file pointers while
considering the file mode and error checks. Linking pointers and file
operations enables the design of efficient and flexible programs that deal
with the computer’s RAM as well as external information repositories. In
general, this lab has improved our grasp of some basic principles of C
programming and it equipped us with the techniques in optimizing the use
of memory and files in real-life application systems.
63
Introduction to javascript
Javascript (JS) is one of the most popular scripting programming language.
It is light weight and mostly used to develop dynamic websites. It is
implemented as client-side script in html to make dynamic pages. It is a
case sensitive interpreted programming language with object– oriented
capabilities. It was developed by Netscape and originally called LiveScript
in 1995. Netscape changed its name to JavaScript language and later it
has been also supported by Internet Explorer and Mozilla Firefox. It is a
general purpose client side scripting language onthe World Wide Web.
Importance of Javascript
64
Javascript program to calculate SI
<html>
<head>
<title>Simple Interest</title>
</head>
<body>
<script type="text/javascript">
var p,t,r,si;
p=parseFloat(prompt("Enter the Principal:"));
t=parseFloat(prompt("Enter the Time(In Year):"));
r=parseFloat(prompt("Enter the rate interest:"));
si=(p*t*r)/100;
document.write("The Simple Interest is="+si);
</script>
</body>
</html>
65
Output screen:-
<html>
<head>
<title>Even Odd</title>
</head>
<body>
<script type="text/javascript">
var n;
n=parseInt(prompt("Enter the number:"))
if(n%2==0)
{
document.write(n+"is a even number.")
}
else
document.write(n+"is an odd number.");
</script>
66
</body>
</html>
Output screen:-
<html>
<head>
<title>Greatest among 3 numbers</title>
</head>
<body>
<script type="text/javascript">
var a=5;
document.write(a+" is the greatest number.");
</script>
</body>
</html>
Output screen:-
67
Javascript program to display even no.
upto 50
<html>
<head>
<title>even number up to 50</title>
</head>
<body>
<script type="text/javascript">
var i;
document.write("The even number are:<br>");
for(i=2;i<=50;i++)
{
if(i%2==0)
document.write(+i);
document.write(" ");
}
68
</script>
</body>
</html>
Output screen:-
<html>
<head>
<title>Factorial</title>
</head>
<body>
<script type="text/javascript">
var n,i,fact=1;
n=parseInt(prompt("Enter the number:"));
if(n<0)
document.write("The factorial is 1.");
else
for(i=1;i<=n;i++)
{
fact=fact*i;
69
}
document.write("The factorial is"+fact);
</script>
</body>
</html>
Output screen:-
<html>
<head>
<title>Factorial</title>
</head>
<body>
<script type="text/javascript">
var n,i,fact=1;
n=parseInt(prompt("Enter the number:"));
if(n<0)
70
document.write("The factorial is 1.");
else
for(i=1;i<=n;i++)
{
fact=fact*i;
}
document.write("The factorial is"+fact);
</script>
</body>
</html>
Output screen:-
71
{
var l=parseInt(prompt("Enter the length of rectangle:"));
var b=parseInt(prompt("Enter the breadth of rectangle:"));
var area=l*b;
document.write("Area of rectangle="+area)
}
area();
</script>
</body>
</html>
Output screen:-
72
r=n%10;
s=(s*10)+r;
n=Math.floor(n/10);
}
document.write("The reversed number is"+s);
</script>
</body>
</html>
Output screen:-
73
s=(s*10)+r;
n=Math.floor(n/10);
}
if(c==s)
{
document.write(c+"is a palindrome number.");
}
else
{
document.write(c+"is not a palindrome number.");
}
</script>
</body>
</html>
Output screen:-
74
Javascript program to find whether given
number is Armstrong or not
<html>
<head>
<title>Armstrong Number</title>
</head>
<body>
<script type="text/javascript">
var n=parseInt(prompt("Enter the number to check:"));
var r,s=0,c;
c=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r)
n=Math.floor(n/10);
}
75
if(s==c)
{
document.write(c+"is an Armstrong number.");
}
else
{
document.write(c+"is not an Armstrong number.");
}
</script>
</body>
</html>
Output screen:-
76
Javascript program for form validation
<html>
<head>
<title>Form validation</title>
<style>
body {
margin:0;
padding:0;
justify-content: center;
align-items: center;
height:100vh;
}
form {
background-color: white;
padding: 20px;
border-radius: 8px;
width:300px;
}
</style>
<script type="text/javascript">
function validate()
{
77
if(document.myForm.name.value=="")
{
alert("Please enter your name!")
document.myForm.name.focus();
return false;
}
if(document.myForm.email.value=="")
{
alert("Please enter your email!")
document.myForm.email.focus();
validateEmail();
return false;
}
if(document.myForm.Zip.value!=5|| isNaN(document.myForm.Zip.value)||
document.myForm.Zip.value=="")
{
alert("Please enter your ZIP code!")
return false;
}
}
</script>
</head>
<body>
<Form action"" name="myForm" onSubmit=validate();>
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<th>Name:</th>
<td><input type="text" name="name"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="email" name="email"></td>
</tr>
78
<tr>
<th>ZIP code:</th>
<td><input type="number" name="Zip"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output screen:-
79
Javascript program to display multiplication
table
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<script type="text/javascript">
var i;
for(i=1;i<=10;i++)
document.write(n+"*"+i+"="+(n*i)+"<br>");
</script>
</body>
</html>
Output screen:-
80
Javascript program to create an object
<html>
<head>
<title>Object</title>
</head>
<body>
<script type="text/javascript">
var student = {
Name: "Prasiddha Budhathoki",
Class: 12,
Roll_no: 26,
Stream: "Science"
};
document.write("Name = " + student.Name + "<br>");
document.write("Class = " + student.Class + "<br>");
document.write("Roll no = " + student.Roll_no + "<br>");
document.write("Stream = " + student.Stream);
</script>
</body>
</html>
Output screen:-
81
Javascript program to find the largest and
smallest among 10 numbers
<html>
<head>
<title>Smallest and Largest</title>
</head>
<script type="text/javascript">
var arr=[];
var smallest, largest;
var i;
for(i=0;i<10;i++)
{
arr[i]=parseInt(prompt("Enter element"+(i+1)));
}
smallest=arr[0];
largest=arr[0];
for(i=1;i<10;i++)
{
if(arr[i]<smallest)
{ smallest=arr[i]; }
else if(arr[i]>largest)
{ largest=arr[i]; } }
document.write("Smallest element="+smallest+"<br>");
document.write("Largest element="+largest+"<br>");
</script>
</body>
</html>
82
Output screen:-
83
Javascript program to add two numbers
using event handling function
<html>
<head><title>Event Handling</title>
</head>
<body>
<script type="text/javascript">
function add()
{
var a=parseInt(prompt("Enter first number="));
var b=parseInt(prompt("Enter second number="));
document.write("Sum of two number="+(a+b));
}
</script>
<p>Click on button below if you want to add two number</p>
<input type="button"onclick="add()"value="Click me">
</body>
</html>
Output screen:-
84
Conclusion
85
In conclusion, this project has explored the extensive capabilities of
JavaScript in enhancing web development, particularly in creating
dynamic, interactive, and user-friendly front-end applications. JavaScript
has proven to be an indispensable tool for developers, enabling the
creation of real-time, responsive web experiences that improve overall
user engagement. Through its integration with HTML and CSS, JavaScript
empowers developers to build interactive elements, manage user input,
and handle asynchronous data with ease. Throughout this project, we
demonstrated how JavaScript’s versatility allows it to handle a wide range
of tasks, from form validation and DOM manipulation to complex
animations and event handling. The introduction of modern JavaScript
features like ES6 syntax, promises, and async/await further enhances its
functionality, making it more efficient and easier to work with. JavaScript's
vast ecosystem of libraries and frameworks, such as React, Angular, and
Vue, has expanded its potential and simplified the development of
complex user interfaces and single-page applications (SPAs). This project
highlights JavaScript's role in enabling seamless user interactions and
improving the overall performance of web applications. In conclusion,
JavaScript continues to be a foundational language for front-end web
development, and its importance is only set to grow as web technologies
advance. With its constant evolution and rich ecosystem, JavaScript is
well-suited to meet the demands of modern web development, making it
a crucial skill for developers.
86
Php pogram to create database
<!DOCTYPE html>
<html lang="en">
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=new mysqli($servername,
$username.$password); if($conn-
>connect_error)
{
die("Connection failed".mysqli_connect_error());
}
else
{
echo"Yes, connected";
else
{
87
echo"Error creating database".$conn->error;
$conn->close();
?>
</body>
</html>
Output:
die("Connection failed".mysqli_connect_error());
}
else
{
echo"Yes, connected";
$conn->close();
?>
</
body>
</
html>
Outp
ut:
89
Php program to create table
<!DOCTYPE html>
<html lang="en">
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="YOURDB";
$conn=new mysqli($servername,$username,
$password,$dbname); if($conn->connect_error)
{
die("Connection failed".mysqli_connect_error());
}
else
{
echo"Yes connected";
}
else
{
90
echo"Error creating table".$conn->error;
}
$conn->close();
?>
</
body>
</
html>
Outp
ut:
91
Php program to insert data in table
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
<bod
y>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="YOURDB";
$conn=new mysqli($servername,$username,
$password,$dbname); if($conn->connect_error)
{
die("Connection failed".mysqli_connect_error());
}
else
{
echo"Yes, connected";
}
92
}
else
{
echo"Error".$conn->error;
}
$conn->close();
?>
Outp
ut:
93
Php program to fetch data from database
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Football Players</title>
<style>
table, th, td {
border: 1px solid
black; border-
collapse: collapse;
}
th, td {
padding:
10px; text-
align: left;
}
</style>
</
head
>
<bod
y>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "YOURDB";
94
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Yes, connected<br>";
}
$sql="SELECT jersey_no FROM FOOTBALL";
$result=$conn-
>query($sql);
if($result-
>num_rows>0)
{
echo"<table><tr><th>jersey_no
</th></tr>"; while($row=$result-
>fetch_assoc())
{
echo"<tr><td>".$row["jersey_no"]."</td></tr>";
echo"</table>";}
}
else
{
echo"Error";
}
$conn->close();
?>
</body>
</html>
Output:
95
Php program to update data
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
<bod
y>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="YOURDB";
$conn=new mysqli($servername,$username,
$password,$dbname); if($conn->connect_error)
{
die("Connection failed.".mysqli_connect_error());
}
else
{
echo"Connected";
}
96
$sql="UPDATE FOOTBALL
SET jersey_no=7
WHERE name='RONALDO'";
if($conn->query($sql)===TRUE)
{
echo"updated succesfully";
}
else
{
echo"failed to update";
}
$conn->close();
?>
</body>
</html>
Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Document</title>
</head>
97
<bod
y>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="YOURDB";
$conn=new mysqli($servername,$username,
$password,$dbname); if($conn->connect_error)
{
die("Connection failed.".mysqli_connect_error());
}
else
{
echo"Connected";
}
echo"deleted succesfully";
}
else
{
echo"failed to delete";
}
$conn->close();
?>
98
</body>
</html>
Output:
Conclusion
In conclusion, this project has focused on the capabilities and applications
of PHP in developing dynamic, server-side web applications. PHP's
flexibility, ease of integration with various databases, and its robust
server-side processing capabilities make it an ideal choice for handling
complex web functionality. Throughout this project, we have
demonstrated how PHP can be used to manage user requests, interact
with databases, and generate dynamic content based on server-side
logic.PHP's widespread adoption and strong community support have
allowed us to build secure, scalable, and efficient web applications. By
99
utilizing key features such as session management, form handling, and
database connectivity, PHP has proven to be a powerful tool for building
web-based systems that can meet both simple and complex application
requirements. This project has also highlighted PHP's compatibility with
other web technologies and its ability to integrate seamlessly with various
front-end tools and frameworks. As web development continues to evolve,
PHP remains a vital skill for developers who seek to build functional and
dynamic web applications. In conclusion, PHP’s versatility and ability to
handle a variety of web-based tasks make it an essential language for
server-side web development, and further exploration into advanced PHP
frameworks and tools can enhance the development of even more
sophisticated web solutions.
100