C: Math

Example: Simple Math in C


	#include 
	int main()
	{
		int a = 5;
		int b = 2;
		int total;
		
		printf("a = %d.\n", a);
		printf("b = %d.\n\n", b);
		
		total = a + b;
		printf("a + b = %d.\n", total);

		total = a - b;
		printf("a - b = %d.\n", total);
		
		total = a * b;
		printf("a * b = %d.\n", total);
		
		total = a / b;
		printf("a / b = %d.\n", total);
		
		total = a % b;
		printf("Remainder when a divided by b = %d.\n", total);
		
		return 0;
	}
	

Output:

screenshot of output from math1.c program

Example: Incrementing, Decrementing, Assignment, and Logical Operators in C


	#include 
	int main()
	{
		int x = 5, y = 10;
		float a = 75.5, b = 50.5;

		printf("++x = %d.\n", ++x);

		printf("--y = %d.\n", --y);

		printf("++a = %f.\n", ++a);

		printf("--b = %f.\n", --b);

		printf("x+=5 = %d.\n", x+=5);

		printf("x%=y = %d.\n", x%y);
		
		// 1 = true; 0 = false
		printf("x > y is %d.\n", x > y);

		return 0;
	}
	

Output:

  ++x = 6.
  --y = 9.
  ++a = 76.500000.
  --b = 49.500000.
  x+=5 = 11.
  x=y = 2.
  x > y is 1.