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
{
// your code goes here
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
// 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
{
// your code goes here
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.
- sc.nextInt(): - this method is used to take integer input.
- 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.
- str.charAt(i) :- this function give char at i position in string (str).
- 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.
Thank you for reading this article. we appreciate your learning dedication. and if you end up like this article then please do follow on our social media handle and share this article with your friend.
Hi if you want to write a post on our website then please check this article on How to do a guest post.
0 Comments