本文概述
- C ++
- Java
- Python3
- C#
例子 :
输入:N = 2346方法:一种有效的方法是找到给定数字中的所有数字, 并找到最大和最小的数字。
输出:6 2
6是最大的数字, 2是最小的输入:N = 5
输出:5 5
C ++
//CPP program to largest and smallest digit of a number
#include <
bits/stdc++.h>
using namespace std;
//Function to the largest and smallest digit of a number
void Digits( int n)
{
int largest = 0;
int smallest = 9;
while (n) {
int r = n % 10;
//Find the largest digit
largest = max(r, largest);
//Find the smallest digit
smallest = min(r, smallest);
n = n /10;
}
cout <
<
largest <
<
" " <
<
smallest;
}//Driver code
int main()
{
int n = 2346;
//Function call
Digits(n);
return 0;
}
Java
//Java program to largest and smallest digit of a number
import java.util.*;
import java.lang.*;
import java.io.*;
class Gfg
{//Function to the largest and smallest digit of a number
static void Digits( int n)
{
int largest = 0 ;
int smallest = 9 ;
while (n != 0 )
{
int r = n % 10 ;
//Find the largest digit
largest = Math.max(r, largest);
//Find the smallest digit
smallest = Math.min(r, smallest);
n = n /10 ;
}
System.out.println(largest + " " + smallest);
}//Driver code
public static void main (String[] args) throws java.lang.Exception
{
int n = 2346 ;
//Function call
Digits(n);
}
}//This code is contributed by nidhiva
Python3
# Python3 program to largest and smallest digit of a number# Function to the largest and smallest digit of a number
def Digits(n):
largest = 0
smallest = 9while (n):
r = n % 10# Find the largest digit
largest = max (r, largest)# Find the smallest digit
smallest = min (r, smallest)n = n //10print (largest, smallest)# Driver coden = 2346# Function call
Digits(n)# This code is contributed by mohit kumar 29
C#
//C# program to largest and
//smallest digit of a number
using System;
class GFG
{//Function to the largest and
//smallest digit of a number
static void Digits( int n)
{
int largest = 0;
int smallest = 9;
while (n != 0)
{
int r = n % 10;
//Find the largest digit
largest = Math.Max(r, largest);
//Find the smallest digit
smallest = Math.Min(r, smallest);
n = n /10;
}
Console.WriteLine(largest + " " + smallest);
}//Driver code
public static void Main (String[] args)
{
int n = 2346;
//Function call
Digits(n);
}
}//This code is contributed by PrinciRaj1992
【算法设计(数字的最大和最小数字)】输出如下:
6 2
推荐阅读
- 黑盒测试与白盒测试之间有什么区别()
- Win8.1提示找不到liveupdate_up_20150211.exe文件的处理办法
- Win8.1某些程序无法运行怎样办?
- Windows8自带虚拟光驱如何运用?
- Win8重命名开始屏幕图标的技巧
- Win8.1更新KB2982791补丁后蓝屏的处理方案
- Win8把关机快捷键添加到开始界面中的办法
- Win8删除桌面图标盾牌的办法
- Win8打开文件时提示没有与之关联的程序怎样办?