-1
#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b){
if(a%b==0){
c=1;
   cout<<"Solution Available :\t"<<c;
} else
{
    c=0;
    }   
     return c;
    } 
int main(){
    int c;
    int e,d;
    cout<<"enter two values : \n";
    cin>>e>>d;
    cout<<endl;
}

error in finding the mod of two numbers and not compiling the program :

error in finding the mod of two numbers and not compiling the program

4 Answers4

1

It compiles for me

#include<iostream>
using namespace std;
int c;
int fun_div();
int fun_div(int a,int b)
{
    if(a%b==0){
        c=1;
        cout<<"Solution Available :\t"<<c;
    } else {
        c=0;
    }   
    return c;
} 
int main(){
    int c;
    int e,d;
    cout<<"enter two values : \n";
    cin>>e>>d;
    fun_div(e,d);
    cout<<endl;
}

You should put the error message when asking about compilation errors. However I copied your code exactly and it compiles.

The other thing is that you don't call your function so I added that as well.

And as a side note, you could just do

int fun_div(int a, int b)
{
    return (a%b == 0);
}

because (a%b == 0) will evaluate to 1 if a is a multiple of b and 0 otherwise.

0
#include<iostream>
using namespace std;
int c;
int fun_div();

int fun_div(int a,int b)
{
 if(a%b==0){
 c=1;
 cout<<"Solution Available :\t"<<c;
} 
else
 { c=0; }   
return c;
} 

int main()
{
    int e,d;
    cout<<"enter two values : \n";
    cin>>e>>d;
    c=fun_div(e,d);
    cout<<endl;
}

Try this. I think this is what you expected. Please explain your question to get a more specific answer.

I have added a function call to the function fun_div.

Issac Saji
  • 106
  • 6
  • Code only answers result in [Cargo Cult Programmers](https://en.wikipedia.org/wiki/Cargo_cult_programming). Recommend an edit to explain what you changed and why. – user4581301 Nov 03 '16 at 05:35
0

you should probably add one more check for which is greater.. the greater check will ensure that there is a proper remainder available

Kushan Mehta
  • 190
  • 3
  • 11
0

Well the main problem in your code is that you are not calling the function which you defined, that is why you are not getting the desired result, And there are some better practices for writing code which you should follow to avoid errors in future.

Don't use global variable and if you are returning the result from function then show on screen from main function.

Recommended Code is given below, i have changed the function so it will only check that 'a' is divisible by 'b' or not and return the value to main so it will show the result on screen.

#include<iostream>
using namespace std;

int fun_div(int a, int b)
{
    return (a%b == 0);
}
int main() {
    int e, d;
    cout << "enter two values : ";
    cin >> e >> d;
    if (fun_div(e, d))
    {
        cout << "Solution Exists.";
    }
    else
    {
        cout << "No Solution Exists.";
    }
    cout << endl;
    return 0;
}
Tayyab Razzaq
  • 461
  • 3
  • 11