C/Math/Divide

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Divide two integer values: how to use div_t and div

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  div_t divresult;
  
  divresult = div (28, 5);
  
  printf ("28 / 5 = %d ( %d", divresult.quot, divresult.rem);
  return 0;
}


Divide two long integer: how to use ldiv and ldiv_t

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  ldiv_t divresult;
  
  divresult = ldiv (635537,189);
  
  printf ("635537 / 189 = %d ( %d", divresult.quot, divresult.rem);
  
  return 0;
}


Quotient and remainder

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
  div_t n;
  n = div(10, 3);
  printf("Quotient and remainder: %d %d.\n", n.quot, n.rem);
  return 0;
}


Quotient and remainder for long

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
  ldiv_t n;
  n = ldiv(10L, 3L);
  printf("Quotient and remainder: %ld %ld.\n", n.quot, n.rem);
  return 0;
}


Read int value and output the divide result

  
#include <stdio.h>
int main(void)
{
  int num1, num2;
  printf("Enter first number: ");
  scanf("%d", &num1);
  printf("Enter second number (Non 0): ");
  scanf("%d", &num2);
  if(num2 == 0) 
      printf("Cannot divide by 0.");
  else 
      printf("Answer is: %d.", num1 / num2);
  return 0;
}