Вы находитесь на странице: 1из 73

CONTENTS

UNIX Operating System

Teacher's Serial no. 1.

Program
Program to perform arithmetic operations

Signature

2.

Program to calculate gross salary of employee

3.

Program to find if a given number is even or odd

4.

Program to add text to a given file if it has write permission

5.

Program to compute the

division of a student 6. Program to test whether two strings are equal. If they are equal, then print a message. Otherwise append the first string at the end of the second string. 7. Program to check permissions for a file using command line arguments 8. Program to find sum of even digits and product of odd digits in a number 9. Program to check whether the entered character is either a lower case alphabet or an upper case alphabet or a vowel or a digit or a special

character 10. Program to compute factorial of a number 11. Program to reverse a number 12. Program to calculate the sum of digits of a number 13. Program to check whether a given number is prime or not 14. Program to compute sum of n natural numbers 15. Program to compute the table of a number 16. Program to print Fibonacci series upto a desired number 17. Program to reverse a string

18.

Program to check whether a given string is palindrome or not

19.

Program to print the list of prime numbers between two numbers

Java Script
Teacher's Serial no. 1.

Program
Program to reverse a number

Signature

2.

Program to generate 20 random numbers in the range 20-40

3.

Program to accept principal amount and rate of

interest from the user and display the amount due to the user for 10 years through a table 4. Program to compute factorial of a number 5. Program to input a number to the text box and print the tenth Fibonacci series of that number 6. Program to input a number and display the exponential value of that number 7. Program to input radius and display the area & circumference of the circle in separate text boxes 8. Program to input a number

to the text box and print the Fibonacci Series upto that position in reverse order

UNIX

Program 1

#program to perform arithmetic operations #filename - computer echo "enter a no.:" read a echo "enter another no." read b echo "addition:" echo `expr $a + $b` echo '\n' echo "subtraction:" echo `expr $a - $b` echo '\n' echo "multiplication:" echo `expr $a \* $b` echo '\n' echo "division:" echo `expr $a / $b`

OUTPUT
user@ARSD051:~$ sh computer enter a no.: 2 enter another no. 34

addition: 36

subtraction: -32

multiplication: 68

division: 0

Program 2
#program to calculate gross salary of an employee #filename - employee echo "enter the basic salary of the employee:" read bs if [ $bc - gt 2000 ] then da = 500 else

da=`echo 0.1 \* $bs | bc` fi da=`echo 0.1 \* $bs | bc` hra=`echo 0.1 \* $bs | bc` gs=`echo $da + $hra + $bs | bc` echo "The value of da is $da" echo "The value of hra is $hra" echo "The gross salary of the employee: $gs"

OUTPUT

user@ARSD051:~$ sh employee enter the basic salary of the employee: 1000 The value of da is 100.0 The value of hra is 100.0 The gross salary of the employee: 1200.0

Program 3
#program to find if a given number is even or odd #filename - even_odd echo Enter the integer read num n=`expr $num % 2` if [ n -eq 0 ] then echo $num is even else echo $num is odd fi

OUTPUT
user@ARSD051:~$ sh even_odd Enter the integer 20

20 is even

user@ARSD051:~$ sh even_odd Enter the integer 21 21 is odd

Program 4

#program to add text to a given file if it has write permission #filename - write_perm echo Enter file name read str if [ -w $str ] then echo Enter your text echo ctrl+d to exit cat>>$str else echo $str does not have write permission fi

OUTPUT

user@ARSD051:~$ sh write_perm Enter file name even_odd Enter your text Hello Everyone

user@ARSD051:~$ sh write_perm Enter file name student student does not have write permission

Program 5
#program to compute the division of a student #filename - division1

echo "Enter the marks of the student in 5 subjects" read a read b

read c read d read e s=`expr $a + $b + $c + $d + $e` per=`echo .2 \* $s | bc` echo "The percentage of the student is $per" if [ $per -gt 85 ] then echo "A grade" elif [ $per -lt 85 ] && [ $per -gt 75 ] then echo "B grade" elif [ $per -lt 75 ] && [ $per -gt 60 ] then echo "C grade" else echo "D grade" fi

OUTPUT

user@ARSD051:~$ sh division1 Enter the marks of the student in 5 subjects 68

79 80 76 90 The percentage of the student is 78.6 D grade

Program 6
#program to test whether two strings are equal, otherwise append the 1st string after the 2nd string #filename - string echo "enter a string" read a echo "enter another string:" read b if [ $a = $b ] then echo "the two strings are equal" else echo "the new string is $b $a"

fi

OUTPUT

user@ARSD051:~$ sh string enter a string hello enter another string: hello the two strings are equal

user@ARSD051:~$ sh string enter a string good enter another string: morning the new string is morning good

Program 7
#program to check permissions for a file using command line arguments #filename - cmd if [ -r $1 -a -w $1 -a -x $1 ] then echo "file has all three permissions !!" else if [ -w $1 ] then echo "the file has write permission" fi if [ -r $1 ] then echo "the file has read permission" fi if [ -x $1 ] then echo "the file has execute permission" fi fi

OUTPUT

user@ARSD051:~$ sh cmd reverse the file has write permission the file has read permission

Program 8

#program to find sum of even digits and product of odd digits in a number #filename - eveodd echo "enter a no." read n p=1 s=0 while [ $n -ne 0 ] do m=`expr $n % 10` if [ `expr $m % 2` -eq 0 ] then s=`expr $m + $s` else p=`expr $m \* $p` fi n=`expr $n / 10` done echo "the sum of even digit is $s" echo "the product of odd digit is $p"

OUTPUT

user@ARSD051:~$ sh eveodd enter a no. 12345 the sum of even digit is 6 the product of odd digit is 15

Program 9
#check the entered character for lower case, uppercase, vowel, digit or a anyo ther character #filename - char echo "enter any character" read ch case $ch in

[aeiou]) echo "lower case vowel";; [a-z]) echo "character in lower case";; [AEIOU]) echo "upper case vowel";; [A-Z]) echo "character in upper case";; [0-9]) echo "entered the digit";; *) echo "entered a special character";; esac

OUTPUT

user@ARSD051:~$ sh char enter any character $ entered a special character

user@ARSD051:~$ sh char

enter any character A upper case vowel

Program 10
#program to compute factorial of a number #filename - factorial echo "enter the no:" read a f=1 i=1 c=`expr $a + 1` while [ $i -lt $c ] do f=`expr $i \* $f` i=`expr $i + 1` done

echo "The factorial of $a is $f"

OUTPUT

user@ARSD051:~$ sh factorial enter the no: 5 The factorial of 5 is 120

Program 11
#program to reverse a no: #filename - reverse echo "Enter a no.:" read a b=0 while [ $a -ne 0 ] do c=`expr $a % 10` b=`expr $b \* 10 + $c` a=`expr $a / 10` done echo "The reverse of the no. is $b"

OUTPUT

user@ARSD051:~$ sh reverse Enter a no.:

123 The reverse of the no. Is 321

Program 12

#program to calculate sum of the digits of a no #filename - sum_of_digits echo "Enter a no.:" read a b=0 d=$a while [ $a -ne 0 ] do c=`expr $a % 10` b=`expr $b + $c` a=`expr $a / 10` done echo "The sum of the digits of $d is $b"

OUTPUT

user@ARSD051:~$ sh sum_of_digits Enter a no.: 561 The sum of the digits of 561 is 12

Program 13
#program to check whether a given number is prime or not #filename - prime echo "enter the no.:" read n m=0 i=1 while [ $i -le $n ] do

if [ `expr $n % $i` -eq 0 ] then m=`expr $m + 1` fi i=`expr $i + 1` done if [ $m -eq 2 ] then echo "$n is a prime no." else echo "$n is not a prime no." fi

OUTPUT

user@ARSD051:~$ sh prime enter the no.: 7 7 is a prime no.

user@ARSD051:~$ sh prime enter the no.:

6 6 is not a prime no.

Program 14
#program to compute sum of n natural no.s #filename - natural echo "enter a no.: " read n m=`expr $n + 1` p=`expr $m \* $n` q=`expr $p / 2` echo "the sum of $n natural nos is : $q "

OUTPUT

user@ARSD051:~$ sh natural

enter a no.: 7 the sum of 7 natural nos is : 28

Program 15
#program to compute table of a no. #filename - table echo "enter a no.:" read a i=1 echo "enter a no. till you want the table:" read n while [ $i -le $n ] do echo $a "*" $i "=" `expr $a \* $i` i=`expr $i + 1` done

OUTPUT

user@ARSD051:~$ sh table enter a no.: 5 enter a no. till you want the table:

10 5*1=5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50

Program 16
#fibonacci series #filename - fibo echo "enter a no. "

read n a=0 echo $a \c b=1 echo $b \c i=2 while [ $i -le $n ] do c=`expr $a + $b` echo $c \c a=$b b=$c i=`expr $i + 1` done

OUTPUT

user@ARSD051:~$ sh fibo enter a no. 6 0112358

Program 17
#program to find the reverse of the string #filename - rev_string echo "enter the string:" read s len=`echo $s | wc -c` while [ $len -gt 0 ] do ch=`echo $s | cut -c $len` len=`expr $len - 1` rev=$rev$ch

done echo $rev

OUTPUT

user@ARSD051:~$ sh rev_string enter the string: dam mad

Program 18
#program to check whether a given string is palindrome or not #filename - pal echo "enter the string:" read s len=`echo $s | wc -c` while [ $len -gt 0 ] do ch=`echo $s | cut -c $len` len=`expr $len - 1` rev=$rev$ch done echo $rev if [ $s = $rev ] then echo "it is a palindrome" else echo "it is not the palindrome" fi

OUTPUT

user@ARSD051:~$ sh pal enter the string: madam madam it is a palindrome

Program 19

#program to print the list of prime no.s between two numbers #filename prime_list echo Enter two numbers read a b x=`expr $a + 1` y=`expr $b 1` i=$x while [ $i -le $y ] do flag=0 n=`expr $i / 2` j=2 while [ $j -le $n ] do num=`expr $i % $j` if [ num -eq 0 ] then flag=1 fi j=`expr $j + 1` done if [ $flag -ne 1 ] then echo $i \c fi

i=`expr $i + 1` done

OUTPUT

user@ARSD051:~$ sh prime_list Enter two numbers 2 10 357

JavaScript

Program 1

<!-- Program to reverse a number --> <HTML> <HEAD> <TITLE>Reverse</TITLE> <SCRIPT LANGUAGE = "Javascript"> var n = parseInt(window.prompt( "Enter number", "0" )); document.write( "<FONT SIZE = 6>Reverse :<BR>" ); var rev = 0; var r; while(n>0) { r = n%10; rev = rev*10+r; n = parseInt(n/10); } document.write( rev+"</FONT>" ); </SCRIPT> </HEAD> <BODY></BODY> </HTML>

OUTPUT

Program 2

<!-- Generating random numbers --> <HTML> <HEAD> <TITLE>Random Numbers</TITLE> <SCRIPT LANGUAGE = "Javascript"> for(var i=1; i<=20; i++) { document.write(20+Math.random()*20); document.write( "<BR>" ); } </SCRIPT> </HEAD> <BODY></BODY> </HTML>

OUTPUT

Program 3

<!-- Computing amount for 10 years --> <HTML> <HEAD> <TITLE>Amount</TITLE> <SCRIPT LANGUAGE = "Javascript" > var p = parseFloat(window.prompt( "Enter principal amount", "0" )); var r = parseFloat(window.prompt( "Enter rate of interest", "0" )); document.write( "<TABLE BORDER = '2' WIDTH = '100%'>" ); document.write( "<TR><TD WIDTH = '100'><FONT SIZE = 5>Year</FONT></TD><BR>" ); document.write( "<TD><FONT SIZE = 5>Amount</FONT></TD></TR>" ); var amount; for(var year=1; year<=10; year++) { amount=p*Math.pow(1+r, year); document.write( "<TR><TD>"+year+"</TD><TD>" ); document.write(Math.round(amount*100)/100+"</TD></TR>" ); } document.write( "</TABLE>" ); </SCRIPT> </HEAD> <BODY></BODY> </HTML>

OUTPUT

Program 4

<!-- Computing factorial of a number --> <HTML> <HEAD> <TITLE>Factorial</TITLE> <SCRIPT LANGUAGE = "Javascript"> var n = parseInt(window.prompt( "Enter number", "0" )); document.write( "<H1>FACTORIAL<BR>" ); var f = 1; for(var i=1; i<=n; i++) { f = f*i; } document.write( f+"</H1>" ); </SCRIPT> </HEAD> <BODY></BODY> </HTML>

OUTPUT

Program 5
<!-- Input a number to the text box and printing tenth fibonacci series of that number --> <HTML> <HEAD> <TITLE>Tenth Fibonacci Series</TITLE> <SCRIPT LANGUAGE = "Javascript"> function fibo() { var n = tenth.num.value; var a = 0; var b = 1; var i = 3; var c = a+b; while(c<=n) { a=b; b=c; ++i; c=a+b; } document.write( "<FONT SIZE = 6>" ); document.write( n+" " ); for(var j=i; j<=i+8; j++)

{ document.write( c+" " ); a=b; b=c; c=a+b; } document.write( "</FONT>" ); } </SCRIPT> </HEAD> <BODY> <FORM NAME = "tenth"> <INPUT TYPE = "text" NAME = "num"> <INPUT TYPE = "button" VALUE = "Calculate" ONCLICK = "fibo()"> </FORM> </BODY> </HTML>

OUTPUT

Program 6

<!-- Displaying exponential value of a number --> <HTML> <HEAD> <TITLE>Exponential Value</TITLE> <SCRIPT LANGUAGE = "Javascript"> var n = parseInt(window.prompt( "Enter number", "0" )); document.write( "<FONT SIZE = 5>Exponential value of the entered number :<BR><BR>" ); document.write( Math.exp(n)+"</FONT>" ); </SCRIPT> </HEAD> <BODY></BODY> </HTML>

OUTPUT

Program 7

<!-- Printing area and circmference of a circle in separate text boxes --> <HTML> <HEAD> <TITLE>Circle</TITLE> <SCRIPT LANGUAGE = "Javascript"> var rad=window.prompt(' Enter the radius of the circle', '0' ); var r = parseInt(rad); function calc() { cir.area.value = 3.14*r*r; cir.circumference.value = 2*3.14*r; } </SCRIPT> </HEAD> <BODY> <FORM NAME = "cir"> <FONT SIZE = 5> Area : <INPUT NAME = "area" TYPE = "Text"><BR><BR>

Circumference : <INPUT NAME = "circumference" TYPE = "Text"></FONT><BR><BR> <INPUT TYPE = "button" VALUE = "Calculate" ONCLICK = "calc()"> </FORM> </BODY> </HTML>

OUTPUT

Program 8

<!-- Printing fibonacci series upto the entered position in reverse order --> <HTML> <HEAD> <TITLE>Fibonacci Series</TITLE> <SCRIPT LANGUAGE = "Javascript"> function fibonacci() { var n=action.position.value; var a=0; var b=1; var c; var arr = new Array(n); arr[1] = a; arr[2] = b; for(var i=3; i<=n; i++) { c=a+b; arr[i] = c; a=b; b=c; } document.write( "<FONT SIZE = 5>" );

for(i=n; i>=1; i--) { document.write( arr[i]+" " ); } document.write( "</FONT>" ); } </SCRIPT> </HEAD> <BODY> <FORM NAME = "action"> <INPUT NAME = "position" TYPE = "text"> <INPUT TYPE = "button" VALUE="Calculate" ONCLICK="fibonacci()"> </FORM> </BODY> </HTML>

OUTPUT

Вам также может понравиться