Friday 26 October 2012

awk menu

BEGIN{
 menu()
}

{
 if(ch==1)
 factorial()
 if(ch==2)
 greater()
 if(ch==3)
 prime()
 if(ch==4)
 numpal()
 if(ch==5)
 strpal()
 if(ch==6)
 exit
}

#functions

function menu()
{
 system("clear")

 printf("\n\nenter your choice:")
 getline ch<"-"
}

function factorial()
{
 printf("enter the number:")
 getline a<"-"
 f=1
 if(a==0)
 print 1
 for(i=1;i<=a;i++)
 f=f*i
 printf("\nfactorial of %d is :%d",a,f)
 getline z"<"
 menu()

}

function greater()
{
 printf("enter the value of a:")
 getline a<"-"
 printf("enter the value of b:")
 getline b<"-"
 printf("enter the value of c:")
 getline c<"-"
 if(a>b && a>c)
 printf("\n%d is greatest",a)
 else if(b>a && b>c)
 printf("\n%d is greatest",b)
 else
 printf("\n%d is greatest",c)
 getline z<"-"
 menu()
}

function prime()
{
 printf("enter the number:")
 getline a<"-"
 i=2
 flag=1
 while(i<a)
 {
  if(a%i==0)
  {
   flag=0
   break
  }
  i++
 }
 if(flag==1)
 printf("\nit is a prime number")
 else
 printf("\nnot a prime number")
 getline z<"-"
 menu()
}

function strpal()
{
 printf("enter the string:")
 getline str<"-"
 i=1
 flag=0
 len=length(str)
 while(i<len)
 {
  ch1=substr(str,i,1)
  ch2=substr(str,len,1)
  if(ch1!=ch2)
  {
   flag=1
   break
  }
  i++
  len--
 }
 if(flag==0)
 printf("\nstring is palindrome")
 else
 printf("\nnot a palindrome")
 getline z<"-"
 menu()
}

function numpal()
{
 printf("\nenter a number:")
 getline num<"-"
 rev=0
 org=num
 while(num>0)
 {
  r=int(num%10)
  num=int(num/10)
  rev=int(rev*10+r)
 }
 if(rev==org)
 printf("\nthe number is a palindrome")
 else
 printf("\nthe number is not a palindrome")
 getline z<"-"
 menu()
}

No comments:

Post a Comment