Skip to content

10.31 Tutorial3

intro-to-computer-science-gtiit-tutorial-03-2024.pdf

Introduction to Computer Science

Tutorial 3: While and Do-While Loops, Logic Operators

1. Manual execution

1.1 Your task is to perform the manual execution of the following programs with iteration.

/* 1.1 */
int i = 0;
int a = 0;
while (i <= 5){
    a = a + i;
    i = i + 1;
}

/* 1.2 */
int i = 0;
int a = 0;
while (i < 4){
  if (i % 2 == 0)
    a = a + 1;
  else
    a = a - 1;
  i = i + 1;
}

/* 1.3 */
int i = 0;
int a = 8;
do {
  i = i + 5;
  a = a - 3;
} while (i < a);

/* 1.4 */
int b = 7;
int c = 0;
if (b > 9 || c < 5)
  c = c + 1;
else if (b > 5)
  c = c + 2;
else
  c = c + 3;
}

/* 1.5 */
int a = 10;
int c = 0;
if (a <= 10){
  while(a > 0)
    a = a - 5;
  c = c + 2;
}

2. Printing sequences

2.1

Write a program that prints the sequence of numbers from 1 to 50 in steps of 7.

#include <stdio.h>
int main() {
    int i = 1;
    while (i<=50) {
        printf("%d\n",i);
        i=i+7;
    }
    return 0;
}

2.2

Write a program that asks for values a​, b​, c​ from the user, and prints out the values from a​ to b​ in steps of c​.

#include <stdio.h>
int main() {
    int a, b, c;
    printf("Please enter the value of a, b and c");
    scanf("%d %d %d", &a, &b, &c);
    while (a<=b) {
        printf("%d\n",a);
        a=a+c;
    }
    return 0;
}

3. Input validation

3.1

Write a program that asks for an integer value x​ from the user, such that x​ belongs to the interval [0, 100]. If the user provides a negative value or a value greater than 100, the program asks for another value. Then, the program prints out the number of digits in x​.

Consider using a do-while statement when appropriate.

#include <stdio.h>
int main() {
    int x, t = 1;
    do {
        printf("Please enter an integer x (0 <= x <= 100): ");
        scanf("%d", &x);
    } while (x < 0 || x > 100);
    int q = x;
    while (q >= 10) {
        q = q / 10;
        t = t + 1;
    }
/*    for (int q = x / 10; q > 0; q = q /10) {
        t++;
    }
*/
    printf("The number of digits in x is %d\n", t);

    return 0;
}

4. A Program with Some Errors

4.1

The following C program compiles without error but does not behave as expected. It is expected that the authorized​ variable is set to 1​ only in cases where the age of the person allows it (age should be greater than or equal to 17 to get authorization).

Your task is to fix the program.

int main() {
    int age;             // Get from input.
                         // Age must be between 0 and 120.
    int authorized = 0;  // 0 by default. Change to 1 to authorize.

    do{
        scanf("%d", &age);
    } while (0 <= age <= 120);

    if (age < 16){
        printf("Cannot authorize.\n");
    } else if (age = 17) {
        printf("Authorized with warning.\n");
    } else {
        printf("Authorized.\n");
    }

    authorized = 1;

    return 0;
}

Modified

#include <stdio.h>
int main() {
    int age;             // Get from input.
                         // Age must be between 0 and 120.
    int authorized = 0;  // 0 by default. Change to 1 to authorize.

    do{
        scanf("%d", &age);
    } while (age < 0 || age > 120);

    if (age <= 16){
        printf("Cannot authorize.\n");
    } else if (age == 17) {
        printf("Authorized with warning.\n");
    } else {
        printf("Authorized.\n");
    }

    authorized = 1;

    return 0;
}
int main() {
    int age;             // Get from input.
                         // Age must be between 0 and 120.
    int authorized = 0;  // 0 by default. Change to 1 to authorize.

    do{
        scanf("%d", &age);
    } while (0 <= age <= 120);

    if (age < 16){
        printf("Cannot authorize.\n");
    } else if (age = 17) {
        printf("Authorized with warning.\n");
    } else {
        printf("Authorized.\n");
    }

    authorized = 1;

    return 0;