Table of Contents
Approach used below is as follows to solve the problem −
Algorithm
Example
输出
Home Backend Development C++ The product of N and the largest odd number of digits in C

The product of N and the largest odd number of digits in C

Aug 29, 2023 pm 01:25 PM
Number of digits product odd number

The product of N and the largest odd number of digits in C

Given a number N with we have to product the number with its largest odd digit. If there is no odd digit then print -1.

Like we have initialized N with “153” and the largest odd digit in this number is 5 so the result would be the product of 153 with 5 i.e. 153 * 5 = 765 and if the number has no odd digit like 246 then the output must be -1.

Input − N = 198

Output − 1782

Explanation − 198 * 9 = 1782

Input − N = 15382

Output − 76910

Explanation − 15382 * 5 = 76910

Approach used below is as follows to solve the problem −

  • 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

Copy after login

Example

 演示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#include <stdio.h>

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</p><p>", findproduct(n));

   return 0;

}

Copy after login

输出

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

1

109459

Copy after login

The above is the detailed content of The product of N and the largest odd number of digits in C. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How many bits does Windows 10 Pro have? How many bits does Windows 10 Pro have? Dec 25, 2023 pm 05:37 PM

Windows 10 Professional Edition is a very easy-to-use computer system. There are both 64-bit and 32-bit versions, and the 32-bit version is better to use. What we will describe below is an introduction to the advantages of 32-bit version. How many bits is Windows 10 Professional Edition? Answer: Both 32-bit and 64-bit are available. Win10 Professional Edition is available in 32-bit and 64-bit versions. This article mainly introduces the advantages of 32-bit. Let’s take a look. 1. Excellent compatibility performance 1. First of all, we want to emphasize that Windows 10 Professional 32-bit performance is very superior in terms of compatibility. Its extremely high stability allows most existing 32-bit applications to run smoothly. 2. For you, this means enjoying the convenience brought by the latest operating system while

What is the number of bits of binary data that a computer can directly process at one time? What is the number of bits of binary data that a computer can directly process at one time? Aug 20, 2020 pm 03:44 PM

When a computer processes data, the number of bits of binary data that can be directly processed at one time is called the word length. The word length refers to the number of bits of binary data that the computer can directly process at one time. The longer the word length, the stronger the overall performance of the computer.

The sum of the squares of the first n odd numbers The sum of the squares of the first n odd numbers Aug 31, 2023 pm 08:29 PM

The series of squares of the first n odd numbers takes the square of the first n odd numbers in the series. The series is: 1,9,25,49,81,121…The series can also be written as -12,32,52,72,92,112….The sum of this series has a mathematical formula -n(2n+1)(2n -1)/3=n(4n2-1)/3For example, Input:N=4Output:sum=Explanation 12+32+52+72=1+9+25+49=84 Using the formula, sum=4 (4(4)2-1)/3=4(64-1)/3=4(63)/3=4*21=84 Both methods are good, but the method using mathematical formulas is better , which reduces the time complexity since it does not use a look

In C++, the number of digits in the nth number composed of the given four numbers In C++, the number of digits in the nth number composed of the given four numbers Aug 29, 2023 pm 03:01 PM

Weneedtofindthenumberofdigitsinthenthnumbermadeofgivenfourdigits1,2,3,and4.Theserieswiththeabovefourdigitsisasfollows1,2,3,4,11,12,13,14,21,22,23,24...Weneedtofindthenumberofdigitsofthenthnumberfromtheaboveseries.Ifyoucarefullyobservethepattern,youwi

C program to print 'even' or 'odd' without using conditional statements C program to print 'even' or 'odd' without using conditional statements Sep 15, 2023 pm 09:21 PM

In this section, we will see how to check whether a number is odd or even without using any conditional statements like <, <=, !=, >, >=, ==. We can easily check if the number is odd or even by using conditional statement. We can divide the number by 2 and check if the remainder is 0. If 0, it is an even number. Otherwise, we can AND the number with 1. If the answer is 0, it is an even number, otherwise it is an odd number. Conditional statements cannot be used here. We'll see two different ways to check whether an odd or even number is present. Method 1 Here we will create an array of strings. The index 0 position will hold "even" and the index 1 position will hold "odd". We can divide numbers

Given an odd number, find the average of all odd numbers Given an odd number, find the average of all odd numbers Sep 03, 2023 pm 03:49 PM

The average of odd numbers up to a given odd number is a simple concept. You just need to find the odd numbers up to that number, then add them and divide by that number. If you want to find the average of odd numbers up to n. Then we will find the odd numbers from 1 to n and add them together and divide by the number of odd numbers. Example The average of odd numbers up to 9 is 5, i.e. 1+3+5+7+9=25=>25/5=5 There are two ways to calculate the average of odd numbers up to n, where n is an odd number using a loop using the formula Program to find the average of odd numbers up to n, using a loop. To find the average of odd numbers up to n, we will add all the numbers up to n and divide by the number of odd numbers up to n. Program to calculate average of odd natural numbers up to n - example code

The sum of the products of each pair The sum of the products of each pair Sep 11, 2023 pm 07:33 PM

The pairwise product of the set X={a,b,c} can be defined as the sum of the products of all possible set pairs. The pairs of sets are Y={a*a,a*b,a*c,b*b,b*c,c*c}, where the products are commutative. Therefore, the pairwise product of set X is the sum of the elements of set Y, which is aa+ab+ac+bb+bc+cc. In mathematical terms, the sum of possible pairwise products can be expressed as: $$\mathrm{\displaystyle\sum\limits_{i=1,j=i}^{i\leqn,j\leqn}\:(i, j)=i\timej}$$Problem StatementGiven a number n. In the range (1, n), including n and 1, find

The product of N and the largest odd number of digits in C The product of N and the largest odd number of digits in C Aug 29, 2023 pm 01:25 PM

GivenanumberNwithwehavetoproductthenumberwithitslargestodddigit.Ifthereisnoodddigitthenprint-1.LikewehaveinitializedNwith“153”andthelargestodddigitinthisnumberis5sotheresultwouldbetheproductof153with5i.e.153*5=765andifthenumberhas

See all articles