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

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com