Find 2nd smallest digit in a given number

Write a C program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number in C.

Program in C

Code:

/*Write a C program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number Using C*/

#include<stdio.h>
#include<limits.h>
 int main ()
{
int num, reminder;
printf ("Enter the Number :");
scanf ("%d", &num);
int smallest=INT_MAX;
int Sec_Smallest=INT_MAX;

while (num > 0)
    {

reminder = num % 10;
if (smallest>=reminder)
{
       Sec_Smallest=smallest;
       smallest = reminder;
}
else if(reminder<=Sec_Smallest)
        Sec_Smallest=reminder;
num = num / 10;
    }

printf ("The Second Smallest Digit is :%d \n", Sec_Smallest);
return 0;
}

Input/Output:
Enter the Number :
54678
The Second Smallest Digit is:5

Write a C++ program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number in C++.

Program in C++

Code:

/*Write a C++ program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number using C++*/

#include<iostream>
using namespace std;
int main ()
{
int num, reminder;
cout<<"Enter the Number :";
cin>>num;
int smallest=INT_MAX;
int Sec_smallest=INT_MAX;
while (num > 0)
    {
reminder = num % 10;
if (reminder<=smallest)
{
       Sec_smallest=smallest;
       smallest = reminder;
}
else if(reminder<=Sec_smallest)
        Sec_smallest=reminder;
num = num / 10;
    }
cout<<"The Second Smallest Digit is : "<<Sec_smallest;
return 0;
}

Input/Output:
Enter the Number :
97845
The Second Smallest Digit is: 5

Write a JAVA program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number in Java.

Program in Java

Code:

/*Write a JAVA program to Find 2nd smallest digit in a given number. or Write a program to  Find 2nd smallest digit in a given number using Java*/

import java.util.*;
public class SecondSmallestNumber {

public static void main(String[] args) {
     Scanner cs=new Scanner(System.in);
     int num, reminder;
     System.out.println("Enter the Number :");
     num=cs.nextInt();
     int smallest=Integer.MAX_VALUE;
     int Sec_Smallest=Integer.MAX_VALUE;

     while (num > 0)
         {

     reminder = num % 10;
     if (smallest>=reminder)
      {
            Sec_Smallest=smallest;
            smallest = reminder;
      }
      else if(reminder<=Sec_Smallest)
             Sec_Smallest=reminder;
     num = num / 10;

         }
     System.out.println("The Second Smallest Digit is : "+Sec_Smallest);
     cs.close();
}
}

Input/Output:
Enter the Number :
989897
The Second Smallest Digit is: 8

Write a PYTHON to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number in Python.

Program in Python

Code:

'''Write a Python program to Find 2nd smallest digit in a given number. or Write a program to Find 2nd smallest digit in a given number using Python '''

import sys
print("Enter the Number :")
num=int(input())
smallest=sys.maxsize
sec_smallest=sys.maxsize
while num > 0:
    reminder = num % 10
    if smallest >= reminder:
        sec_smallest=smallest
        smallest = reminder
    elif reminder <= sec_smallest:
        sec_smallest=reminder
    num =num // 10
print("The Second Smallest Digit is ", sec_smallest)

Input/Output:
Enter the Number :
4521
The Second Smallest Digit is  2

C/C++/Java/Python Practice Question



Post a Comment

0 Comments