Tutorial 7 - Variable and Data Types





01. Write a C program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal. 

 

Answer :

 

#include <stdio.h>

int main() {

    int result = 87 - 15;

    printf("Subtracting 15 from 87 gives: %i", result);

    return 0;

}

 

 02. Identify the syntactic errors in the following program. Then type in and run the corrected program to ensure you have correctly identified all the mistakes.

 

 

#include <stdio.h>

int main (Void) 

    ( int sum; 

    /* Calculate Results*/

    sum = 25 + 37 – 19 

    /* Output Results */ 

    printf ("The answer is %i\n" sum); 

    return 0; 

}

 

 

Answer :  

aHere are the syntactic errors in the provided program:

aThe Void in int main (Void) should be lowercase, i.e., void.

aThe opening parenthesis after int main (Void) should be a curly brace {.

aThe data type INT should be in lowercase, i.e., int.

aThere is a missing semicolon at the end of the line sum = 25 + 37 – 19.

aThe closing comment marker // after DISPLAY RESULTS is placed incorrectly. It should be before the printf line.

 

 

Corrected version of the program:

#include <stdio.h>

int main(void) {

    // Calculate Results

    int sum;

    sum = 25 + 37 - 19;

    // DISPLAY RESULTS

    printf("The answer is %i\n", sum);

    return 0;

}