Tutorial 2

1. Write a 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 a , b, c;

                      a = 87;

                      b = 15;

                      c = a - b;

                      printf("The difference between %d and %d is %d", a, b, c);

                      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 RESULTS */

            sum = 25 + 37 - 19;

            /*display results;*/

            printf("The result 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

The result is 95


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 char 6_05                                    - Invalid, Because space is not a valid character in variable name                                     and int, char are reserved words.

 Calloc Xx alpha_beta_routine       - Invalid, Because space is not a valid character in variable name.

 floating _1312 z                            - Invalid, Because space is not a valid character in variable name.

ReInitialize _ A$                            - Invalid, Because space is not a valid character in variable name                                               and $is not a valid character in variable name.    

 

 


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


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


Write C Programs for the requirements given below

7. Convert given value in Meter to centimeter.

ANSWER

            #include <stdio.h>

            int main()

            {

                 int a,b;

                 printf("\nEnter the meter value : ");

                 scanf("%i", &a);

                 b = a*100;

                 printf("The centimeter value is %icm", b);

                 return 0;

          }

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

ANSWER

            #include <stdio.h>

            #define PI 3.142

            int main()

            {

                  float r, h;

                  printf("\nEnter the radius : ");

                  scanf("%f", &r);

                  printf("\nEnter the height : ");

                  scanf("%f", &h);

                  printf("\nVolume is %f", PI*r*r*h);

                  return 0;

            }

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

ANSWER

    #include <stdio.h>

    int main ()

    {

         float  m1,m2,m3,m4,total,average;

          printf("Enter subject 1 mark :\n");    

          scanf("%f",&m1);

         printf("Enter subject 2 mark :\n");    

          scanf("%f",&m2);

          printf("Enter subject 3 mark :\n");    

         scanf("%f",&m3);

          printf("Enter subject 4 mark :\n");    

          scanf("%f",&m4);

          total  = m1 + m2 + m3 + m4;

          average = total/4;

          printf("Average is %.2f",average);

        return 0;

   }


          

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

ANSWER

           #include <stdio.h>

            int main()

            {

        float Tc, Tf;

        printf("\nEnter temparature in celsius : ");

        scanf("%f",&Tc);

        Tf = (Tc*1.8)+32;

        printf("\nTemperature in fahrenheit is %.2f", Tf);

        return 0;

            }

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

 ANSWER

             #include <stdio.h>

            int main()

           {

        float x, y;

        x = 5.23;

        y = 3.5*x +5;

        printf("\nValue of y is %.2f", y);

        return 0;

            }

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

ANSWER

#include <stdio.h>
int main()
{
               float up, cp;
               up = 10.50;
               cp = 5*up;
               printf("\nCostprice of 5 items is %.2f", cp);
               return 0;
}
 

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 n[10];

float h, w, bmi;

printf("\nEnter the name : ");

scanf("%s", n);

printf(" \nEnter the height : ");

scanf("%f", &h);

printf(" \nEnter the weight : ");

scanf("%f", &w);

bmi = w/(h*h);

printf("\n%s's BMI is %.2fKg", 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.)

ANSWER

#include <stdio.h>

int main()

{

               float i, c;

               printf("\nEnter the value in inches : ");

               scanf("%f", &i);

               c = i*2.54;

                printf("\nThe centimeter value is %.2fcm", c);

               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.






 ANSWER

#include <stdio.h>

int main()

{

               const float pi = 3.142;

               int l;

               float r, d;

               l = 67;

               r = 21/2;

               d = (2*l)+(2*pi*r);

               printf("\nThe distance if the running track is %.2fm", d);

              

              

               return 0;

}

 

0 comments:

Post a Comment