Group B

Q8: Write a program that asks the user for a number and outputs the number squared.

Square Number

Solution and implementation for Q8 from Internet of Things (iotl).

square_number.cpp Download
void setup() {
  Serial.begin(9600);
  Serial.println("Input a number:");
}

void loop() {
  if (Serial.available() > 0) {
    int input = Serial.parseInt();
    if (input != 0) {
      int inputSquared = square(input);
      Serial.print("Squared: ");
      Serial.println(inputSquared);
    } else {
      Serial.println("Please enter a valid number.");
    }
  }
  delay(500);
}

int square(int num) {
  return num * num;
}

Other Questions in Internet of Things

See All Available Questions
Download