### Solved Programs

This section contains problems along with their answers .

##### Q) Write a program to convert number into word. ```Example : Input -234 Output-Two Thee Four ```
``````
import java.util.*;  //importing package
class Convert //class name
{  //opening class

Scanner ob = new Scanner(System.in); //creating object of scanner class
int n;    //initializing n

Convert() //  non parameterize constructor
{          //opening constructor
n = 0;  //initializing n with 0
}          //closing constructor

void input() throws Exception //method to input value
{
System.out.print("Enter the Number:");
n = ob.nextInt();
}

void extdigit(int a) //method to covert number to word by extracting each digit
{
int dig = 0;   //extra variable
if (a == 0) //condition
{
return;
} else {
dig = a % 10;
extdigit(a / 10);  //recalling method
num_to_word(dig); //calling method which convert no. to word
}
}

void num_to_word(int b) //fuction to covert number to word
{
switch (b) {

case 0:
System.out.print("Zero ");   //if digit is 0 then print zero
break;//break

case 1:
System.out.print("One ");    //if digit is 1 then print one
break;

case 2:
System.out.print("Two ");   //if digit if 2 then print two
break;
case 3:
System.out.print("Three ");   //if digit if 3 then print three
break;
case 4:
System.out.print("Four ");   //if digit if 4 then print four
break;
case 5:
System.out.print("Five ");   //if digit if 5 then print five
break;
case 6:
System.out.print("Six ");    //if digit if 6 then print six
break;
case 7:
System.out.print("Seven ");   //if digit if 7 then print seven
break;
case 8:
System.out.print("Eight ");   //if digit if 8 then print eigth
break;
case 9:
System.out.print("Nine ");   //if digit if 9 then print nine
break;
}
}

public static void main(String ar[]) //main method
{
System.out.print("Enter Number :");    //input a number

Convert ob1 = new Convert();
int c = ob1.ob.nextInt();//creating object of convert class
ob1.extdigit(c);   //method calling
}   //closing main
}   //closing class
``````
```Enter Number :2335
Two Three Three Five```

##### Q) Write a Program for checking whether a string is palindrome or not using Recursion. ```E.g. NITIN is a palindrome String because if reverse String we get same as primary String ```
``````
import java.util.*;   //importing package
class pallindrome //class name
{//opening class

Scanner ob = new Scanner(System.in);   //creating object of scanner class
String str;//intializing
String Revst; // string to store reverse of original string

pallindrome() //non parameterize constructor
{
str = null;   //initializing str with null
Revst = " "; //initializing with empty
} //closing constructor

void input() throws Exception //method to input value
{
System.out.print("Enter the sentence:");
str = ob.nextLine();
Revst = " ";
recReverse(str.length() - 1);
}

void recReverse(int a) {
if (a < 0) {
return;
} else {
Revst = Revst + str.charAt(a);
recReverse(a - 1);
}
}

void check() //mehtod to check pallindrome string
{
Revst = Revst.trim();// to remove space from beginning
System.out.println("Entered String :" + str);
System.out.println("Reverse String :" + Revst);

if (str.equals(Revst)) //if revst is excactly equal to str
{
System.out.print("it is pallindrome string");   //then it is pallindrome string
} else//else
{
System.out.print("it is not pallindrome string");  //it is not a pallindrome string
}
}   //cloasing function

public static void main(String arr[]) throws Exception //main method
{//opening main
pallindrome ob1 = new pallindrome();    //creating object of pallindrome class
ob1.input();   //calling method to input
ob1.check();  //calling method to check
}  //closing main
}  //closing class
``````
```Enter the sentence:NITIN
Entered String :NITIN
Reverse String :NITIN
it is pallindrome string```

##### Q) Write a program accept a sentence from user and print the initial of word in sentence except last word of a sentence and print in following format ```Input- sachin ramesh tendulkar Output - TENDULKAR S. R. ```
``````
import java.util.*;  // importing util package
class initials  //class name
{
public static void main (String arg[])throws Exception
{
Scanner ob=new Scanner (System.in);  //creating object of scanner class
System.out.print("Enter the Sentence");  //inputting value from user
String s=ob.nextLine();
s=s.toUpperCase();    //converting into upper case
int l=s.lastIndexOf(' ');   //initialing l with last index of space
String s1=s.substring(l);
s=" "+s;//adding space in at index -1
System.out.print(s1+" ");  //printing modified string

for(int i=0;i<=l;i++)   //loop till last index of space
{
char ch=s.charAt(i);  //extracting each character
if(ch==' ')
{
char chh=s.charAt(i+1);
if(Character.isLowerCase(chh))
{
System.out.print((char)(chh-32)+"."+" ");
}
else
System.out.print((chh)+"."+" "); //printing string
}
}
}  // closing main
} //closing class
``````
```Enter the Sentence
Sachin Ramesh Tendulkar
TENDULKAR S. R.```

##### Q)Write a Program to print a circular Matrix. ```E.g. if a number is 3 Then output is- 1 2 3 8 9 4 7 6 5 ```
``````
import java.util.*;   //importing util pckage

class circularMatrix //name of class
{  //opening class
public static void main(String[] args) //main function
{   //openiing main
Scanner ob = new Scanner(System.in);   //creating object of scanner class
System.out.print("Enter the number ");// input from a user
int n = ob.nextInt();
int t = n;   //coppying value of n to t
int[][] s = new int[n][n];  //declearing matrix n*n
int r = (n % 2 == 0) ? (n / 2) : ((n + 1) / 2);
int c = 1; //extra variable to perform specific operation
int x = 0;  //temporary variable initializing with 0
int y = 0;  //temporary variable initializing with 0
for (int i = 1; i <= r; i++) //loop
{
for (int j = 1; j <= (4 * n - 4) || (j == 1); j++) //loop for rotation
{
s[x][y] = c;//
c++;
if (j < n || j == (4 * n - 4)) {
y++;//increasing y
} else if (j < (2 * n - 1)) {
x++;//increasing x
} else if (j < (3 * n - 2)) {
y--;//decreasing y
} else if (j < (4 * n - 4)) {
x--;//decreasing x
}
}
n = n - 2;
}

for (int i = 0; i < t; i++)//loop to print
{
for (int j = 0; j < t; j++) {
System.out.print(s[i][j] + "\t");//printing whole matrix
}
System.out.println();
}
}//closing main
}//closing class
``````
```Enter the number 4
1	2	3	4
12	13	14	5
11	16	15	6
10	9	8	7	```

##### Q) Write a Program to print a spiral matrix. ```E.g. if the number is 3 Then output is- 9 8 7 2 1 6 3 4 5 ```
``````
import java.util.*; //importing a util package

class spiralMatrix //name of the class
{//opening class

public static void main(String[] args) { //opening main
Scanner ob = new Scanner(System.in);//creating object of scanner class
System.out.print("Enter the number "); //input a number from user
int n = ob.nextInt();			//accepting size of array
int t = n;
int[][] s = new int[n][n];		//declaring array
int r = (n % 2 == 0) ? (n / 2) : ((n + 1) / 2);	//calculating rotations
int c = n * n;					//initializing first value
int x = 0;
int y = 0;
for (int i = 1; i <= r; i++) //loop
{
for (int j = 1; j <= (4 * n - 4) || (j == 1); j++)//loop for rotation
{
s[x][y] = c;//transfersing values to matrix
c--;
if (j < n || j == (4 * n - 4)) {
y++;
} else if (j < (2 * n - 1)) {
x++;
} else if (j < (3 * n - 2)) {
y--;
} else if (j < (4 * n - 4)) {
x--;
}
}
n = n - 2;
}
for (int i = 0; i < t; i++)//loop for printing
{
for (int j = 0; j < t; j++) {
System.out.print(s[i][j] + "\t");// massage displayed
}
System.out.println();//changing line
}
}//end main
}//end class
``````
```Enter the number 4
16	15	14	13
5	4	3	12
6	1	2	11
7	8	9	10	```

##### Q) Write a program to print a febnoci series . `Febnoci series - 0 1 1 2 3 5 8 13â?¦.`
``````
import java.util.*; //importing package

class febnoci //class name
{//opening class

int print(int step) //function to print the steps of series
{
if (step <= 1) //step is <0
{
return step;
} else {
return (print(step - 1) + print(step - 2));  //recalling the function using recursion
}
}

public static void main(String arg[]) //main function
{  //opening main
Scanner o = new Scanner(System.in);
febnoci ob = new febnoci();  //creating a object of class created erlier
System.out.print("Enter the Number:");
int n = o.nextInt();
for (int i = 0; i <= n; i++)//loop to print
{  //opening loop
System.out.print("\t" + ob.print(i));  //printing values of series
}//closing loop
}//closing main
}//closing class
``````
```Enter the Number:5
0	1	1	2	3	5```

##### Q) Write a program to print a pattern. ```Input : computer Output- computer omputer mputer puter uter ter er r r re ret retu retup retupm retupmo retupmoc ```
``````
import java.util.* ;  //importing util package
class Pattr //class name
{//opening class
public static void main(String args[])throws Exception
{//opening main
Scanner ob=new Scanner(System.in);  //creating object of scanner class
System.out.println("Enter sentence");  //input string
String s=ob.nextLine();
int l=s.length(); //length of string in int a
for(int i=0;i=0;i--) //2nd loop
{
for(int j=l-1;j>=i;j--) //nested loop
{
System.out.print(s.charAt(j)); //printing
}
System.out.println(); //going to next line
}}}//closing loop/closing mai//closing class

``````
```Enter sentence
Computer

Computer
omputer
mputer
puter
uter
ter
er
r
r
re
ret
retu
retup
retupm
retupmo
retupmoC```