Factorial,Greatest Number,Palindrome String using AWK Programming
Write a Menu driven program using AWK Programming for
a) Find factorial of a number.
b) Find greatest of three numbers
c) Find a prime numbers.
d) Find whether a number is palindrome
e) Find whether a string is palindrome
f) Exit.
Factorial,Greatest Number,Palindrome AWK Programming Code
BEGIN{
print"main menu" ;
print"1.factorial of number" ;
print"2.greatest of numbers" ;
print"3.prime number" ;
print"4.palindrome of number" ;
print"5.palindrome of string" ;
print"enter ur choice" ;
getline ch<"-" ;
print ch ;
if(ch==1)
{
print "enter the number" ;
getline tmp<"-" ;
c=1 ;
while(tmp != 0)
{
c=c*tmp ;
tmp-- ;
}
print c ;
}
if(ch==2)
{
print "enter first number" ;
getline a1<"-" ;
print "enter second number" ;
getline a2<"-" ;
print "enter third number" ;
getline a3<"-" ;
if(a1>a2 && a1>a3)
print "first number is greater " ;
if(a2>a1 && a2>a3)
print "second number is greater " ;
if(a3>a1 && a3>a2)
print "third number is greater " ;
}
if(ch==3)
{
print "enter the number" ;
getline tmp<"-" ;
if(tmp==1)
print "not prime number" ;
i=tmp-1 ;
while(i > 1)
{
a=tmp%i ;
i--;
if(a == 0)
{
print "not prime number" ;
break ;
}
}
if(i==1)
print "prime number " ;
}
if(ch==4)
{
print "enter the number" ;
getline tmp<"-" ;
sd=0 ;
rev=" " ;
on=tmp ;
n=length(tmp)-1 ;
while(tmp != 0)
{
sd=tmp % 10 ;
tmp=tmp / 10;
rev=rev+(sd*(10^n)) ;
n-- ;
}
print rev ;
if(on == rev)
print "palindrome" ;
else
print "not palindrome" ;
}
if(ch==5)
{
print "enter the number" ;
getline tmp<"-" ;
p = ""
for(i=length(tmp); i > 0; i--) { p = p substr(tmp, i, 1) }
print p
if(p == tmp )
print "palindrome " ;
else
print "not palindrome" ;
}
}


Nishant says
I tried the string palindrome program, but looks like some edge case of ‘CR’ character is not handled, In my ssytem following worked fine (on ‘sh’ shell):
awk ‘{
rstr=””
for(i = length($1)-1; i!=0; –i)
rstr=rstr substr($1, i, 1);
rstr=rstr “\r”;
#print $1,rstr,length(rstr),length($1);
if($1 == rstr)
print rstr;
}’
ProjectsGeek says
Could you please let us know input you used ? and output you got ?