Showing posts with label how to write. Show all posts
Showing posts with label how to write. Show all posts

08 December 2012

Shell Script to find Armstrong number between 1 to 500

3 comments

Q. How do I write Bash Script to find Armstrong number


Ans:

#!/bin/bash
#Script to find armstrong number till 500, you can change it 
i=1
while((i<=500))
do
c=$i
d=$i
b=0
a=0
#checking each number
while((c>0))
do
#separating each digit
a=$((c%10))
#finding cube of each digit and adding them
b=$((b + a*a*a))
c=$((c/10))
done
if((b==d)); then
echo "$i"
fi
i=$((i+1))
done
Read More...

22 October 2012

Swapping two numbers using Multiplication and Divide

5 comments

Q. How do I swap two numbers without using addition and subtraction in Bash


Ans:

#!/bin/bash
read -p "Enter first number :" first
read -p "Enter second number:" sec
echo ""
echo -e "Number before swapping is $first and $sec \n"
#swapping logic
first=$((first*sec))
sec=$((first/sec))
first=$((first/sec))
echo -e "Number after swapping is $first and $sec \n"
Read More...

14 June 2012

Shell Script to find Greatest and Smallest number

3 comments

Q. How do I write shell script to find Smallest and Greatest number in an array


Ans:

#!/bin/bash
echo "enter size of an array"
read n
#taking input from user
for((i=0;i<n;i++))
do
echo " enter $((i+1)) number"
read nos[$i]
done
#printing the entered number
echo "number entered are"
for((i=0;i<n;i++))
do
echo ${nos[$i]}
done
#main loop
small=${nos[0]}
greatest=${nos[0]}
for((i=0;i<n;i++))
do
#logic for smallest number
if [ ${nos[$i]} -lt $small ]; then
small=${nos[$i]}
#logic for greatest number
elif [ ${nos[$i]} -gt $greatest ]; then
greatest=${nos[$i]}
fi
done
#printing smallest and greatest number
echo "smallest number in an array is $small"
echo "greatest number in an array is $greatest"

Read More...

20 January 2012

Shell Script to add two numbers by the use of Function

13 comments

Q. How do I write Bash Script to add two numbers using function


Ans:

#!/bin/bash
#function to add two numbers
add()
{
x=$1
y=$2
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}
# main script
echo "enter first number"
read first
echo "enter second number"
read sec
#calling function
add $first $sec
echo "end of the script"
Read More...

16 January 2012

Shell Script to Display numbers Using Array

2 comments

Q. How to use array in Bash for taking input and displaying it


Ans:

#!/bin/bash
for((i=0;i<5;i++))
do
echo "enter `expr $i + 1` number"
read arr[$i]
done
echo "the numbers you hav entered are"
for((i=0;i<5;i++))
do
echo ${arr[$i]}
done
Read More...

14 January 2012

Shell Script to find the sum of two numbers supplied as command line argument

2 comments

Q. How do I add command line arguments in Bash


Ans:

#!/bin/bash
if [ $# -ne 2 ] ; then
echo -e  " please provide correct number of arguments"
else
# $1 is first argument and $2 is second .
echo " sum of $1 + $2 is `expr $1 + $2` "
fi
Read More...

13 January 2012

Shell Script to find Greatest of Three Number using if-else Statement

30 comments

Q. How do I write Shell Script to find Greatest of Three Numbers by If-else statement


Ans:

#!/bin/bash
echo "enter first number"
read first
echo "enter second number"
read sec
echo "enter third number"
read third
if [ $first -gt $sec ] ; then
if [ $first -gt $third ] ; then
echo -e " $first is greatest number "
else
echo -e " $third is greatest number "
fi
else
if [ $sec -gt $third ] ; then
echo -e " $sec is greatest number "
else
echo -e " $third is greatest number "
fi
fi
Read More...

12 January 2012

Shell Script to Find whether a year is Leap year or not

1 comment

Q. How do I write Shell Script to find leap year


Ans:

#!/bin/bash
echo -e "enter year "
read year
#if year is less than or equal to 0 then current year is assigned to year variable
if [ $year -le 0 ] ; then
year=`date | cut -d " " -f6`
fi
if [ `expr $year % 400` -eq 0 ]; then
echo "$year is a Leap year"
elif [ `expr $year % 100` -eq 0 ]; then
echo "$year is not a Leap year"
elif [ `expr $year % 4 ` -eq 0 ]; then
echo "$year is not a Leap year"
else
echo "$year is not a leap year"
fi
Read More...

10 January 2012

Shell Script to Find Sum of Digits of a Number

1 comment

Q. How do I write Shell Script to find Sum of digits of a Number


Ans:

#!/bin/bash
echo -e "enter the number"
read n
sum=0
a=$n
while(($n >0))
do
x=`expr $n % 10`
sum=`expr $sum + $x`
n=`expr $n / 10`
done
echo "the sum of $a is $sum"
Read More...

09 January 2012

Shell Script To find Factorial of a Number

3 comments

Q. How do I write Shell Script to find factorial of a number


Ans:

#!/bin/bash
fact=1
#taking input from user
echo -e "enter a number"
read n
#if enter value less than 0
if [ $n -le 0 ] ; then
echo "invalid number"
exit
fi
#factorial logic
if [ $n -gt 0 ] ; then
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
fi
echo "The factorial of $n is $fact"
Read More...