C/Math/Divide

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

Divide two integer values: how to use div_t and div

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>

int main () {

 div_t divresult;
 
 divresult = div (28, 5);
 
 printf ("28 / 5 = %d ( %d", divresult.quot, divresult.rem);
 return 0;

}

      </source>


Divide two long integer: how to use ldiv and ldiv_t

<source lang="cpp">

  1. include <stdio.h>
  2. include <stdlib.h>

int main () {

 ldiv_t divresult;
 
 divresult = ldiv (635537,189);
 
 printf ("635537 / 189 = %d ( %d", divresult.quot, divresult.rem);
 
 return 0;

}

      </source>


Quotient and remainder

<source lang="cpp">

  1. include <stdlib.h>
  2. 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;

}

      </source>


Quotient and remainder for long

<source lang="cpp">

  1. include <stdlib.h>
  2. 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;

}


      </source>


Read int value and output the divide result

<source lang="cpp">

  1. 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;

}

      </source>