Q:

Write a C program to check given number is divisible by A and B

0

C program to check given number is divisible by A and B

In this C program, we will read an integer number and check whether given integer number is divisible by A and B. Here, A and B are the divisors given by the user.
Submitted by IncludeHelp, on March 27, 2018

Given an integer number number and two divisors A and B, we have to check whether number is divisible by A and B in C.

Example:

    Input:
    number = 100
    A = 10, B = 20
    Output:
    100 is divisible by 10 and 20

    Input:
    number = 90
    A = 10, B = 20
    Output:
    90 is not divisible by 10 and 20

To check whether a given number is divisible by two divisors (A and B) or not, we are creating a function named CheckDivision() which will return 1, if number is divisible by both divisors else it will return 0.

Function declaration:

 int CheckDivision(int num, int a , int b)

Here,

  • int is the return type
  • CheckDivision is the function name
  • int num is the number to divide
  • int a , int b are the divisors

Statements with function calling:

    if(CheckDivision(number,A,B))
	    printf("%d is divisible by %d and %d\n",number,A,B);
    else
	    printf("%d is not divisible by %d and %d\n",number,A,B);
 

Statement printf("%d is divisible by %d and %d\n",number,A,B); will be executed if function will return 1 else printf("%d is not divisible by %d and %d\n",number,A,B); will be executed.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check number is divisible by A and B in C

/** 
C program to check given number
is divisible by A and B
Here, A and B are integers
*/
 
#include <stdio.h>

//function will return 1 if given number is
//divisible by both numbers a and b
int CheckDivision(int num, int a , int b)
{
	//we have to check whether num is
	//divisible by a and b
	if( num%a==0 && num%b==0 )
		return 1;
	else
		return 0;
}

// main function
int main()
{
  int number=0,A=0,B=0;
    
  printf("Enter any integer number : ");
  scanf("%d",&number);
    
  printf("Enter first divisor : ");
  scanf("%d",&A);
    
  printf("Enter second divisor : ");
  scanf("%d",&B);

	if(CheckDivision(number,A,B))
		printf("%d is divisible by %d and %d\n",number,A,B);
	else
		printf("%d is not divisible by %d and %d\n",number,A,B);
	
    return 0;
}

Output

Run 1 : 
Enter any integer number : 100
Enter first divisor : 10
Enter second divisor : 20
100 is divisible by 10 and 20

Run 2:
Enter any integer number : 90
Enter first divisor : 10
Enter second divisor : 20
90 is not divisible by 10 and 20

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
Write a C program to find sum of all numbers from... >>
<< Write a C program to read name and marital status ...