Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0.
Examples:
If the user input is 17, the output would be 1
If the user input is 2, the output would be 1
If the user input is 15, the output would be 0
If the user input is -2, the output would be 0
You should write and run the program using MARIE simulator. Add enough comments to understand your code.
You do not have to include the .mas file in the submission. Instead, the code should be presented as a word-processed section in the assignment, not as an image.
Insert images to show you have tested the code with several possibilities
Answers
Answer:
import java.io.*;
class prime
{
public staticvoid main ( String args [] ) throws IOException
{
BufferedReader in = new BufferedReader ( new InputStreamReader ( System . in ) );
int count = 0 ;
System. out. println ( " Enter a number " );
int n = Integer . parseInt (in . readLine ( ) );
for ( int i = 0 ; i < = n ; i + +)
{
if ( n % i = = 0 )
{
count = count + 1 ;
}
}
if ( count < = 2 )
{
System. out. println ( " prime number 1 " );
}
else
{
System. out. println ( " It is composite 0 " );
}
}
}
Answer:
Org 0
Input
Store A
Store Val2
Output
Subt one
jump testNeg
Loop, Load A
Subt Value
Skipcond 400
jump div
jump prime
div, Load Val2
Store A
if, Load A
Skipcond 400
jump else
jump prime
else, Skipcond 800
Jump Endif
Then, Load A
Subt Value
Store A
Jump if
Endif, Load Value
Add one
Store Value
jump div
testNeg, Skipcond 000
jump testZero
jump nonPrime
testZero, Skipcond 400
jump Loop
jump nonPrime
nonPrime, Load zero
Output
Halt
prime, Load Val2
Subt Value
Skipcond 400
jump nonPrime
Load one
Output
Halt
A, DEC 0
one, DEC 1
zero, DEC 0
Value, DEC 2
Val2, DEC 0
Explanation: