- Published on
Level 2 Solutions
Table of Contents
Assignment 1
Check if a number is prime.
int x, y;
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) { //Check for serial data
x = Serial.parseInt(); //Read Serial input
Serial.println(x);
for (int i = 2; i < x; i++) { //For loop dividing the number by each number between 0 and itself
if (x % i == 0) { // If it divides then toggles y to 1
y = 1;
}
}
if (y==1)
Serial.println("It is not a prime number");
else
Serial.println("It is a prime number");
y=0;
}
}
Enter a prime number and see!
Assignment 2
Encrypt and decrypt an integer input using XOR
//Initialize variables
char x = 'F';
int y, z;
void setup()
{
Serial.begin(9600);
Serial.print("Enter the message to encrypt/decrypt");
}
void loop()
{
if (Serial.available() > 0) //Check for Serial data
{
y = Serial.parseInt(); //Read serial data and store in a variable
Serial.print("Your integer is: ");
Serial.println(y);
z = x ^ y; //XOR operation
Serial.print("The encrypted/decrypted code is :");
Serial.println(z); //Print the encrypted/decrypted value
}
}
Start encrypting your messages now on!
Assignment 3
Black box code, analyze the waveform.
Explanation
As you enter a value between 0 to 255, the time duration for which the output is HIGH is proportional to the number entered. That means average output voltage would be higher if the entered number is greater. The formula is .