Sunday, March 11, 2012

SUMS OF DIGITS OF AN INTEGER


SUMS OF DIGITS OF AN INTEGER


AIM:
            To write a Shell Program to calculate the sum of digits of an Integer.

ALGORITHM:

Step 1: Start

Step 2: Read the Integer as ‘num’

Step 3: Initialize the value of sum=0.

Step 4: While the value of ‘num’ is greater than 0, solve the following expressions.

Step 5: Find the remainder of ‘num’ by 10 and store in ‘y’

Step 6: Add ‘sum’ with ‘y’ and store it in ‘sum’

Step 7: Divide ‘num’ by 10 and store the value in ‘num’

Step 7: Close the while loop

Step 8: Print the Result

Step 9: Stop



SHELL PROGRAM:

echo "Enter a Number"
read num
sum=0
while [ $num -gt 0 ]
do
y=`expr $num % 10`
sum=`expr $sum + $y`
num=`expr $num / 10`
done
echo "The Sum of the Digits of the Integer is $sum"



SAMPLE OUTPUT:

[Cseb19@sathish-desktop]# sh digits_sum.sh
Enter a Number
123456
The Sum of the Digits of the Integer is 21



























RESULT:
            Thus the Shell program to calculate the sum of the digits of the integer was executed and the output was verified.

No comments:

Post a Comment