Larger and smaller DEV problem of a value

Asked

Viewed 90 times

1

Can someone help me fix the higher and lower command basically it has to speak the largest number of a value and the smallest number of the same value.

Example: I add the value 727522 the program will have to read it and say that the Maior valor é 7 e menor é 2.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <iostream>

//variaveis
int ckdig (int n, int* p, int* i)
{

//Valores nas variaveis
    int d = 0;
    if(n < 0)   n = -n;
    *p = *i = 0;

    //Par e impar
    while(n)
    {
        if((n % 2))
            (*p)++;
         else 
            (*i)++;

        n /= 10;
        d++;
    }
    return d;
}

//


inline int _abs (int n)
{
    return (n < 0) ? -n:n;
}

int digits (int val, int base)
{
    register int digs = 0, n;

    n = _abs(val);

    do
    {
        n /= base;
        digs++;
    }while(n);

    return digs;
}

int main (int argc, char** argv)
{


//Variaveis
    int n, p, i, d,maior=0,menor=0,num,sobra;

//Acento

(setlocale(LC_ALL,""));

//Da um Valor a n 


printf("Digite um número:");
scanf("%d", &n);

d = ckdig(n, &p, &i);


//Escreve na tela os pares e impares   


printf("Há %d pares e %d impares.\n", p, i);

//Soma


int soma;
printf("Soma de N:");soma=0;
while(n>0)
{
    soma+=n%10;
    n/=10;
}
  printf("%d\n",soma); 

// Maior e Menor 

//encontra o maior valor
    maior    = n=0;


    for (d = 1; d < n; d++) {
        if (n> maior) {
            maior    = n;
        }
    }

    //encontra o menor valor
    menor    = n=0;
    for (d = 1; d < n; d++) {
        if (n < menor) {
            menor    = n;
        }
    }


   //Escreva na tela o maior e menor numero        
  printf("Maior valor é %d e o Menor é %d ",maior,menor);
}
  • 1

    correct the indentation of your code, otherwise it will be difficult for other people to understand and help...also, be direct (but polite) with the question, do not need to call for help, ask for help, etc

  • And also put a more explanatory title to your doubt. I, for example, can’t get any information out. Which means exactly "DEV problem" ?

  • Thanks for helping me I’m new to the forum and I want to learn more

1 answer

0

The first problem is that you destroyed the user input value during the digit sum process, so I created an auxiliary variable that receives the input value.

Then the search processes of the largest and the smallest digit are wrong you need to iterate over the digits to decide what is larger and what is smaller.

//Soma
int soma;
int aux = n;
printf("Soma de N:");soma=0;
while(aux > 0)
{
    soma+=aux%10;
    aux/=10; 
}
  printf("%d\n",soma); 

//encontra o maior e o menor valor
maior = 0;
menor = 10;
aux = n;

do 
{
     int d = aux%10;         
     if (d > maior) maior = d;
     if (d < menor) menor = d;
} while (aux/=10);

Your code running on Repl.it

  • Thanks I’ll try

  • Thank you truly you opened my mind of vdd

Browser other questions tagged

You are not signed in. Login or sign up in order to post.