C - Exercise 2

C Programming

Share

In this exercise section of the tutorial we will write some C program.

Write a program in C to multiple two integer values and print the result

To multiple we use the * multiplication operator.

/**
 * file: multiply.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find the product of two numbers
 */

#include <stdio.h>
int main(void)
{
  int a = 10, b = 20;
  int product = a * b;
  printf("Product of axb = %d\n", product);
  return 0;
}

Output

Product of axb = 200

Write a program in C to find the area of a rectangle

Formula to find the area of a rectangle is length * width.

For this example we will use the floating point data type to store the value of the sides and the answer.

To print floating point value we pass "%f" in the printf( ) function.

/**
 * file: multiply.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find the area of rectangle
 */

#include <stdio.h>
int main(void)
{
  float length = 12.34, width = 56.78;
  float area = length * width;
  printf("Area: %f\n", area);
  return 0;
}

Output

Area: 700.665222

Write a program in C to find the speed of a train if it covers 300 km in 2 hours

Formula to find speed is speed = distance/time.

To divide numbers we use the / division operator.

Click here to learn about Speed Distance Time.

/**
 * file: speed.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find speed
 */

#include <stdio.h>
int main(void)
{
  float
    distance = 300,
    time = 2;
  float speed = distance / time;
  printf("Speed: %f\n", speed);
  return 0;
}

Output

Speed: 150.000000

Write a program in C to convert 100 degree Celsius into Fahrenheit

Formula to convert temperature from Celsius to Fahrenheit is given below.

F = C * 1.8 + 32

Where F is temperature in degree Fahrenheit and C is temperature in degree Celsius.

/**
 * file: temp.c
 * author: yusuf shakeel
 * date: 2010-11-26
 * description: program to find temp
 */

#include <stdio.h>
int main(void)
{
  float temp_c = 100;
  float temp_f = temp_c * 1.8 + 32;
  printf("%f deg C = %f deg F\n", temp_c, temp_f);
  return 0;
}

Output

100.000000 deg C = 212.000000 deg F
Share

Recently Updated