Skip to content

11.1 Lab3

intro-to-cs-laboratory-3.pdf

Introduction to Computer Science

Laboratory 3 November 1, 2024

Exercises

  1. Write a C program that asks the user for an integer value n​ and then prints the multiplication table for n​.

    Example: For n = 3​, the output should be:

    3 * 0 = 0 3 * 1 = 3 ... 3 * 10 = 30

    #include <stdio.h>
    int main() {
        int n, t=0;
        scanf("%d",&n);
        while (t<11) {
            printf("%d * %d =%d\n", n, t, n*t);
            t++;
        }
        return 0;
    }
    
  2. Write a C program that asks the user for an integer n​ and then prints all of the digits of n​. Utilize a do-while​ loop.

    Hint: Use the modulo operation %​ to get the last digit of a number.

    #include <stdio.h>
    #include <math.h>
    
    int main() {
        // number needs user to input, digit is output digit at one time, times defines both the digits of number and how many times do the loop run //
        int number, digit, times = 1;
        scanf("%d", &number);
    
        // calculate the digits of a number, q is 计数器 //
        int q = number;
        while (q >= 10) {
            q = q / 10;
            times++;
        }
    
        // calculate the seperate digit //
        do {
            // because (int)pow(10,0)==0, thus we need to use if to fix this problem //
            if ((int)pow(10, times - 1) == 0) {
                // this only happen when we have 1 digit number //
                digit = number % (int)(pow(10, times));
            } else {
                // for example, (1234%1000-1234%100)/100=2 //
                digit = (number % (int)(pow(10, times)) - (number % (int)(pow(10, times - 1)))) / (int)(pow(10, times - 1));
            }
            printf("%d ", digit);
            times = times - 1;
        } while (times != 0);
    
        return 0;
    }
    
  3. Write a C program that asks the user for a number n​ and then prints the first n​ even numbers.

    #include <stdio.h>
    
    int main() {
        int n, number=0;
        scanf("%d", &n);
        while (n!=0) {
            printf("%d", number);
            number=number+2;
            n--;
            }
        return 0;
    }
    
  4. Write a C program that asks the user for an integer n​ and then prints the factorial of n​.

    #include <stdio.h>
    
    int main() {
        int n, number=1;
        scanf("%d", &n);
        while (n!=0) {
            number=number*n;
            n--;
            }
        printf("%d", number);
        return 0;
    }
    
  5. Write a C program that uses the rand​ function to generate a random number r​ (from 0 to 100). Then, the program should ask the user to enter numbers until they guess r​. For each attempt, the program should indicate if the attempt is greater or lower than r​.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        int n, random;
        srand(time(0));
        random=rand()%100;
        do {
            printf("Please enter a number from 0 to 100: ");
            scanf("%d", &n);
            if (n>random) 
                printf("Too big!\n");
            else if (n<random)
                printf("Too small!\n");
            else 
                printf("Yes, you are right!\n");
        } while (n != random);
    
        return 0;
    }
    

    A more advanced version

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        int n, random, max = 100, min = 0;
        srand(time(0));
        random = rand()%100;
        do {
            printf("Please enter a number from %d to %d: ", min, max);
            scanf("%d", &n);
            if (n > random) {
                max = n;
                printf("Too big! Try again!\n");
            }
            else if (n < random) {
                min = n;
                printf("Too small! Try again!\n");
            }
            else 
                printf("Yes, you are right!\n");
    
        } while (n != random);
    
        return 0;
    }
    
  6. Modify the last C program to limit the number of guessing attempts to 5.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
        int n, random, max = 100, min = 0, times=5;
        srand(time(0));
        random = rand()%100;
        do {
            printf("%d attempts, please enter a number from %d to %d: ", times, min, max);
            scanf("%d", &n);
            if (n > random) {
                max = n;
                printf("Too big! Try again!\n");
            }
            else if (n < random) {
                min = n;
                printf("Too small! Try again!\n");
            }
            else 
                printf("Yes, you are right!\n");
            times--;
        } while (n != random && times!=0 );
        printf("Game over!\n");
        return 0;
    }
    
  7. Write a C program that asks the user for an integer number n​ and prints the n​th number of the Fibonacci sequence.

    Example: For n = 6​, the output should be: 0, 1, 1, 2, 3, 5, 8

    #include <stdio.h>
    
    int main() {
        int n, sum_old=0, sum_new=1, x;
        printf("Please enter a number to generate the nth number of the Fibonacci sequence:");
        scanf("%d", &n);
        printf("0 1 ");
        while ((n-1) != 0) {
            //memorize sum_new because it will change but we need its later//
            x=sum_new;
            sum_new=sum_new+sum_old;
            //give it back//
            sum_old=x;
            n--;
            printf("%d ", sum_new);
        }
        return 0;
    }
    
  8. How to determine a prime number or composite number?

    #include <stdio.h>
    
    int main() {
        int n, divisor=2;
        printf("Please enter a number and I will tell whether it is a prime or not: ");
        scanf("%d", &n);
        while (divisor < n) {
            if (n % divisor != 0) {
                divisor++;
            }
            else {
                printf("%d is a coposite number!", n);
                return 0;
            }
        }
        printf("%d is a prime number!", n);
        return 0;
    }
    
  9. How to calculate the greatest common divisor?

    #include <stdio.h>
    
    int main() {
        int x, y, gcd = 1, divisor = 2, max;
        printf("Please enter two numbers and I will calculate the greatest common divisor:");
        scanf("%d %d", &x, &y);
        // determine what the largest divisor should be
        if (x <= y) {
            max = x;
        }
        else {
            max = y;
        }
        while (divisor <= max) {
            if ((x % divisor) != 0 || (y % divisor) != 0) {
                divisor++;
            }
            else {
                gcd = divisor;
                divisor++;
            }
        }
        printf("The greatest common divisor between %d and %d is %d ", x, y, gcd);
    }