TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C++

C++ Modulus Operator

Overview

In this tutorial, we will learn about C++ Modulus Operator
with examples.

C++ Modulus Operator

The C++ modulus operator is the % symbol. The modulus operator returns the remainder of the integer division. We can use this operator with integer operands.

Example

13%3 =  13 – (4*3)

=  13 – 12

=  1

13%3 = 1

Modulus operator illustration

C++ Program

Let’s write a simple C++ program and use the modulus operator.

//*******************************
// C++ Modulus Operator
// C++ Tutorials
// www.TestingDocs.com
//*******************************

#include 
using namespace std;

int main()
{
 //Declare integer variables
 int x = 15;
 int y = 10;

 int result = x%y;

 cout << "x = " << x << endl ;
 cout << "y = " << y << endl ;
 cout << "x%y = " << result << endl ;
 return 0;
}

C++ Modulus Operator

Output

Build and run the program in Code::Blocks. Let’s analyze the program output.

x = 15
y = 10
x%y = 5

The variable x has 15 and the variable y has 10. When we divide 15 with 10 we get 5 as the remainder.

The result of 15%10 or x%y is 5.

#

Let’s look at another example. Let’s assume the following variables:

int x = 12;

int y = 4;

12 is divisible by 4, so we get 12%4 = 0. This is a useful condition to check if the number is divisible by another number.

Exercises

# What would be the output of 16%6?

# Write a simple C++ program to check if a number is divisible by 3.  Hint:

if(x%3 ==0)
cout << “The number is divisible by 3 ” << endl ;

—

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Related Posts

g not found OpenSuse

C++ /

Install GCC C++ Compiler on OpenSuse

sum of digits c program

C++ /

Sum of Digits of a Number C++ Program

C++ Hello World Program

C++ /

C++ Hello World Program

C++ /

Object-Oriented Programming Language Examples

C++ /

Object-Oriented Programming Features

‹ C++ Decrement Operator› C++ Array Basics

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version