Integers and Arithmetic Operations Solutions - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Integers and Arithmetic Operations Solutions

#### Integers and Arithmetic Operations Dersi Problem Çözümleri

1. We can use the `/` operator for the division and the `%` operator for the remainder:

       import std.stdio;

       void main() {
           int first;
           write("Please enter the first number: ");
           readf(" %s", &first);

           int second;
           write("Please enter the second number: ");
           readf(" %s", &second);

           int quotient = first / second;
           int remainder = first % second;

           writeln(first, " = ",
                   second, " * ", quotient, " + ", remainder);
       }

2. We can determine whether the remainder is 0 or not with an `if` statement:

       import std.stdio;

       void main() {
           int first;
           write("Please enter the first number: ");
           readf(" %s", &first);

           int second;
           write("Please enter the second number: ");
           readf(" %s", &second);

           int quotient = first / second;
           int remainder = first % second;

           // We cannot call writeln up front before determining
           // whether the remainder is 0 or not. We must terminate
           // the line later with a writeln.
           write(first, " = ", second, " * ", quotient);

           // The remainder must be printed only if nonzero.
           if (remainder != 0) {
               write(" + ", remainder);
           }

           // We are now ready to terminate the line.
           writeln();
       }

3.     import std.stdio;

       void main() {
           while (true) {
               write("0: Exit, 1: Add, 2: Subtract, 3: Multiply,",
                     " 4: Divide - Please enter the operation: ");

               int operation;
               readf(" %s", &operation);

               // Let's first validate the operation
               if ((operation < 0) || (operation > 4)) {
                   writeln("I don't know this operation");
                   continue;
               }

               if (operation == 0){
                   writeln("Goodbye!");
                   break;
               }

               // If we are here, we know that we are dealing with
               // one of the four operations. Now is the time to read
               // two integers from the user:

               int first;
               int second;

               write(" First number: ");
               readf(" %s", &first);

               write("Second number: ");
               readf(" %s", &second);

               int result;

               if (operation == 1) {
                   result = first + second;

               } else if (operation == 2) {
                   result = first - second;

               } else if (operation == 3) {
                   result = first * second;

               } else if (operation == 4) {
                   result = first / second;

               }  else {
                   writeln(
                       "There is an error! ",
                       "This condition should have never occurred.");
                   break;
               }

               writeln("       Result: ", result);
           }
       }

4.     import std.stdio;

       void main() {
           int value = 1;

           while (value <= 10) {
               if (value != 7) {
                   writeln(value);
               }

               ++value;
           }
       }

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Wed Aug 5 11:24:20 2026