Computer Science, asked by angelicsoul482, 6 months ago

write a program in Java to demonstrate the use of modulus operator.

I will mark u brainlist ONLY for correct answer!!!!! ​

Answers

Answered by khushant90
6

Answer:

I am sure you would be familiar with the term Modulus. It is also one of the most commonly asked interview questions for C, C++, Java or Python. In this article, I am going to brush up the definition of modulus to you followed by the implementation process through Java.

Have a look at the agenda of this article:

What is a Modulus operator in Java?

Syntax

Example of Modulus in Java

Let’s begin!

What is a Modulus operator in Java?

The modulus operator returns the remainder of the two numbers after division. If you are provided with two numbers, say, X and Y, X is the dividend and Y is the divisor, X mod Y is there a remainder of the division of X by Y.

Let me make you guys familiar with the technical representation of this operator.

Modulus in Java: Syntax

X%Y

Where X is the dividend and Y is divisor.

So simple, isn’t it?

Now that you are well acquainted with the syntax, I am going to use it in the Java program.

Example of Modulus in Java

class ModulusOperator{

public static void main(String[] args){

int num1,num2,result;

num1=26;

num2=15;

System.out.println("num1=26; num2=15");

result=num1%num2;

System.out.println("The result after modulus operation is : "+result);

}

}

Output:

num1=26 num2=15

The result after modulus operation is: 11

Here, you can see how easy it is to implement this concept in the Java program.

You can also use the modulus operator in finding whether a number is even or odd. Let’s see how!

package com.nullbeans.basics;

public class Main {

public static void main(String[] args) {

int userInput = 32232;

isEvenNumber(userInput);

}

public static boolean isEvenNumber(int userInput){

if(userInput%2 == 0){

System.out.println("Input is an even number");

return true;

}else {

System.out.println("Input is an odd number");

return false;

}

}

}

Hence, you can see that the modulus operator can be used in many cases. This Modulus in Java can also be used to check whether a number is a prime number or not, or applications that calculated the remaining sum of amounts or likewise.

Explanation:

pLz mark me as brainlist, please and thank my all answer

Answered by CopyThat
1

Program :-  [JAVA]

public class DivisionOperator

{

public static void main(String args [ ] )

{

int x = 18;

float a = 22.3f;

int b = 20;

System.out.println(''10 % 3 results in '' + 10 % 3);

System.out.println(''x % 4 results in '' + x % 4);

System.out.println(''a % b results in '' + a % b);

}

}

Output :-

10 % 3 results in 1

x % 4 results in 2

a % b results in 2.2999992

Learn more :-

Write a program to demonstrate the use of addition operator.

https://brainly.in/question/21967050

Similar questions