In computing, finding the reminder of division of a number by another is called a modulo operation. To find the remainder of a divison in C, use the modulus operator %.
The function below checks whether or not the second integer received as argument is among the first number's divisors.
/* * Description: * Checks if a number is divisible by another * Parameters: * number - the dividend * divisor - integer to be checked as divisor * Returns: * 0 - if number is divisible by divisor * 1 - if number is not divisible by divisor */ int Divisor(int number, int divisor) { int i = 0; if (divisor != 0) { if (number%divisor == 0) { return 1; } } return 0; }Example 1: checking if a number is divisible by another
#include<stdio.h> #include<conio.h> int Divisor(int number, int divisor); int main(void) { int number = 0, divisor = 0, ok = -1; printf("\n Enter the number: "); scanf("%d",&number); printf(" Enter the divisor to be checked: "); scanf("%d",&divisor); ok = Divisor(number,divisor); if (ok == 0) { printf("\n\t%d is not divisible by %d.",number,divisor); } else if (ok == 1) { printf("\n\t%d is divisible by %d.",number,divisor); } getch(); return 0; }Output:
#include<stdio.h> #include<conio.h> int Divisor(int number, int divisor); int main(void) { int number = 0, i = 0, ok = 0, counter = 0; printf("\n Enter the number: "); scanf("%d",&number); if (number > 0) //if the dividend is positive { for (i=1; i<=number; i++) { ok = Divisor(number,i); if (ok == 1) { if (counter == 0) { printf("\n\t%d is divisible by: %d ",number,i); counter++; } else if (counter > 0) { printf("%d ",i); } } } } else if (number < 0) //if the dividend is negative { for (i=number; i<=-number; i++) { ok = Divisor(number,i); if (ok == 1) { if (counter == 0) { printf("\n\t%d is divisible by: %d ",number,i); counter++; } else if (counter > 0) { printf("%d ",i); } } } } getch(); return 0; }Output:
Example 3: determine the number the divisors of a given number
#include<stdio.h> #include<conio.h> int Divisor(int number, int divisor); int main(void) { int number = 0, i = 0, counter = 0, ok = -1; printf("\n Enter the number: "); scanf("%d",&number); if (number > 0) //if the dividend is positive { for (i=1; i<=number; i++) { ok = Divisor(number,i); if (ok == 1) { counter++; } } } else if (counter < 0) //if the dividend is negative { for (i=number; i<=-number; i++) { ok = Divisor(number,i); if (ok == 1) { counter++; } } } printf("\n\t%d has %d divisors.",number,counter); getch(); return 0; }
adu sabung ayam bangkok vietnam
ReplyDeleteBonus Spesial Bolavita Taruhan Bola Online Kemerdekaan dan Asian Games 2018
ReplyDeletemau yang asik ? adu ayam
ReplyDeleteTips dan Trick Bermain Judi Online
ReplyDeleteAgen Togel Singapura
Agen Togel Sidney
Agen Toto Macau
Museumbola Slot Habanero
Museumbola Slot Pulsa
Museumbola Joker123
Museumbola Livecasino
AKSES SEGERA SITUS KAMI 1 ID BANYAK PERMAINAN
WA OFFICIAL : +6283157394921