if else statement

Hi! I'm new to C++ and I was wondering how to get a statement using selection statements (if and else). Like, a statement that is executed in case condition is true, and in case it is not, a different statement is executed.

If the output doesn't have a remainder, this statement will show up on the output (This is how should my output look like. For example:


x divides the number y.
x/y = z



If the output has a remainder:

x does not divide the number y.
x/y = z.r 
 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
using namespace std;

int main (){
	
	int number, divisor, qoutient, remainder;
	
	cout<<"Please enter a number: ";
	cin>>number;
	
	cout<<"Please enter a divisor: ";
	cin>>divisor;
	
	qoutient = number / divisor;
	remainder = number % divisor;

	//if the qoutient doesn't have a remainder, this statement will show up on the output
	if (qoutient>1){
	cout << "\n" << divisor << " divides the number " << number << "." << endl;
	cout << number <<"/"<< divisor << "=" << qoutient << endl;		
	}
	
	//if the qoutient has a remainder, this statement will show up instead of the other statement without the remainder
	else (remainder){
	cout << "\n" << divisor << " does not divide the number " << number << "." << endl;
	cout << number <<"/"<< divisor << "=" << qoutient << "." << remainder << endl;	
	}

	return 0;
}






Last edited on
You're so close!

else does not have a condition (as in, no parentheses or anything in between them). It runs if the if statement immediately above it had a false condition (and thus didn't run).

Also, your condition on line 18 needs to be tweaked. It might help to reread your comment just above it:
if the qoutient doesn't have a remainder


-Albatross
@albatross Thank you so much for the help !!
elseif is not provided in c++ because you can chain them. If you need this functionality, just combine ifs and elses:

if (something)
blah;
else if (somethingelse) //else if is just putting 2 statements on one line. its not a thing in c++
meh;
else
default;

Last edited on
@jonnin thank you! it worked!
Topic archived. No new replies allowed.