linux prog

5
AIM: To write a shell program to check whether the given number is Armstrong or not. ALGORITHM: · Enter the given number. · Separate the digit using the formula q=`expr $n % 10` and r=`expr $n % 10`. · Calculate sum of cube of digits by the formula a=`expr $a + $r /* $r /* $r`. · Compute the result with the given number. · if found equal then display it as Armstrong number else display not Armstrong number. PROGRAM SOURCE CODE echo ”enter the number” read n q=$n a=0 while [ $q – gt 0 ] do r= `expr $q % 10 ` q= `expr $q / 10 ` a=`expr $a + $r /* $r /*$r ` done if [ $a=$n ] then echo “the number $n is armstrong number” else echo “the number $n is not armstrong number” fi CONCLUSION: Thus the shell program to check whether a number is armstrong or not is written and executed successfully.

Upload: sougata-roy-chowdhury

Post on 09-Mar-2015

64 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Linux Prog

AIM:To write a shell program to check whether the given number is Armstrong or not.ALGORITHM:· Enter the given number.· Separate the digit using the formula q=`expr $n % 10` and r=`expr $n % 10`.· Calculate sum of cube of digits by the formula a=`expr $a + $r /* $r /* $r`.· Compute the result with the given number.· if found equal then display it as Armstrong number else display not Armstrong number.

PROGRAM SOURCE CODEecho ”enter the number”read nq=$na=0while [ $q – gt 0 ]dor= `expr $q % 10 `q= `expr $q / 10 `a=`expr $a + $r /* $r /*$r `doneif [ $a=$n ]thenecho “the number $n is armstrong number”elseecho “the number $n is not armstrong number”fi

CONCLUSION:Thus the shell program to check whether a number is armstrong or not is written and executed successfully.

Page 2: Linux Prog

I need to write a shell script that will output prime numbers within a range..

what I tried is to check whether a number is prime or not

set -vxecho "Enter the number"read N

I=$NJ=2

until [ $J == $N ]doif [ `expr $I % $J` == 0]thenecho "The number is not prime"exitelseJ=`expr $J + 1`fi

doneecho "The number is not prime"

I wrote the program that will output prime numbers within a range in C

the program in C is

int n,m,i,j,pprintf ("Enter the first number")scanf ("%d",&n )printf ("Enter the second number")scanf ("%d",&m )

for ( i=m;i<=n;i++){p=0for(j=2;j<=i/2;j++){if(i%j==0){p=1break;}printf ("%d", i );else{printf ("%d, is not prime" );} }

Page 3: Linux Prog

}

-----------------------------------------------------------------------------------------------------------------------------------

To generate Fibonacci number. In the Linux Bash shell script programmingStep by step program Algorithm For Generating Fibonacci Number fibonacci() {choice=$1n1=0n2=1i=1while [ $i -le $choice ]doif [ $i -eq 1 ]thenecho "$n1"elif [ $i -eq 2 ]thenecho "$n2"elsenew=`expr $n1 + $n2`echo "$new"n1=$n2n2=$newfii=`expr $i + 1`doneecho "Enter the number of terms( 1-100 ) : "read numecho "Fibonacci Series for $num terms"fibonacci $numechoFibonacci Series Output Red Hat Rhel5 Enter the number of terms( 1-100 ) :10Fibonacci Series for $num terms0112358132134CONCLUSION: The following shell script generates the Fibonacci series for a given range

AIM:To write a shell program for the following:(i) Sum of series.(ii) Factorial of a number.ALGORITHM:Sum of series:· Get the range of series· intialise the sum to zero and increment the variable.· Check whether increment value is less than equal to range.

Page 4: Linux Prog

· if so calculate the sum and value.· Display the result.· Terminate the program.Factorial of a number:· Get the number to find factorial.· Initialize the variable and find the factorial.· Check whether the value is lesser than zero.· if true calculate factorial.

PROGRAM SOURCE CODE(i) Sum of digits:echo ”enter the range”read ranges=0i=1while [ $i – le $range ]dos= `expr $s + $i `i=`expr $i + 1`doneecho “the sum is : $s”

(ii) Factorial of a number:

echo “enter the number”Read ni=1j=1while [ $i –le $n ]doj=`expr $i \* $j`i=`expr $i + 1`doneecho “factorial : $j”

CONCLUSION:Thus the shell program to find the sum of series and factorial computation are written and executed successfully.

echo "Total no of factorial wants"read fact

ans=1counter=0while [ $fact -ne $counter ]do counter=`expr $counter + 1` ans=`expr $ans \* $counter`doneecho "Total of factorial is $ans"

-------------------------------------------------------------------output-------------------------------------------------------------------Total no of factorial wants5Total of factorial is 120