Default-image

Just to provide a bit of variety in the course I thought I would also provide code for these problems in Commodore 64 BASIC from 1979, so you can see how little computers have actually changed over the decades. You will also notice that the programming languages were simpler, but also less flexible.

Simple Problems

1, A Program that prints the multiplication tables up to 10 for any given number.

10 PRINT"ENTER A NUMBER"
20 INPUTN%
30 PRINT"YOU ENTERED "; N%
35 J%=0
40 FORI=1TO10
45 J%=I*N%
50 PRINT I;" X "; N%; " = ";J%
60 NEXTI

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available() == 0) {
     //waits forever unless a serial cable is connected
  }
    Serial.println("Enter a number");
    int n = Serial.parseInt();  // This will read characters until you type a non number
    Serial.print("You entered: ");
    Serial.print(n);
    Serial.print(" the multiples of ");
    Serial.print(n);
    Serial.println(":");
    int sum=0;
    for (int i=1; i<10; i++) {
      int m=n*i;
      Serial.print(n);
      Serial.print(" X ");
      Serial.print(i);
      Serial.print("=");
      Serial.println(m);
    }
}

2. A program that adds all natual numbers before and including the input number.

10 PRINT"ENTER A NUMBER"
20 INPUTN%
30 PRINT"YOU ENTERED "; N%
35 J%=0
40 FORI=1TON%
50 J%=J%+I
60 NEXTI
70 PRINT"SUM OF NUMBERS TO ";N%;"IS ";J%
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available() == 0) {
     //waits forever unless a serial cable is connected
  }
    Serial.println("Enter a number");
    int n = Serial.parseInt();  // This will read characters until you type a non number
    Serial.print("You entered: ");
    Serial.print(n);
    Serial.println(" the sum of all integers is:");
    int sum=0;
    for (int i=1; i<n+1; i++) {
      sum=sum+i;
    }
      Serial.println(sum);
}

3. A program that adds the squares of Natural numbers.

10 PRINT"ENTER A NUMBER"
20 INPUTN%
30 PRINT"YOU ENTERED "; N%
35 J%=0
40 FORI=1TON%
50 J%=J%+I*I
60 NEXTI
70 PRINT"SUM OF SQUARES TO ";N%;"IS ";J%

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
      Serial.println("Enter a number");
  while (Serial.available() == 0) {
     //waits forever unless a serial cable is connected
  }

    int n = Serial.parseInt();  // This will read characters until you type a non number
    Serial.print("You entered: ");
    Serial.print(n);
    Serial.println(" the sum of squares is:");
    int sum=0;
    for (int i=1; i<n+1; i++) {
      sum=sum+i*i;
    }
      Serial.println(sum);
}

4. A program that swaps two numbers.

10 PRINT"ENTER A NUMBER"
15 INPUTM%
20 PRINT"ENTER ANOTHER NUMBER"
25 INPUTN%
30 PRINT"YOU ENTERED "; M%;",";N%
35 J%=M%
40 M%=N%
50 N%=J%
60 PRINT"I SWAPPED THEM ";M%;",";N%

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
      Serial.println("Enter two numbers");
  while (Serial.available() == 0) {
     //waits forever unless a serial cable is connected
  }

    int n = Serial.parseInt(); 
    int m = Serial.parseInt(); // This will read characters until you type a non number
    Serial.print("You entered: ");
    Serial.print(n);
    Serial.print(",");
    Serial.print(m);
    Serial.print(" I switched them ");
    int tmp=m;
    m=n;
    n=tmp;
    Serial.print(n);
    Serial.print(",");
    Serial.println(m);
}

5. A program the tells the bottom number on a die.

10 PRINT"ENTER A NUMBER"
15 INPUTM%
20 IFM%=1THENN%=6
30 IFM%=2THENN%=5
40 IFM%=3THENN%=4
50 IFM%=4THENN%=3
60 IFM%=5THENN%=2
70 IFM%=6THENN%=1
80 PRINT"YOU ENTERED ";M%
90 PRINT"THE BOTTOM OF THE DIE IS ";N%

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
      Serial.println("Roll and dice and enter the top number");
  while (Serial.available() == 0) {
     //waits forever unless a serial cable is connected
  }
    int n = Serial.parseInt(); 
    Serial.print("You entered: ");
    Serial.print(n);
    Serial.print(" The bottom number is ");
    int m=0;
    switch (n) {
      case 1:
        m=6;
        break;
      case 2:
        m=5;
        break;
      case 3:
        m=4;
        break;
      case 4:
        m=3;
        break;
      case 5:
        m=2;
        break;
      case 6:
        m=1;
        break;
      default:
        m=7;
        break;
    }
    if (m==7) {
      Serial.println("");
      Serial.println(" ***ERROR you entered a number not on a die*** ");
    }
    else
    {
      Serial.println(m);
    }
}

Easy Problems

1, A program that tells if a number is prime

10 PRINT"ENTER A NUMBER"
15 INPUTN
16 PRINT"YOU ENTERED ";N
20 IFN<1THENGOTO100
21 IFN=2THENGOTO200
22 IFN=3THENGOTO200
23 R=N-INT(N/2)*2
24 IFR=0THENGOTO100
25 R=N-INT(N/3)*3
26 IFR=0THENGOTO100
30 S=SQR(N)
35 I=5
36 IFI>STHENGOTO200
37 R=N-INT(N/I)*I
38 IFR=0GOTO100
39 J=I+2
40 R=N-INT(N/J)*J
41 IFR=0GOTO100
45 I=I+6
100 PRINT"THE NUMBER IS NOT PRIME"
110 GOTO 300
200 PRINT"THE NUMBER IS PRIME"
210 GOTO 300
300 PRINT"DONE"
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Enter a number and I'll tell you if it's prime:");
  while (Serial.available() == 0) {
    //waits forever unless a serial cable is attached
  }
  int n = Serial.parseInt();
  Serial.print("You entered: ");
  Serial.println(n);
  bool prime=true;
  if (n<1) {
    prime=false;
  }
  if (n==2) {
    prime=true;
  }
  if (n==3) {
    prime=true;
  }
  if ((n%3==0)&(n>3)){
    prime=false;
  }
  if ((n%2==0)&(n>2)){
    prime=false;
  }
  int s=sqrt(n);
  for (int i=5;i<s;i=i+6) {
    if (n%i==0) {
      prime=false;
    }
    if (n%(i=2)==0){
      prime=false;
    }
  }
  if (prime) {
    Serial.println("The number is prime");
  } else {
    Serial.println("The number is not prime");
  }
}

2. Calculate the distance between two points on a graph.

10 PRINT "ENTER TWO POINTS (X1,Y1) (X2,Y2)"
20 INPUT "X1:";X1
30 INPUT "Y1:";Y1
40 INPUT "X2:";X2
50 INPUT "Y2:";Y2
60 D=SQR(((X2-X1)*(X2-X1))+((Y2-Y1)*(Y2-Y1)))
70 PRINT "THE DISTANCE BETWEEN THE POINTS IS ";D
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Enter two points and I'll measure the distance between them use the format");
  Serial.println("(x1,y1),(x2,y2)");
  while (Serial.available() == 0) {
    //waits forever unless a serial cable is attached
  }
  float x1 = Serial.parseInt();
  float y1 = Serial.parseInt();
  float x2 = Serial.parseInt();
  float y2 = Serial.parseInt();
  Serial.print("You entered: (");
  Serial.print(x1);
  Serial.print(",");
  Serial.print(y1);
  Serial.print("),(");
  Serial.print(x2);
  Serial.print(",");
  Serial.print(y2);
  Serial.println(")");
  float distance=sqrt(((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
  Serial.print("The distance between the points is:");
  Serial.println(distance);
}

3. Find the least common multiple of two numbers.

10 INPUT"ENTER A NUMBER:";A
20 INPUT"ENTER A NUMBER:";B
25 MA=2:A1=A
26 MB=2:B1=B
27 PRINT "A:";A;"B:";B
30 IF A=B THEN LCD=A:GOTO130
60 IF A>B THEN GOTO 100
70 A=A1*MA
80 MA=MA+1
90 GOTO 27
100 B=B1*MB
110 MB=MB+1
120 GOTO 27
130 PRINT"LCM IS ";LCD
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // open seral port and set data rate
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Enter two numbers and I'll find their least common multiple");
  while (Serial.available() == 0) {
    //waits forever unless a serial cable is attached
  }
  int a = Serial.parseInt();
  int b = Serial.parseInt();
  int ma=2;
  int mb=2;
  int a1=a;
  int b1=b;
  while (a!=b) {
  Serial.print("A:");
  Serial.print(a);
  Serial.print(" B:");
  Serial.println(b);
    if (a<b){
      a=a1*ma;
      ma=ma+1;
    }
    else if (b<a){
      b=b1+mb;
      mb=mb+1;
    }
  }
  Serial.print("LCM IS ");
  Serial.println(a);

Medium Problem

1, Find the largest prime factor of a number

10 INPUT"ENTER NUMBER";N
20 LP=-1
30 R=N-INT(N/2)*2
40 IFR=0THENGOTO100
50 GOTO 200
100 REM CHECK/2
105 LP=2:N=N/2:GOTO30
200 REM CHECK ODD PRIMES
201 I=3
205 REM CHECK/ODD
210 IF I*I>N GOTO400
220 R=N-INT(N/I)*I
230 IFR=0THENGOTO240
235 GOTO 300
240 LP=I:N=N/I:GOTO220
300 I=I+2:GOTO210
400 REM CHECK N>2
401 IF N>2 THEN LP=N
500 PRINT"LARGEST PRIME IS:";LP

Hard Problem

1. Solve the Towers of Hanio

10 INPUT"NUMBER OF RINGS:";N
20 L$="A":M$="B":R$="C":GOSUB 100
30 END
100 IF N>1THENGOTO120
110 PRINT"MOVE DISK FROM ";L$;" TO ";R$
115 RETURN
120 N=N-1:T$=M$:M$=R$:R$=T$:GOSUB100
130 PRINT"MOVE DISK FROM ";L$;" TO ";M$
140 T$=R$:R$=M$:M$=L$:L$=T$:GOSUB100
150 T$=L$:L$=M$:M$=T$:N=N+1:RETURN

Leave a Reply

Share via
Copy link
Powered by Social Snap