Variables and Data Types (Tutorial)

 Tutorial 07



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

#include <stdio.h>
int main ()
{
int sub;
sub = 87-15;   /* calculation */
printf (“The answer of subtracting 15 from 87 is %i\n” , sub);   
return 0;
}

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 -: 
#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;
}


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 -: 95

4. Which of the following are invalid variable names? Why?

Int  - Valid
char - invalid - Keyword
6_05 - invalid - started with a number
Calloc - valid
Xx - valid
alpha_beta_routine - valid
floating - valid
_1312 - valid 
z - valid
ReInitialize -valid 
_- valid 
A$ - invalid - can't use special characters


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 - Hexadecimal constant cannot have decimal points.
0X0G1 - Hexadecimal constant cannot have letter G.
0996 - Octal constant only can have numbers from 0 upto 7.
17777s - s is not a valid numeric suffix.
1.2Fe-7 - Fe is not a valid floating point suffix.

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 -: d

Write C Programs for the requirements given below

7. Convert given value in Meter to centimeter.

#include <stdio.h>
int main()
{
         float meter = 0.0f;
         float centimeter = 0.0f;
         
         printf("Enter a value in Meter : ");
         scanf("%f", &meter);
         
         centimeter = 100 * meter;
         printf("%.2f m is equal to %.2f cm\n",meter,centimeter);
         
         return 0;
}

8. Calculate the volume of a cylinder. PI * r2 h

#include <stdio.h>
#define PI 3.14
int main()

{
          float radius = 0.0f;
          float height = 0.0f;
          float volume = 0.0f;
         
          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\n", volume);
          return 0;
}

9. Calculate average marks of 4 subjects which, entered separately.

#include <stdio.h>
int main()
{
    float mark1 = 0.0f;
    float mark2 = 0.0f;
    float mark3 = 0.0f;
    float mark4 = 0.0f;
    float average = 0.0f;
 
    printf("Enter the marks for first subject : ");
    scanf("%f", &mark1);
    printf("Enter the marks for second subject : ");
    scanf("%f", &mark2);
    printf("Enter the marks for third subject : ");
    scanf("%f", &mark3);
    printf("Enter the marks for fourth subject : ");
    scanf("%f",&mark4);
 
    average = (mark1+mark2+mark3+mark4)/4;
 
    printf("Average of the 4 subjects = %.2f\n", average);
 
    return 0;
}

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

#include <stdio.h>
int main()
{
    float celcius = 0.0f; 
float fahrenheit = 0.0f;
    printf("Enter the temperature value in Celcius : ");
    scanf("%f", &celcius); 
    fahrenheit = (celcius * 1.8)+32;
printf("%.2f Celcius is equal to %.2f Fahrenheit\n", celcius, fahrenheit);
    return 0;
}

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


#include <stdio.h>
int main()
{
float y=0.0f;
float x=5.23f;
    y=3.5*x + 5;
    printf("The value of y is %.2f\n", y);
    return 0;
}

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

#include <stdio.h>
int main()
{
int items=0;
float unit_price=0.0f;
float cost=0.0f;
             
    printf("Enter the number of items : ");
    scanf("%i", &items);
    
printf("Enter the unit price : ");
    scanf("%f", &unit_price);
             
    cost=items * unit_price;
             
    printf("cost = %.2f\n",cost);
    return 0;
}

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

BMI = weight/ height2

#include <stdio.h>
int main()
{
    char name[10]="";
    char gender[10]="";
    float height=0.0f; 
    float weight=0.0f;
    float bmi=0.0f;
    printf("Enter your name : ");
    scanf("%s", &name);

    printf("Enter your gender : ");
    scanf(" %s", &gender);
  
    printf("Enter your height : ");
    scanf("%f", &height);

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

    bmi=weight/(height*height);
  
    printf("Name   : %s\n", name);
    printf("Gender : %s\n",gender);
    printf("BMI    : %.4f\n", bmi);

    return 0;
}

14. Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a Length in inches, the output would be 42.926cm. (Hint: 1 inch = 2.54 centimeters.)

#include <stdio.h>
int main ()
{
    float cm = 0.0f;
    float inch = 0.0f;
           
    printf("Enter value in inches : ");
    scanf("%f", &inch);
           
    cm = inch*2.54;           
    printf("%.2f inch in centimeter is %.2fcm.\n", inch,cm);
    return 0;         
}

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.



#include <stdio.h>
int main ()
{
    float width = 21.0f;
    float length = 67.0f;
    float distance = 0.0f;
    float radius = 0.0f;
         
    radius = width/2.0;
    distance = 2*length+2*3.14*radius;
           
    printf("The distance of the track is %.2f m.\n",distance);
    return 0;         
}

Post a Comment

Previous Post Next Post