You are on page 1of 4

Number divisible by 5:

#!/bin/bash echo "Enter a number" read number if [ $(( $number % 5 )) -eq 0 ] ; then echo "Your number is divisible by 5" else echo "Your number is not divisible by 5" fi

PRIME NUMBER OR NOT

echo -n "Enter a number: " read num i=2 while [ $i -lt $num ] do if [ `expr $num % $i` -eq 0 ] then echo "$num is not a prime number" echo "Since it is divisible by $i" exit fi i=`expr $i + 1` done echo "$num is a prime number "

GCD OF TWO NUMBERS ( GREATEST COMMON DIVISOR)

echo enter two numbers read n1 n2 remainder=1 # Preserve numbers for future use t1=$n1 t2=$n2 if [ $n2 -eq 0 ] then echo "GCD of $n1 and $n2 = $n1" exit 0

fi while [ $remainder -ne 0 ] do remainder=`expr $n1 % $n2` # or use #remainder=$((n1%n2)) n1=$n2 n2=$remainder done echo "GCD of $t1 , $t2 is $n1"

To print first 5 odd numbers


echo Enter a 5 digit number read num n=1 while [ $n -le 5 ] do a=`echo $num | cut -c $n` echo $a n=`expr $n + 2` done

To print first 5 even numbers (please check with execution)


echo Enter a 5 digit number read num n=1 while [ $n -le 5 ] do a=`echo $num | cut -c $n` echo $a n=`expr $n + 1` done

PROGRAM TO PRINT FIRST N NATURAL NUMNERS DIVISIBLE BY 3 BUT NOT DIVISIBLE BY 4


#include<stdio.h> #include<conio.h> void main() { int i,n; clrscr(); printf(Enter the limit: ); scanf(%d,&n); printf(Numbers upto %d which are divisible by 3 and not by 4 are: \n,n); for(i=1;i<=n;i++) { if((i%3==0)&&(i%4!=0)) printf(%d ,i); } getch(); }

Factorial of given number:


echo "Enter a number: " read num i=2 res=1 if [ $num -ge 2 ] then while [ $i -le $num ] do res=`expr $res \* $i` i=`expr $i + 1` done fi echo "Factorial of $num = $res"

Shell program to count Vowels, blank spaces, characters, number of line and symbols A, E, I, O, U
#!/bin/bash # Shell program to count # Vowels, blank spaces, characters, number of line and symbols # A, E, I, O, U # -------------------------------------------------------------------# This is a free shell script under GNU GPL version 2.0 or above # Copyright (C) 2005 nixCraft project. # Feedback/comment/suggestions : http://cyberciti.biz/fb/ # ------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------file=$1 v=0 if [ $# -ne 1 ] then echo "$0 fileName" exit 1 fi if [ ! -f $file ] then echo "$file not a file" exit 2 fi # read vowels exec 3<&0 while read -n 1 c do l="$(echo $c | tr '[A-Z]' '[a-z]')" [ "$l" == "a" -o "$l" == "e" -o "$l" == "i" -o "$l" == "o" -o "$l" == "u" ] && (( v++ )) || : done < $file echo echo echo echo "Vowels : $v" "Characters : $(cat $file | wc -c)" "Blank lines : $(grep -c '^$' $file)" "Lines : $(cat $file|wc -l )"

You might also like