How to create a function that divides two numbers into C?

Asked

Viewed 59 times

-5

How to create a function that divides two numbers into C?

  • So 5 / 2 = 2.50 ?

  • 1

    yes, that’s right

  • yes, that’s right

  • I need a C function that divides any two numbers

  • 1

    Post some code you’ve tried to do.

  • if any response has helped you mark it as accepted, see https://i.stack.Imgur.com/evLUR.png and why https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

Show 1 more comment

3 answers

1

More briefly it is so:

float dividir(float a, float b)
{
float c=a/b;
return c;
}

This way I put function float in the case will return a float to main. There is only you do the assignment... type float d=dividir(a,b) I hope you helped him!

  • 1

    yes that’s right

  • 1

    but now as I call this function in the main int?

  • @We will create a float variable... Example: float aux=split(a,b); Assumes that a and b you have already initialized in the program. Then you can give a printf("%f",aux); to test

  • worked out, thanks

  • but I have a question, why do I need the function? I could not just do the normal program without the function?

  • Power you can yes, but in question you asked how to create a function. If it is only in the main one just make a new variable and assign the division @Iureramos float c=a/b; just in the main()!

  • ah, got it, all right then

Show 2 more comments

1

ideone

int a = 5, b = 2;
float c = 0.0;
c = (float)a/b;
printf("%.1f", c);
return 0;

%.1f returns with a decimal place %.2f with two houses and etccc

  • Hello Leo I liked your answer! At first the "%. 1f" is to write with a house after the "," correct?

  • 1

    @Iure Ramos, reply: https://ideone.com/61Bz41 :)

0


include

int f_divisao(float num1, float num2) /** function creation **/ { float result; result=num1/num2;

printf("VALOR DA DIVISÃO DE %f / %f EH: %.2f\n",num1,num2,resultado);  /** saida de dados **/

return(f_divisao);

}

int main() {

float num1,num2;

printf("DIGITE OS DOIS VALORES:\n");
scanf("%f%f",&num1,&num2);  /** entrada de dados **/

f_divisao(num1,num2);  /** retorna os valores para a função f_divisao **/

} // Look at this one I made!

Browser other questions tagged

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