
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# program to accept two integers and return the remainder
Firstly, set the two numbers.
int one = 250; int two = 200;
Now pass those numbers to the following function.
public int RemainderFunc(int val1, int val2) { if (val2 == 0) throw new Exception("Second number cannot be zero! Cannot divide by zero!"); if (val1 < val2) throw new Exception("Number cannot be less than the divisor!"); else return (val1 % val2); }
Above we have checked for two conditions i.e.
- If the second number is zero, an exception occurs.
- If the first number is less than the second number, an exception occurs.
To return remainder of two numbers, the following is the complete code.
Example
using System; namespace Program { class Demo { public int RemainderFunc(int val1, int val2) { if (val2 == 0) throw new Exception("Second number cannot be zero! Cannot divide by zero!"); if (val1 < val2) throw new Exception("Number cannot be less than the divisor!"); else return (val1 % val2); } static void Main(string[] args) { int one = 250; int two = 200; int remainder; Console.WriteLine("Number One: "+one); Console.WriteLine("Number Two: "+two); Demo d = new Demo(); remainder = d.RemainderFunc(one, two); Console.WriteLine("Remainder: {0}", remainder ); Console.ReadLine(); } } }
Output
Number One: 250 Number Two: 200 Remainder: 50
- Related Articles
- C Program to Add two Integers
- C++ Program to Find Quotient and Remainder
- C Program to Compute Quotient and Remainder?
- C# Program to return the difference between two sequences
- Java program to add two integers
- Java program to swap two integers
- Golang Program to Read Two Numbers and Print their Quotient and Remainder
- Python Program to Read Two Numbers and Print Their Quotient and Remainder
- remainder() in C++ program
- Program to multiply two strings and return result as string in C++
- How to sum two integers without using arithmetic operators in C/C++ Program?
- Program to add two binary strings, and return also as binary string in C++
- Java program to compute Remainder and Quotient
- Java Program to Compute Quotient and Remainder
- Swift Program to Compute Quotient and Remainder

Advertisements