Computer Project
Computer Project
TECHNOLOGY
Bishara Chowk, Janakpurdham-4
Project Work of Computer Science
ACKNOWLEDGEMENT
I have taken efforts in this project. However, it would not have been
possible without the kind support and help of many individuals. I would
like to extend my sincere thanks to all of them.
I would like to express a deep sense of thanks and gratitude to my
subject teacher Mr. Sanjeev Kumar Yadav for guiding me immensely
through the course of the project. He always evinced keen interest in my
work. His constructive advice and constant motivation have been
responsible for the successful completion of this project.
My sincere thanks go to Mr. Raja Ram Yadav , our principal sir, for
his co-ordination in extending every possible support for the completion of
this project.
I also thanks to my parents for their motivation and support. I must
thanks to my classmates for their timely help and support for completion
of this project.
Last but not the least, I would like to thank all those who had
helped directly or indirectly towards the completion of this project.
Divakar Yadav
Class: XII ''C"
Table of Contents
JavaScript
JavaScript
JavaScript is a dynamic scripting language (website development environment). It is
lightweight and mostly used as a part of web pages. JavaScript code can be inserted into any HTML
page, and it can be executed by all types of web browsers. JavaScript is used to make web pages
interactive. It runs on your visitor's computer and doesn't require constant downloads form your
website.
JavaScript Syntax
JavaScript can be implemented using JavaScript statement that are placed within the <script>
….. </script> HTML tags in a web page. We can place the <script> tags, containing our JavaScript,
anywhere within our web page, but it is normally recommended that we should keep it within the
tags.
The <script> tags alert the browser program to start interpreting all the text between these tags as a
script. A simple syntax of our JavaScript will appear as follows.
<script…>
JavaScript code
</script>
JavaScript Control Structures
A control structure refers to the flow of execution of the program. These control structures
enable us to specify the order in which the various instructions in a program are to be executed. The
types of control structure are:
1. Selection Control structure
• If
• If else
• If else if
• Switch case
2. Looping Control structure
• For loop
• While loop
• Do while loop
a. If statement:
The if statement is the fundamental control statement that allows JavaScript to make
decisions and execute statements conditionally. If statement is the simplest decision-making
statements will be executed or not. That is, if a certain condition is true then a block of statement is
executed otherwise not.
Syntax:
If (expression) {
Statement (s) to be executed if expression is true
}
The if statement evaluates the conditions first and then, if the value of the condition is true, it execute
the statement within this black. Otherwise, it skips the statements within its block and continues from
the first statement outside the if block.
Example:
<html>
<body>
<script type= "text/javascript">
var age = 20;
if (age >18) {
document.write("<b>Qualifies for driving</b>");
}
</script>
</body>
</html>
Output:
Qualifies for driving
Example:
<html>
<body>
<script type= "text/javascript">
var age = 16;
if (age > 18) {
document.write ("<b>Qualifies for driving</b>");
}
else {
document.write ("<b>Does not qualifies for driving</b>");
}
</script>
</body>
</html>
Output:
The conditions are evaluated from the top downward. As soon as a true condition is found,
the statement associated with it is executed and the rest of the ladder is bypassed. If none of the
conditions are true, the final else is executed. If the final else is not present, no actions take place if all
other conditions are false.
Example:
<html>
<body>
<script type="text/javascript">
Var book = "maths";
if (book == "history") {
document.write ("<b> History Book </b>");
}
else if (book == "maths") {
document.write ("<b> Maths Book</b>");
}
else if (book == "economics") {
document.write ("<b> Economics Book</b>");
}
else {
document.write ("<b> Unknown Book</b>");
}
</script>
</body>
</html>
d. Switch Statement
The objective of the switched statement is to give expression to evaluate several different
statements to execute based on the value of the expression. The interpreter checks each case against
the value of the expression until a match is found. If nothing matches, a default condition will be used.
Syntax:
switch (expression)
{
case condition 1: statement (s)
break;
……….
case condition n: statement (s)
break;
default: statement (s)
}
Example:
<html>
<body>
<script type="text/javascript">
var grade = 'A';
document.write ("Entering switch block</br>");
switch (grade)
{
case 'A' : document.write("Good job<br/>");
Break;
case 'B' : document.write("Pretty Good<br/>");
Break;
case 'C' : document.write("Passed<br/>");
Break;
case 'D' : document.write("Not so good<br/>");
Break;
case 'F' : document.write("Failed<br/>");
Break;
}
document.write ("Exiting switch block");
</script>
</body>
</html>
Output:
Entering switch block
Good job
1. The loop initialization where initialize our counter to starting value. The initialization statement is
executed before the loop begins.
2. The test statement which test is given condition is true or not. If the condition is true, then the code
given inside the loop will be executed, otherwise, the control will come out of the loop.
3. The iteration statement where we can increase or decrease our counter.
Syntax:
For (initialization; test condition; iteration statement) {
Statement (s) to be executed if the test condition is true
}
Example:
<html>
<body>
<script type="text/javascript">
var i;
for (i=0; i<5; i++)
{
document.write("Computer Science</br>");
}
</script>
</body>
</html>
Result:
Computer Science
Computer Science
Computer Science
Computer Science
Computer Science
b. While Loop
While loop is an entry-controlled loop. In while loop, condition is evaluated first and if it
returns true then the statements inside the while loop execute. When condition return false, the
control comes out of loop and jumps to the next statement after while loop. When using while you
need to use increment or decrement statement inside the while loop so that the loop variable gets
changed on each iteration and at some point, condition returns false. This way you can end the
execution of while loop otherwise the loop will execute indefinitely.
Syntax:
While (expression) {
Statement (s) to be executed if expression is true
}
Example:
<html>
<body>
<script type="text/javascript">
var count=0;
document.write ("Starting Loop <br/>");
while (count <=5) {
document.write ("Current Count: "+ count +"<br/>");
count ++;
}
document.write ("Loop stopped!");
</script>
</body>
</html>
Output:
Starting Loop
Current Count: 0
Current Count: 1
Current Count: 2
Current Count: 3
Current Count: 4
Current Count: 5
Loop Stopped!
Loop control
JavaScript provides full control to handle loops and switch statements. There may be a
situation when we need to come out of a loop without reaching its bottom. There may also be a
situation when we want to skip a part of our code block and start the next iteration for the loop.
a. The break statement
The break statement is used to exit all loop early, breaking out of the enclosing curly braces.
Example
<html>
<body>
<script type="text/javascript">
var x = 0;
document.write ("Entering the loop<br/>");
while (x<20)
{
if (x == 5) {
break;
}
x = x + 1;
document.write (x + "<br />");
}
document.write ("Exiting form the loop! <br />");
</script>
</body>
</html>
The continue statement
The continue statement tells the interpreter to immediately start the next iteration of the loop and
skip the remaining code block.
When a continue statement is encountered, the program flow moves to the loop check expression
immediately and if the condition remains true, then it starts the next iteration, otherwise the control
comes out of the loop.
Example
<html>
<body>
<script type="text/javascript">
var x= 0;
document.write ("Entering the loop<br />");
while (x < 9)
{
x=x+1;
if (x==5) {
continue;
}
document.write (x + "<br/>");
}
document.write ("Exiting from the loop!<br/>");
</script>
</body>
</html>
Output:
Entering the loop
1
2
3
4
6
7
8
9
</html>
Form Validation
Form validation is a process of verifying whether the data entered by the user on the web
page its complete or not.
The data given by the user be sent to the server to check accuracy. If any mistake persists,
that has to be sent back to the user for correction. As this is a time taking process, the data will be
checked by JavaScript before submitting. This will save a lot of time.
Efficient web programming is possible only by making the information secure. JavaScript
forms authentication is defined as a process of validating the user credentials against some
authority. When the form is submitted without providing the data in the form field, JavaScript to show
a pop-up message restricting the form submission.
Example
<html>
<head>
<script>
function validateData()
{
var name=document. form1.user_name.value;
var password=document. form1.user_password.value;
if (name == null||name==")
{
alert ("Name can't be blank");
return false;
}
elseif (password. Length < 6) {
alert ("Password must be at least 6 character long.");
return false;
}
}
</script>
</head>
<body>
<form name="form 1" method="post" action="valid.php" onsubmit="return validateData()">
Username: <input type="text" name="user_name"><br><br>
Password: <input type="password" name="user_passsword"><br>
<input type="submit" value="register">
</form>
</body>
</html>
Output:
PHP
PHP
The PHP Hypertext Preprocessor is a programming language that allows web developers to
create dynamic content that interacts with databases. PHP is basically used for developing web-based
software applications. The product was initially named Personal Home Page. It is used to create web
applications in combining with the web server, such as Apache. PHP has nothing to do with layout,
events, and nothing about the look and feel of a web page. In fact, most of what PHP does invisible to
the user.
• PHP is an acronym for “PHP: Hypertext Preprocessor”.
• PHP is a widely used, open-source scripting language.
• PHP scripts are executed on the server.
• PHP is free to download and use.
• PHP 8 is used currently.
• PHP is free to use.
• PHP is easy to learn and write.
• Returns simple output to the client.
• PHP files have a file extension of “.php”.
• PHP runs on different platforms (Windows, Linux, Unix, etc.).
Variables:
Variables are containers for storing information. PHP variables are case sensitive. PHP is a
Loosely Typed Language. PHP automatically converts the variable to the correct data type, depending
on its value. In other languages such as C++, C and Java programmers must declare the name and type
of variable before using it.
In PHP, a variable is start with the sign $ followed by the name of the variable.
Example:
<?php
$txt = "Hello world!";
$x =5;
$y =10.5;
?>
PHP Variables Scope:
In PHP, variables can be declared anywhere in the script. The scope of a variable is the part
where the variables can be referenced/used.
PHP has three different variable scopes:
a. Global Scope:
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed
outside a function:
Example:
Variable with a global scope:
<?php
$x=5;
function myTest() {
echo "<p> Variable x inside function is: $x</p>";
}
myTest();
echo"<p> Variable x outside function is: $x </p>
?>
b. Local Scope
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that
function:
Example:
Variable with local scope:
<?php
function myTest();
$x =5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside this will generate an error
echo "<p>Variable x outside function is: $x</p>";
?>
PHP The global Keyword:
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):
Example
<?php
$x=5;
$y=10;
function myTest() {
global $x, $y;
$y=$x+$y;
}
myTest();
echo $y;
?>
Output
15
PHP Constants:
A constant is an identifier (name) for a simple value. The value cannot be changed during the
script. A valid constant name starts with a letter or underscore (no $ sign before the constant name).
<?php
echo(abs(-607));
?>
Output:
6.7
PHP round() Function: -- PHP round() Function rounds a floating-point number to its nearest integer.
Example:
<?php
echo(round(0.60));
echo(round(0.49));
?>
Output:
1
0
PHP date() Function: converts timestamp to a more readable date and time format.
Example:
<?php
echo "Today's date is:";
$today= date("d/m/Y");
echo $today;
?>
Output:
Today's date is: 10/04/2022
Example:
<?php
echo date("h:i:s") . "\n";
echo date("M,d,Y h:i:s A") . "\n";
echo date("h:I a");
?>
Output:
09:33:07
Apr,10,2022 09:33:07 AM
09:33 am
The difference is small: echo has no return value while print has a return value of 1 so it can be used
in expressions, echo can take multiple parameters (although such usage is rare) while print can take 1
argument, echo is marginally faster than print.
Example:
<html>
<head>
</head>
<body>
<?php
$str="Mukesh";
$x=5;
$y=4;
echo $x+$y;
print $str;
print "<br> what anice day!";
print "My teacher's name is $str";
?>
Operator
Operators are used to perform operations on variable and values. The operators used in PHP
and other programming language including JavaScript are similar. PHP divides the operators in the
following groups:
• Arithmetic operators (+, -, *, /, %, **)
• assignment operators (=, +=, -=, *=, /=, %=)
• comparison operators (==, ===, !=, <>, !==, >, <, >=, <=, <=>)
• increment decrement operators (++$x, $x++, --$x, $x--)
• logical operators (and, or, Xor, And, Or, ||, !)
• string operators (.=)
• conditional assignment operators (?:, ??)
PHP conditional statements
Conditional statements are used to perform different actions based on different conditions.
Very often when you write code, you want to perform different actions for different conditions. You
can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
1. PHP - the if statement
The if statement executes some code if one condition is true.
Syntax:
if (condition) {
code to be executed if condition is true;
}
Example:
<?php
$t=date("H");
if ($t<"20") {
echo "Have a good day!";
}
?>
Output:
18
Have a good day!
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
…..
default:
code to be executed if n id different from all labels;
}
Example:
<?php
$favcolor = "red";
switch ($favcolor) {
case "red";
echo "Your favorite color is red!";
break;
case "blue";
echo "Your favorite color is blue!";
break;
case "green";
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue nor green!";
}
?>
Output:
Your favorite color is red!
PHP Loops
Often when you write code, you want to the same block of code to run over and over again a
certain number of times. So, instead of adding several almost equal code-lines in a script, we can use
loops.
Loops are used to execute the same block of code again and again, as long as a certain condition is
true.
In PHP, we have the following loop types:
1. The PHP while loop
The while loop executes a block of code as long as the specified condition is true.
Syntax:
while (condition is true) {
code to be executed;
}
Example:
<?php
$x=1;
while($x<=5) {
}
Example:
<?php
for ($i=1; $i<5; $i++) {
for ($j=1; $j<=$i; $j++) {
echo " * ";
}
echo "<br/>";
}
?>
Output
*
**
***
****
*****
PHP Break
You have already seen the break statement used in an earlier chapter of this tutorial. It was
used to "jump out" of a switch statement.
Example:
<?php
for ($x=0; $x<10; $x++) {
if ($x==4) {
break;
}
Echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
PHP Continue
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.
Example:
<?php
for ($x=0; $x<5; $x++) {
if ($x==2) {
continue;
}
echo "The number is: $x<br>";
}
?>
Output:
The number is: 0
The number is: 1
The number is: 3
The number is: 4
PHP goto statement
The go to statement is used to send flow program to a certain location in the code. The
location is specified by a user defined label. Generally, goto statement comes in the script as a part
of conditional expression such as if, else or case ( in switch construct).
Syntax:
statement1;
statement2;
if (expression)
goto label1;
statement3;
label1: statement4;
Example:
<?php
$x=0;
start:
$x++;
echo "x=$x\n";
if ($x<5)
goto start;
?>
Output:
x=1
x=2
x=3
x=4
x=5
PHP- A Simple HTML Form
</html>
File: welcome.php
<html>
<body>
Welcome <?php echo $POST ["name"]; ?><br>
Your email <?php echo $_POST ["email"]; ?>
</body>
</html>
C Programming Language - II
Function in C
Function
A function is a self-contained program segment or the block of statement that perform some
specific, well-defined task every C program comprises of one or more functions. The execution of any
program always begins by the executing the instructions in the main() functions.
scanf(“%d”, &a);
b= value(a); //call function
}
int value (int a) //function declarator
{
int p; function body (called function)
P =a^ a
return (p);
}
iii. Function definition:
A function definition provides the actual body of the function. It contains the statements that
will be executed when the function is called. It contains function name, list of parameters, return type
of function and statement of the function.
Syntax:
data_type function_name(data_type arg1,data_type arg2......data typeargn)
{
declaration and statements
}
e.g.
int sum(int a, int b) //function declarator
{
int c;
c=a+ b; body of the function
return(c)
}
iv. Return and void statements of the function
Return statement: One of the main features of the function is that it can return the value to the calling
function. For this, a return keyword is used. So, return keyword return the program control from a
function to the calling function.
Syntax:
return expression:
Example:
int great (int a, int b)
{
if(a> b)
return a;
else
return b;
}
Void statement: If a function does not return a value, then we say that the function return type is
void. So, no expression is present if a return keyboard is used, then it is used to transfer the control at
the end of the function.
Syntax:
void function_name(data_type var)
Example:
void evenodd(int n)
{
if (n%2==0)
printf(“%d is even number”, n);
else
printf(“%d is odd number”, n);
}
Example: WAP in c to find sum of two number using function.
Solution:
#include<stdio.h>
#include<conio.h>
int sum(int, int); //function declaration
void main()
{
int a, b, s;
printf(“enter two number:”);
scanf(“%d%d”, &a, &b);
s=sum(a, b); //function call
printf(“the sum is %d”, S);
getch();
}
int sum(int a, int b) //function definition
{
int add;
add =a+ b;
return add;
}
Output:
enter two number:
3
4
The sum is 7
Recursion:
The concept of using recursive function to repeat the execution of statements many times is
known as recursion.
Example: WAP in c to calculate factorial of user input number by using recursion.
Solution:
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n, ans =0;
printf(“\n enter any number:”);
scanf(“%d”, &n);
ans = factorial(n);
printf(“ the factorial of %d is %d”, n ans);
getch();
factorial(int p)
{
if(p==1)
return(1);
else
return(p* factorial(p-1));
}
Output:
enter any number: 3
the factorial of 3 is 6
STRUCTURE AND UNION
Introduction to structure
A structure is a collection of one or more variables, possible of different data types, grouped
together under a single name for convenient handling. Each data item is called member (elements,
field) of structure. The keyboard struct is used to declare structure.
Declaration of structure
Syntax:
Keyword structure name or tag
Struct user_defined_name
{
data-type member1;
data-type member2;
data-type member3;
-------------------------; Structure members
-------------------------;
-------------------------;
data-type-member n;
} structure variables;
Optional
Example of structure declaration:
A structure declaration to present information about a student’s name and other details.
Structure variable
A structure variable is a variable of a structure type. To declare a variable of type students,
defined earlier, write:
struct student st;
Initialization of structure
Like other variable and arrays, structure variable can be initialized where they are declared.
The format is quite similar to that used to initialized arrays. The initial values must appear in the order
in which they will be assigned to their corresponding structure members. Let us initialized structure
variable emp1, emp2 and emp3 as well in the above-mentioned example,
struct employee
{
char name[35];
unsigned int age;
char address[50];
float salary;
} emp1= {“alpha”,20, “ktm”,15000.00};
emp2= {“beta”, 25,”jnp”, 20000.00};
struct employee emp3 ={“gamma”,25,”prk”,25000.00};
Size of structure
The elements of structure are always stored in contiguous memory location.
struct employee
char name[35];
unsigned int age;
char address[50];
float salary;
As we know every structure member takes its own memory. In the above example, name
takes 35 bytes. As we know that char takes 1 byte i.e., 35 char takes 35 bytes.
Note: we can access an individual member of a structure variable by using a dot (.) operator.
UNION
Introduction to Union
Union is similar to structure but it is differing only its storage location. The ‘union’ keyword is used to
define union. In structure, each member has its own memory block whereas all member of union can
share same memory location. Union can take less amount of memory than structure and it can access only
one member at a time.
Declaration of union: OR:-
Union union_name union user defined as name
{ {
data type member variable1; data type member1;
data type member variable2; data type member2;
------------------------------------- ---------------------------
------------------------------------- ---------------------------
------------------------------------ }
data type member variable n; union user_defined name var1,
}; var2; …………… var n;
Once union name is declared as new data type, then variables of that
type can be declared as
union union_name union_variable;
Example: Create a union named structure that has name, roll-no, marks, gender and phone-no as
members.
#include<stdio.h>
#include<conio.h>
union student
{
char name[20];
int roll_no;
float marks;
};
void main()
{
union student s;
clrscr();
printf(“enter the name:”);
gets(s.name);
printf(“name=%s \t”, s.name);
printf(“\n enter roll no:”);
scanf(“%d”, &s.roll);
printf(“\n Roll= %d”, s.roll);
s.marks =91.5;
printf(“\n marks= %2f”, s.marks);
getch();
}
POINTER
Pointer
A pointer is a variable which stores the address of another variable i.e., direct address of memory
location. The pointer variable might be any of the data type such as int, float, char, double etc.
Pointer Declaration:
Pointer variables like all other variables must be declared before they may be used in the c program.
We can use asterisk(*)to do so. Its general form is:
data type *pointer variable:
For example:
int *ptr;
This statement declares the variable ptr as a pointer to int, that is ptr can hold address of an integer
variable. Some examples of pointer declaration is as follows:
int*ip; //Pointer to as integer
double*dp; //Pointer to as double
float*fp; //Pointer to as float
Pointer Arithmetic
Pointer can be manipulated by arithmetic operators with integer values. These arithmetic operators
are only + and - and increment and decrement operator. The increment and decrement of pointer variable
depend on its data types.
Example: An example to illustrate pointer arithmetic.
#include<stdio.h>
#include<conio.h>
main()
{
int *p;
int age =17
p = &age;
printf("\n Value of age is %d", age);
printf("\n Increment of an age is%d", ++age);
printf("\n Address of age is%u", p);
printf("\n Increment in pointer is %u", ++p);
getch();
}
Output:
Value of age is 17
Increment of an age is 18
Address of age is 284989
Increment of pointer is 284993
File Handling
File
A file is a collection of bytes stored on a secondary storage media, (i.e., hard disk, floppy disk, etc).
Files are used to store information permanently and to access and change that information whenever
necessary. C Programming has different types of library function for creating and managing data files.
Note:
Compiler automatically origins special character at the end of the file called EOF(end of file)
The keyword FILE is used to declare filer data type. The first statement defines a file pointer variable
with a data type file. The second statement creates a file with name file name and returns memory
location(address) that is arrogated to the file pointer fp. The mode defines the purpose of the file
which can be read, written or append.
2. String input/output
i. fgets()
It is used to read a set of character as a string from a given file and copies the string to a given
memory location normally in array.
Syntax
fgets(sptr,n,ptr);
where ptr is file pointer, control is different format like %d%f etc and list is variable parasfeter list to
be read from specified file.
ii. fprintf():- It is used to write a formatted data into a given file.
Syntax
fprintf(ptr, control spring”, list);
when ptr is the file pointer, control spring is a formatted command and list is a variable parameter list
record.
5. Block input/output
i. fwrite()
It is used to write a binary data to a given file.
Syntax
fwrite(ptr, size, n items, fptr);
//returno;
getch();
}
Example
1. WAP in c which writes “welcome to Nepal” in a file.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
file*fp;
fp= fopen(“NEPAL.DAT”,”w”);
fprintf(fp,”%s” “welcome to Nepal”);
fclose(fp);
getch();
}
2. WAP in c which needs name, roll number and age from a file named “student.dat” and display
them
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int rn,age;
char name[25];
file*fp
fp= fopen(“student.dat”, “r”);
printf(“\n name \t Roll number \t age \n”);
printf(“\n……………………….\n”);
fscanf(fp, “%s %d %d “, name, &rn, &age);
/*reads first record*/ while(!feof (fp))
{
printf(“\n %s \t %d \t %d”,name, rn, age);
fscan(fp, %s %d %d”,name,&rn,&age);
/*reads remaining records*/
}
fclose(fp);
getch();
}