KEMBAR78
Structure | PDF | Dynamic Web Page | Java Script
0% found this document useful (0 votes)
8 views100 pages

Structure

The document provides an overview of functions in C programming, including their types (library and user-defined), advantages, and examples of various C programs demonstrating function usage. It also discusses structures as a way to handle heterogeneous data types and provides examples of programs using structures for distance, time, and student records. The conclusion emphasizes the importance of functions and structures in developing efficient and maintainable code.

Uploaded by

legendgamer2987
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)
8 views100 pages

Structure

The document provides an overview of functions in C programming, including their types (library and user-defined), advantages, and examples of various C programs demonstrating function usage. It also discusses structures as a way to handle heterogeneous data types and provides examples of programs using structures for distance, time, and student records. The conclusion emphasizes the importance of functions and structures in developing efficient and maintainable code.

Uploaded by

legendgamer2987
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/ 100

Function

A function is a self-contained program, which means to do some specific,


well defined task. A C program consists of one or more functions.
Execution of every C program always begins with main(). Additional
functions will be subordinate to main, and perhaps to one another. After
each function has done its operation, control returns back to the main().
Then remaining statements of main () are executed. If a program contains
multiple functions their definitions may appear in any order, though they
must be bi-dependent of one another.

Types of functions

 Library (built-in functions): C has the facility to provide some


library functions to the programmer for doing some operations. As
an example C has a mathematical function which is used for finding
out the square root of any number, string library functions are used
for string operations and etc. these operations are programmed and
stored as library functions so that they can be called by any
program.
 User-defined functions: Library functions provide the facility for
doing some predefined tasks. User can also create functions for
doing any specific tasks of the program. Such functions are called
user-defined functions. In other words. User defined functions are
the functions which are created by the programmer according to the
need.

Advantages of functions

 Function increases code reusability.


 The length of the source program can be reduced by using function
at appropriate places.
 The program development will be faster.
 The program debugging will be easier.
 Large number of programmers can be involved.

1
 The program can be developed in a short period of time.

C program to find whether a number iis


Armstrong or not
#include<stdio.h>

int arm(int);

void main()

int a;

printf("\nEnter a three digit number:");

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

printf("%d is not an armstrong.",t);

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)

printf("\n%d is positive number",n);

else if(n<0)

printf("\n%d is negative number");

else

printf("%d is zero",n);

4
Output screen:-

5
C program to swap any two numbers
#include<stdio.h>

#include<string.h>

void swap(int a,int b);

int main()

int a,b;

printf("\nenter the two numbers:");

scanf("%d%d",&a,&b);

printf("\nbefore swapping a=%d and b=%d",a,b);

swap(a,b);

return(0);

void swap(int a,int b)

int temp;

temp=a;

a=b;

b=temp;

printf("\nafter swapping a=%d and b=%d",a,b);

6
Output screen:-

7
C program to display 1 5 9 upto nth
term
#include<stdio.h>

void series(int n);

void main()

int n;

printf("Enter number of terms: ");

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;

printf("Enter a number: ");

scanf("%d",&n);

fact(n);

void fact(int n)

int fact=1;

while(n!=0)

fact=fact*n;

n=n-1;

printf("The factorial of the given number is %d",fact);

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:-

C program to display prime number


upto nth number

11
#include<stdio.h>

int prime(int n);

void main()

int n,i;

printf("Enter number prime :");

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>

int power(int x,int y);

void main()

int x,y;

printf("Enter value of x:");

scanf("%d",&x);

printf("\nThe value of y:");

scanf("%d",&y);

printf("\nThe result is %d",power(x,y));

int power(int x,int 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++)

printf("Enter array elements:");

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;

printf("%d is the largest among the numbers",n[9]);

printf("\n%d is the smallest among the numbers",n[0]);

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++)

printf("Enter array elements:");

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;

printf("\nThe sorted terms of the array are : \n");

for(i=0;i<a;i++)

printf("%d\t",n[i]);

18
Output screen:-

C program to print the factorial of a


number using recursive function
#include<stdio.h>

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:-

C program to find the sum of n natural


numbers
#include<stdio.h>

int sum(int);

20
void main()

int n,f;

printf("Enter number of natural numbers :");

scanf("%d",&n);

f=sum(n);

printf("\nThe sum of numbers is %d",f);

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>

int fibo(int n);

void main()

int i,n;

printf("Enter a number: ");

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.

Structure is collection of heterogeneous data items treated as a single


unit. Each data items is called member of structure. The keywordstruct is
used to decleare structure variable and type of structure variable is
defined by tag_name of the structure. The general syntax to define
structure variable is given below.

Some major features of structure

 It can be treated as record that is collection of interrelated data


fields having different data types.
 It is possible to copy one structure variable to another by simply
assignment operator(=).
 It allows nested structure that is one structure inside another
structure.
 It is possible to pass structure variable as parameter to any
function.
 It is possible to define structure pointer also known as linked list.

Syntax of structure

struct tag_name

data_type member1;

data_type member2;

24
…………..data_type memberN;

struct tag_name var1,var2,var3……..varN;

C program to calculate sum of two distance


in feet and inch.
#include<stdio.h>
typedef struct
{
int in;
int fe;
}distance;
void main()
{
distance d1,d2,d;
printf("Enter distance in feet and inch :");
scanf("%d%d",&d1.fe,&d1.in);
printf("Enter 2nd distance in inch :");
scanf("%d%d",&d2.fe,&d2.in);
d.in=(d1.in+d2.in)%12;
d.fe=(d1.fe+d2.fe)+(d1.in+d2.in)/12;
printf("\nThe sum of distance is %d feet %d inch",d.fe,d.in);
}
Output screen

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;

printf("Enter time in hour minutes and seconds : ");

scanf("%d%d%d",&t1.hh,&t1.mm,&t1.ss);

printf("Enter 2nd time in hour minutes and seconds : ");

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;

printf("\nThe sum is %d hour %d minutes %d


seconds",t3.hh,t3.mm,t3.ss);

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++)

printf("Enter roll no : ");

scanf("%d",&s[i].r);

printf("Enter first name : ");

scanf("%s",s[i].f);

printf("Enter last name : ");

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()

struct student temp;

int i=0,j=0;

for(i=0;i<5;i++)

printf("Enter roll no : ");

scanf("%d",&s[i].r);

printf("Enter first name : ");

scanf("%s",s[i].f);

printf("Enter last name : ");

scanf("%s",s[i].n);

printf("Enter total marks : ");

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;

printf("\nDisplaying records : ");

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;

printf("\nEnter number of records not more than 100 : ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter id : ");

scanf("%d",&s[i].id);

printf("Enter name : ");

scanf("%s",s[i].n);

printf("Enter salary : ");

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

C program to input information of students


and put them in ascending order according
to their address
#include<stdio.h>

#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;

printf("Enter number of students not more than 100 : ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter roll no : ");

scanf("%d",&s[i].roll);

printf("Enter name : ");

scanf("%s",s[i].f);

printf("Enter adress : ");

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;

printf("\nDisplaying records : ");

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";

printf("\nEnter number of records not more than 100 : ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter id : ");

scanf("%d",&s[i].id);

printf("Enter name : ");

scanf("%s",s[i].n);

38
printf("Enter salary : ");

scanf("%d",&s[i].sal);

printf("Enter adress : ");

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";

printf("\nEnter number of records not more than 100 : ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter id : ");

scanf("%d",&s[i].id);

printf("Enter name : ");

scanf("%s",s[i].n);

41
printf("Enter salary : ");

scanf("%d",&s[i].sal);

printf("Enter adress : ");

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()

struct book temp;

int i,j;

for(i=0;i<5;i++)

printf("Enter title of book: ");

scanf("%s",s[i].title);

printf("Enter author of book: ");

scanf("%s",s[i].author);

printf("Enter price of book: ");

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("The highest priced book is: ");

printf("\n%s\t%s\t%d",s[0].title,s[0].author,s[0].price);

printf("\nThe lowest priced book is: ");

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.

Some major features of pointers

 Pointer saves memory space


 Pointer assigns memory space and also releases it which makes the
best use of available memory (dynamic memory allocations)
 The execution time is faster because data manipulation is done
directly in memory.
 Pointer is closed to array so it is efficient for solving arrayand string
related problem’s
 Pointer is closed to hardware so it si very efficient for solving
hardware interfacing problem.

Syntax

Data_type *Variable_name;

Example

char *p;

float *q;

Where ‘p’ is a variable which contains address of a string type variable


and ‘q’ contains address of float type variable

45
C program to show a simple pointer
program
#include<stdio.h>

void main()

int *p;

int q;

p=&q;

*p=10;

printf("The value of q is %d",*p);

printf("\nThe address is q is %d",p);

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;

printf("*p1=%c and *p2= %c",*p1,*p2);

Output screen:-

47
C program to calculate sum of two number
using pointer
#include<stdio.h>

void main()

int a,b,c,*x,*y;

printf("Enter any two numbers : ");

scanf("%d%d",&a,&b);

x=&a;

y=&b;

c=*x+*y;

printf("\nThe sum is %d",c);

Output screen:-

48
C program to calculate factorial of a
number using pointers
#include<stdio.h>

void main()

int n,i,f=1,*x;

printf("Enter number : ");

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;

printf("Enter any three numbers :\n");

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);

printf("The largest number is %d ",l);

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.

A data file is defined as the collection of data or information which is


permanently stored inside secondary memory as a single unit. We can
read, write, append, and delete data in the data file as per requirements

File manipulation function:

 fopen( ) :- To create a new file


 fclose( ) :- To close data file
 getc( ) :- To read a character from data file
 putc( ) :- To write a character from data file
 getw( ) :- To read an integer from data file
 putw( ) :- To write an integer form data file
 fscanf( ) :- To read formulated data from data file
 fprintf( ) :- To write formulated data in data file
 fread( ) :- To read record form data file
 fwrite( ) :- to write record from data file

52
C program to write and read characters
using putc() and getc()
#include<stdio.h>

void main()

FILE *f;

char ch;

printf("\nInput data( to exit press ctrl+z): ");

f=fopen("data.txt","w");

while((ch=getchar())!=EOF)

putc(ch,f);

fclose(f);

printf("\nOutput data :");

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;

printf("How many integer do you want? ");

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");

printf("\nThe numbers are :");

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)

printf("Enter name : ");

scanf("%s",n);

printf("Enter roll : ");

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;

char str[20]="Welcome to Nepal";

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.

Major Features of JavaScript

 It is standard scripting language and supported by most of the


browsers.
 It is a lightweight and efficient intrepeted progammming language.
 it is complementary to and intregated with HTML and Java.
 It is object- oriented event driven programming language.
 It is open source and cross platform (independent to OS)

Importance of Javascript

 It is designed for solving client-side application with prompt


responds.
 It can dynamically modify webpage as per requirement.
 It can validate and respond user’s input.
 It can be used to create cookies. (A piece of information stored on
the users computer by the web browser while browsing a website)
 It does not require any interaction to web server while processing
scripts on web browser.

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:-

Javascript program to find whether given


number is even or odd

<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:-

Javascript program to find the largest


among three numbers

<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("&nbsp; &nbsp;");
}

68
</script>
</body>
</html>

Output screen:-

Javascript program to find the factorial of


number

<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:-

Javascript program to find whether a


number is prime or composite

<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:-

Javascript program to find the area of


rectangle using function
<html>
<head>
<title>Area of Rectangle</title>
</head>
<body>
<script type="text/javascript">
function area()

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:-

Javascript program to reverse a number


<html>
<head>
<title>Reverse</title>
</head>
<body>
<script type="text/javascript">
var n=parseInt(prompt("Enter the number to reverse"));
var r,s=0;
while(n>0)
{

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:-

Javascript program to find whether given


number is palindrome or not
<html>
<head>
<title>Palindrome</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;

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 n=parseInt(prompt("Enter the number"));

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";

$sql="CREATE DATABASE YOURDB";


if($conn->query($sql)==TRUE)
{

echo"Databse created successfully";


}

else
{

87
echo"Error creating database".$conn->error;

$conn->close();

?>

</body>

</html>
Output:

Php program to create connection


88
<!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";

$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";
}

$sql="CREATE TABLE FOOTBALL(


jersey_no INT(7) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30)NOT NULL,
club VARCHAR(30)NOT NULL,
nationality VARCHAR(30)NOT
NULL)"; if($conn-
>query($sql)==TRUE)
{

echo"table created successfully";


}

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";
}

$sql="INSERT INTO FOOTBALL


VALUES(7,'RONALDO','AL NASAR','PORTUGAL')";
if($conn->query($sql)==TRUE)
{

echo"<br>Inserted data successfully";

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:

Php program to delete 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>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";
}

$sql="DELETE FROM FOOTBALL


WHERE jersey_no=7 ";
if($conn->query($sql)===TRUE)
{

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

You might also like