C Programming Tutorial Exercises

Exercise 1

Write a C program that reads a temperature in Fahrenheit and converts that temperature to the same temperature in Celsius. 


Solution 1

Let us use the formula: C/5=(F-32)/9.

#include <stdio.h>

int main()
{
   float F, C;

   printf("Enter a temperature in Fahrenheit : ");
   scanf("%f", &F);
   C = 5 * (F - 32) / 9;
   printf("%f F = %f C\n", F, C);
}


Exercise 2

Write a C program that reads an integer, computes the square root of the integer, and prints the square root in the following formats:
     a) an integer in decimal,
     b) an integer in hexadecimal,
     c) a floating point number,
     d) a floating point number in the scientific notation.


Solution 2

#include <stdio.h>
#include <math.h>

int main ()
{
   int n;
   double x;

   printf("Enter a positive integer : ");
   scanf("%d", &n);
   x = sqrt(n);
   printf("sqrt(%d) = %d\n", n, (int)x);
   printf("sqrt(%d) = %x\n", n, (int)x);
   printf("sqrt(%d) = %lf\n", n, x);
   printf("sqrt(%d) = %le\n", n, x);
}

Note: If you make math library calls, compile your program with the -lm option.


Exercise 3

Write a C program that produces the following formatted output:

%  one                        %
%%  two three                %%
%%%  four five six          %%%
%%%%  seven eight nine ten %%%%


Solution 3

#include <stdio.h>

int main ()
{
   printf("%%  one                        %%\n");
   printf("%%%%  two three                %%%%\n");
   printf("%%%%%%  four five six          %%%%%%\n");
   printf("%%%%%%%%  seven eight nine ten %%%%%%%%\n");
}


Exercise 4

Write a C program that reads the x and y coordinates of two points in the plane and computes the distance between them.


Solution 4

#include <stdio.h>
#include <math.h>

int main ()
{
   double x1, y1, x2, y2, d;

   printf("Enter the coordinates of the first point: ");
   scanf("%lf%lf",&x1,&y1);
   printf("Enter the coordinates of the second point: ");
   scanf("%lf%lf",&x2,&y2);
   d = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   printf("Distance between (%lf,%lf) and (%lf,%lf) is %lf\n", x1,y1,x2,y2,d);
}


Exercise 5

Write a C program that reads the name of your birth place and prints the number of letters in it.


Solution 5

#include <stdio.h>
#include <string.h>

int main ()
{
   char name[100];

   printf("Enter your birth-place: ");
   scanf("%s", name);
   printf("Number of letters = %d\n", strlen(name));
}


Brain-teaser

I ran the following C program on my machine:

#include <stdio.h>

int main()
{
   float x = 9.81;

   if (x == 9.81)
      printf("Yep, x is equal to 9.81.\n");
   else
      printf("Wow, x is not equal to 9.81.\n");
}

I obtained the following output:

Wow, x is not equal to 9.81.

Try it on your machine. Compile your program by an ANSI-compliant compiler. You are expected to get the same result as I got.

How come? How can you rewrite the program so as to obtain the desired output?



No comments

DATA WAREHOUSING AND MINING (MCA2104)

  UNIT I Introduction to Data mining ,  types of Data ,  Data Quality ,   Data Processing ,   Measures of Similarity   and  Dissimilarity , ...