Tutorial 7 - Variables and Data Types

💡Question 1

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;

}

💡Question 2

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; 

    /* COMPUTE RESULT 

    sum = 25 + 37 – 19 

    /* DISPLAY RESULTS // 

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

    return 0; 

}

Answer 

Here are the syntactic errors in the provided program:

  • The Void in int main (Void) should be lowercase, i.e., void.
  • The opening parenthesis after int main (Void) should be a curly brace {.
  • The data type INT should be in lowercase, i.e., int.
  • There is a missing semicolon at the end of the line sum = 25 + 37 – 19.
  • The 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) {

    // COMPUTE RESULT

    int sum;

    sum = 25 + 37 - 19;

    // DISPLAY RESULTS

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

    return 0;

}

💡Question 3

What output might you expect from the following program?

#include <stdio.h>

int main (void)

{

    int answer, result;

    answer = 100;

    result = answer - 10;

    printf ("The result is %i\n", result + 5);

    return 0;

}

Answer

  • answer is initialized to 100.
  • result is assigned the value of answer - 10, which is 100 - 10 = 90.
  • The printf statement prints "The result is %i\n", where %i is a placeholder for an integer. It prints the value of result + 5.
  • result + 5 is 90 + 5 = 95.

💡Question 4

Which of the following are invalid variable names? Why?

Int char 6_05
Calloc Xx alpha_beta_routine
floating _1312 z
ReInitialize _ A$

Answer
  • Int - Invalid. Variable names in C are case-sensitive, and int is a reserved keyword in C for declaring integer variables. Using a variation such as Int is not allowed.
  • char - Invalid. char is a reserved keyword in C for character data type declaration. Using it as a variable name is not allowed.
  • 6_05 - Invalid. Variable names cannot start with a digit in C. Although underscores are allowed, a variable name cannot start with a digit.
  • floating - Invalid. floating is a reserved keyword in C. Therefore, using it as a variable name is not allowed.
  • A$ - Invalid. Variable names cannot contain special characters like $ in C.

💡Question 5

Which of the following are invalid constants? Why?

123.456             0x10.5       0X0G1
0001                  0xFFFF     123L
0Xab05              0L             -597.25
123.5e2    .         0001         +12
98.6F                 98.7U        17777s
0996                  -12E-12     07777
1234uL              1.2Fe-7     15,000
1.234L               197u         100U
0XABCDEFL    0xabcu     +123

Answer
  • 0x10.5 - Invalid. Floating-point constants in hexadecimal notation cannot have fractional parts.
  • 0X0G1 - Invalid. Hexadecimal constants cannot contain the letter 'G'.
  • 98.6F - Invalid. Suffix 'F' is not a valid suffix for a floating-point constant. It should be 'f' for float or 'L' for long double.
  • 98.7U - Invalid. Suffix 'U' is not a valid suffix for a floating-point constant.
  • 17777s - Invalid. Suffix 's' is not a valid suffix for an integer constant.
  • 0996 - Invalid. In C, octal constants cannot start with '0' followed by another digit other than '0'.
  • 1.2Fe-7 - Invalid. 'Fe' is not a valid part of a floating-point constant.
  • 15,000 - Invalid. Commas are not allowed in constants.

💡Question 6

What output would you expect from the following program?

#include <stdio.h>
int main (void)
{
    char c, d;
    c = 'd';
    d = c;
    printf ("d = %c\n", d);
    return 0;
}

Answer

This program assigns the character 'd' to the variable c, and then assigns the value of c to the variable d. Finally, it prints the value of d. Since the value of c is 'd', after assigning it to d, the value of d will also be 'd'.

So, the output of the program will be: d = d


Write C Programs for the requirements given below

💡Question 7

Convert given value in Meter to centimeter.

Answer

#include <stdio.h>

int main() {

    float meters, centimeters;

    printf("Enter the value in meters: ");

    scanf("%f", &meters);

    centimeters = meters * 100;

    printf("%.2f meters is equal to %.2f centimeters.\n", meters, centimeters);

    return 0;

}

💡Question 8

Calculate the volume of a cylinder. PI * r2 h

Answer

#include <stdio.h>

#define PI 3.14159

int main() {

    float radius, height, volume;

    printf("Enter the radius of the cylinder: ");

    scanf("%f", &radius);

    printf("Enter the height of the cylinder: ");

    scanf("%f", &height);

    volume = PI * radius * radius * height;

    printf("The volume of the cylinder is: %.2f cubic units.\n", volume);

    return 0;

}

💡Question 9

Calculate average marks of 4 subjects which, entered separately.

Answer

#include <stdio.h>

int main() {

    float marks[4];

    float sum = 0, average;

    int i;

    for (i = 0; i < 4; i++) {

        printf("Enter marks for subject %d: ", i + 1);

        scanf("%f", &marks[i]);

        sum += marks[i];

    }

    average = sum / 4;

    printf("Average marks: %.2f\n", average);

    return 0;

}

💡Question 10

Convert the given temperature in Celsius to Fahrenheit. T(°F) = T(°C) × 1.8 + 32

Answer

#include <stdio.h>

int main() {

    float celsius, fahrenheit;

    printf("Enter the temperature in Celsius: ");

    scanf("%f", &celsius);

    fahrenheit = celsius * 1.8 + 32;

    printf("%.2f Celsius is equal to %.2f Fahrenheit.\n", celsius, fahrenheit);

    return 0;

}

💡Question 11

Find the value of y using y = 3.5x+5 at x = 5.23.

Answer

#include <stdio.h>

int main() {

    float x = 5.23;

    float y;

    y = 3.5 * x + 5;

    printf("The value of y at x = %.2f is: %.2f\n", x, y);

    return 0;

}

💡Question 12

Find the cost of 5 items if the unit price is 10.50 Rupees.

Answer

#include <stdio.h>

int main() {

    float unit_price = 10.50;

    int quantity = 5;

    float total_cost;

    total_cost = unit_price * quantity;

    printf("The total cost of %d items at %.2f Rupees each is: %.2f Rupees\n", quantity, unit_price, total_cost);

    return 0;

}

💡Question 13

Enter the name, height, weight and gender of a person and calculate his/her BMI in Kg.
BMI = weight/ height2

Answer

#include <stdio.h>

int main() {
    char name[50];
    float height, weight;
    char gender;
    float bmi;

    printf("Enter name: ");
    scanf("%s", name);

    printf("Enter height (in meters): ");
    scanf("%f", &height);

    printf("Enter weight (in kilograms): ");
    scanf("%f", &weight);

    printf("Enter gender (M/F): ");
    scanf(" %c", &gender);

    bmi = weight / (height * height);

    printf("\nBMI Report for %s:\n", name);
    printf("Height: %.2f meters\n", height);
    printf("Weight: %.2f kilograms\n", weight);
    printf("Gender: %c\n", gender);
    printf("BMI: %.2f kg/m^2\n", bmi);

    return 0;
}

💡Question 14

Write a program that converts inches to centimeters.

Answer

#include <stdio.h>

int main() {
    float inches, centimeters;

    printf("Enter length in inches: ");
    scanf("%f", &inches);

    centimeters = inches * 2.54;

    printf("%.2f inches is equal to %.2f centimeters\n", inches, centimeters);

    return 0;
}

💡Question 15

The figure gives a rough sketch of a running track. It includes a rectangular shape and
two semi-circles. The length of the rectangular part is 67m and breadth is 21m.Calculate
the distance of the running track.

Answer

#include <stdio.h>
#define PI 3.14159

int main() {
    float length_rectangular = 67.0;
    float breadth_rectangular = 21.0;
    float distance_rectangular;
    float radius_semicircle = breadth_rectangular / 2.0;
    float distance_semicircle;
    float total_distance;

    distance_rectangular = 2 * (length_rectangular + breadth_rectangular);

    distance_semicircle = 0.5 * PI * radius_semicircle * 2;

    total_distance = distance_rectangular + 2 * distance_semicircle;

    printf("Distance of the running track: %.2f meters\n", total_distance);

    return 0;
}


Post a Comment

Previous Post Next Post