Sunday, March 11, 2012

NUMBER OF DIGITS IN AN INTEGER----- unix lab


NUMBER OF DIGITS IN AN INTEGER

AIM:
            To count the number of digits in an integer, using Shell Programming.

ALGORITHM:

Step 1: Start

Step 2: Read the value of ‘a’

Step 3: Initialize the value of c as 0

Step 4: When the value of ‘a’ is not equal to 0, solve n=a %10.

Step 5: If the value of ‘n’ is not equal to 0, solve c=c + 1(increment)

Step 6: End if condition

Step 7: Solve the expression “a=a / 10”

Step 8: Repeat steps 4 to 7 until ‘a’ is equal to 0

Step 9: Print the value of c as the number of digits in an integer.

Step 10: Stop.


SHELL PROGRAM:

echo "Enter a Number"
read a
c=0
while [ $a -ne 0 ]
do
n=`expr $a % 10`
if [ $n -ne 0 ]
then
c=`expr $c + 1`
fi
a=`expr $a / 10`
done
echo "The Number of Digits in the Integer is $c"


SAMPLE OUTPUT:

Enter a Number
123456789
The Number of Digits in the Integer is 9

































RESULT:     
            Thus the Shell Program to calculate the number of digits of an integer was executed and the output was verified.

No comments:

Post a Comment