Practice programming questions loops
(SAMPLE SOLUTIONS AT BOTTOM OF PAGE)

  1. Write a program that inputs a positive integer from the user and then displays all the positive integers that are factors of that number.
    Hint: X is a factor of Y if and only if (Y % X) is zero.

  2. Write a function that computes N! for positive integers (i.e. 1*2*...*N) then use that function to write a program that can read two positive integers from the user, M and N, and compute N choose M.
    Note: N choose M is defined as N! / (M! * (N-M)!)

  3. Modify the program below so that it only computes the sum of elements that are on the diagonals.
    #include <cstdio>
    
    const int SIZE = 24;
    
    int main()
    {
       int sum = 0;
       int position = 0;
       for (int row = 0; row < SIZE; row++) {
           for (int column = 0; column < SIZE; column++) {
               printf("%d ", position);
               sum = sum + position;
               position++;
           }
           printf("\n");
       }
       printf( "The sum is %d\n", sum);
    }
    
    #include <iostream>
    using namespace std;
    
    const int SIZE = 24;
    
    int main()
    {
       int sum = 0;
       int position = 0;
       for (int row = 0; row < SIZE; row++) {
           for (int column = 0; column < SIZE; column++) {
               cout << position;
               sum = sum + position;
               position++;
           }
           cout << endl;
       }
       cout << "The sum is " << sum << endl;
    }
    

SAMPLE SOLUTION

  1. Factors of N
    #include <cstdio>
    
    int main()
    {
       // get the positive integer from the user
       //    (no error checking done here)
       printf( "Please enter a positive integer\n");
       int N;
       scanf("%d", &N);
    
       // for each value 1..N see if it evenly divides N
       //     i.e. remainder of 0 
       for (int n = 1; n <= N; n++) {
           if ((N % n) == 0) {
              printf("%d is a factor of %d\n", n, N);
           }
       }
       return 0;
    }
    

  2. Factorials and N choose M
    int factorial(int N) 
    {
       // result will be the product 1*2*3*...*N
       int result = 1;
       for (int n = 1; n <= N; n++) {
           result *= n;
       }
       return result;
    }
    
    void choose()
    {
       // get N and M from the user
       printf( "Please enter two positive integers\n");
       int N, M;
       scanf("%d", &N);
       scanf("%d", &M);
    
       // result = N choose M
       int result = 0;
       if (N > M) {
          result = factorial(N) / (factorial(M) * factorial(N-M));
       } 
       printf("%d choose %d is %d\n", N, M, result);
    }
    

  3. Modify the prog
    Only one change needed, shown in italics, to determine if the current position is on the forward or backward diagonal
    #include <cstdio>
    
    const int SIZE = 24;
    
    int main()
    {
       int sum = 0;
       int position = 0;
       for (int row = 0; row < SIZE; row++) {
           for (int column = 0; column < SIZE; column++) {
               printf("%d ", position); 
               if ((row == column) || ((row+column) == (SIZE-1))) 
                   sum = sum + position;
               position++;
           }
           printf("\n");
       }
       printf("The sum is %d\n", sum);
    }