### Ticker

20/recent/ticker-posts

# program in Java to find out the second smallest digit.

## Write a program in Java to find out the second smallest digit of a given number without using arrays

Hi, welcome to this series of data structure and algorithm made easy. today in this section we solve this program using the Java language to solve this program we should know basic of java programming like how to take input in java using Scanner of Bufferreader and how to apply conditional statement in java using if-else statement. to solve this program we define two variable one holds the value of the minimum element and one store second minimum we set these value by using if-else statement and a loop in java and one condition is given in the question that we don't use array data structure we solve this question according to question statement. this approach is a little bit tricky but doesn't worry we will do it ðŸ˜….

So let's get started with our Approach

// hear is the code of the program

import java.util.*;
import java.lang.*;
import java.io.*;

class Algomentor // your own class name

{
public static void main (String[] args) throws java.lang.Exception
{
try {
Scanner sc = new Scanner(System.in);
// number of element user gives
int n = sc.nextInt();
// then we conver this number to String
String str = Integer.toString(n);
int first = 9 ,second =9;
for(int i=0;i<str.length();i++)
{
if (Character.getNumericValue(str.charAt(i)) < first)
{
second = first;
first = Character.getNumericValue(str.charAt(i));
}
else if (Character.getNumericValue(str.charAt(i)) < second && Character.getNumericValue(str.charAt(i)) != first)
{
second = Character.getNumericValue(str.charAt(i));
}
}
System.out.println(second);
} catch(Exception e) {
} finally {
}
}
}

// hear is the output of the code

In this solution we use some predefine java method now we will talk about them.

1. sc.nextInt(): - this method is used to take integer input.
2. Character.getNumericValue():- this method take char as a perimeter and convert it into a Integer for example Character.getNumericValue('9') will proceed with output 9.
3. str.charAt(i) :- this function give char at i position in string (str).
4. System.out.println():- this function used to print the output at the screen we can pass an integer, string, double, or any other data type.
I hope you like our editorial for this question Write a program in Java to find out the second smallest digit of a given number without using arrays and hope you are learning from our series data structure and algorithm made easy. If you have any doubts regarding any concept you can contact us