N与C中最大的奇数位数的乘积

2023年 8月 29日 26.8k 0

N与C中最大的奇数位数的乘积

  • Take the input N.

  • Traverse every digit and look for odd digits

  • Find the largest odd element.

  • Product the largest off element with the original number N.

  • If there is no odd element update result with -1.

  • Return the result.

  • Algorithm

    Start
    In function int largestodd(int n)
    Step 1→ Declare and Initialize large as -1
    Step 2→ Loop While n > 0
    Set digit as n % 10
    If digit % 2 == 1 && digit > large then,
    Set large as digit
    Set n as n / 10
    Step 3→ Return large
    In function int findproduct(int n)
    Step 1→ Declare and Initialize large set largestodd(n)
    Step 2→ If large == -1 then,
    Return -1
    Step 3→ Return (n * large)
    In function int main()
    Step 1→ Initialize n as 15637
    Print the results from calling findproduct(n)
    Stop

    登录后复制

    Example

    演示

    #include
    int largestodd(int n){
    // If all digits are even then
    // we wil return -1
    int large = -1;
    while (n > 0) {
    // checking from the last digit
    int digit = n % 10;
    // If the current digit is odd and
    // is greater than the large
    if (digit % 2 == 1 && digit > large)
    large = digit;
    n = n / 10;
    }
    // To return the maximum
    // odd digit of n
    return large;
    }
    int findproduct(int n){
    int large = largestodd(n);
    // If there are no odd digits in n
    if (large == -1)
    return -1;
    // Product of n with its largest odd digit
    return (n * large);
    }
    int main(){
    int n = 15637;
    printf("%d

    ", findproduct(n));
    return 0;
    }

    登录后复制

    输出

    如果运行上述代码,将会生成以下输出−

    109459

    登录后复制

    以上就是N与C中最大的奇数位数的乘积的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

    相关文章

    JavaScript2024新功能:Object.groupBy、正则表达式v标志
    PHP trim 函数对多字节字符的使用和限制
    新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
    使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
    为React 19做准备:WordPress 6.6用户指南
    如何删除WordPress中的所有评论

    发布评论