C Programming
In this exercise section of the tutorial we will write some C program.
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
Formula to find the area of a rectangle is length * width.
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; }
Area: 700.665222
Formula to find speed is speed = distance/time.
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; }
Speed: 150.000000
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; }
100.000000 deg C = 212.000000 deg F