Posts

Showing posts from July, 2015

Shell script to find factorial of a number using recursion

A shell script is a computer program designed to be run by the Unix shell, a command line interpreter. This blog is about a shell scripting program to find the factorial of a number using recursion. Logic The logic is simple. If the number ' n ' is 1, return 1. Else return ' n ' * ' factorial(n-1) '. Program #!/bin/sh factorial() { if [ "$1" -gt "1" ]; then a=`expr $1 - 1` b=`factorial $a` c=`expr $1 \* $b` echo $c else echo 1 fi } echo "Enter a number:" read x factorial $x Things to note: - After writing the script, change the mod of the script to enable execution. This is done by: chmod +x factorial.sh or chmod 755 factorial.sh Output of the program Enter a number: 4 24 Good to read 1) Java program to convert mobile number pad to characters 2) Difference between "diff" and "sdiff" command 3) Java program to return first non repeated character in a string

Java program to convert mobile number pad to characters

Image
This post is about Java program to convert mobile number pad to characters. Example ========= Here is an image of mobile phone num pad. For every numbers except 1, there are certain character value associated with that. # is used to separate the letters. If the user presses 44#44, it should print HH Logic of the program ================ A hashmap is used to store the number and its corresponding characters. If the user presses the number n times before pressing # , the nth character is selected from the hashmap. if n is greater than number of characters, select the character which is at the position (n mod number of characters) Program ======== The java program is as follows: import java.util.HashMap; public class MobilePhoneNumPadToCharacters { public static void main(String[] args) { System.out.println(returnWords( "222#2#8" )); //CAT System.out.println(returnWords( "5555#2#888888#2222" )); /