Saturday, September 3, 2011

How to use document.write in JavaScript?


Hello guys, Today I am sharing very basic method of javascript which is "document.write()".
Using document.write you can write inside a html document using javascript.

A very basic hello world example is below:

<script type="text/javascript">
document.write("<p>Hello World</p>");
</script>


To understand document.write function in more depth, I am sharing few more simple usage of this function.

<script type="text/javascript">
var sampleText="Sample Example of Document.write function";
document.write("<p>"+sampleText+"</p>");
</script>

Printing date using document.write function:

<script type="text/javascript">
var sampleText="Sample Example of Document.write function";
document.write("<p>"+Date()+"</p>");
</script>


Using document.write function inside a function:

<script type="text/javascript">
function f1() {
document.write('hello world');
}
</script>

<script type="text/javascript">
f1();
document.write('Ba Bye');
</script>


Overriding whole document's content using document.write function:
<script type="text/javascript">
function f1() {
document.write('Some text');//for overwrite entire page content
}
window.onload = w1;
</script>



I hope you like this post. Please share your feedback by commenting below.






Friday, December 10, 2010

C program for matrix multiplication of two matrix

Hello everybody I want to discuss the source code for matrix multiplication in c programming.
matrix-multiplication

Program Code



//C program for multiplication of two matrix
#include < stdio.h >
#include < conio.h >
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("Enter the elements of first 3x3 matrix");
for (i = 0;i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter the elements of second 3x3 matrix");
for(i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\nThe first matrix is :-\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("\t%d", a[i][j]);
}
printf("\n");
}
printf("\nThe second matrix is :-\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n");
}

printf("\nMultiplication of the two matrices is as follows:\n");
for (i = 0;i < 3; i++)
{
printf("\n");
for (j = 0; j < 3; j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j] = c[i][j]+a[i][k] * b[k][j];
printf("\t%d", c[i][j]);
}
}
getch();
}


Input :


Enter the elements of first 3x3 matrix

1
2
3
4
5
6
7
8
9
Enter the elements of second 3x3 matrix
1
2
3
4
5
6
7
8
9

Output :



Multiplication of the two matrices is as follows

30 36 42
66 81 96
102 126 150


matrix-multiplication

Thursday, December 9, 2010

Javascript for find greatest and smallest among three numbers

Hello I want to discuss the code for find greatest and smallest from 3 numbers using javascript.

HTML CODE



<html>
<head>
<title>
Javascript Program to find greatest and smallest from 3 numbers
<script language="javascript">
function greater()
{
var num1=parseInt(document.f1.t1.value);
var num2=parseInt(document.f1.t2.value);
var num3=parseInt(document.f1.t3.value);
if(num1>num2 && num1>num3)
{
alert(num1 +" is greatest number");
}
else if(num2>num1 && num2>num3)
{
alert(num2 +" is greatest number");
}
else
{
alert(num3 +" is greatest number");
}
}
function smaller()
{
var num1=parseInt(document.f1.t1.value);
var num2=parseInt(document.f1.t2.value);
var num3=parseInt(document.f1.t3.value);
if(num1<num2 && num1<num3)
{
alert(num1 +" is smallest number");
}
else if(num2<num1 && num2<num3)

{
alert(num2 +" is smallest number");
}
else
{
alert(num3 +" is smallest number");
}
}
</script>
</head>
<body>
<h1 align="center"><font color="red">Javascript for find greatest and smallest</font></h1>
<form name=f1>
num1: <input type="textbox" name=t1><br>
num2: <input type="textbox" name=t2><br>
num3: <input type="textbox" name=t3><br><br><br>
<input type="button" value="greatest" onclick="greater()" >
<input type="button" value="smallest" onclick="smaller()">
</form>
</body>
</html>

OUTPUT




javascript

Wednesday, December 8, 2010

Javascript program to find factorial of given number

Hello everybody I want to discuss the javascript code for find factorial of given number


HTML CODE




<html>
<head>
<title>
Javascript program for find factorial of given number
</title>
<script type="text/javascript">
function fact(n)
{
var m=1;
while(n)
{
m=m*n;
n--;
}
alert("Factorial of given number :"+" "+ m);
}
</script>
</head>
<body>
<h1 align="center"><font color="green">Javascript Program for calculate factorial</font></h1>
<form name="frm1">
Enter any number :<input type="text" name="fact1"><br><br>
<input type="submit" value="calculate factorial" onclick="fact(frm1.fact1.value)">
</form>
</body>
</html>

OUTPUT