⯈Algorithm and its Characteristics
⯈Elementary Problems
- Addition of two numbers
- Calculate area and circumference of circle
- Calculate area of triangle
- Calculate simple interest
- Calculate slope and distance between two points
- Convert length in feet to inches
- Weighted score in exam
- Convert temperature in degree Celsius to Fahrenheit
- Swap two numbers
- Swap two numbers without using extra variable
- Overview of C Programming Language
- Getting started with C Programming Language
- Keywords
- Identifiers
- Constants
- Operators
- Expression Evaluation
- Mathematical expression to C equivalent expressions
- Datatypes
- Variables
- Integer representation in C
- Character representation in C
- Type conversion in C
- sizeof operator
- Comments
- Mathematical Functions
- input output statements
- width specifiers in C
- structure of a C program
- header files
- Compilation process of a C program
- Types of initializations.
⯈Basic C Programs
- C program to add two numbers
- C program to find area and circumference of circle
- C program to swap two numbers
- C program to swap two numbers without using extra variable
- C program to swap two numbers using bitwise XOR
- C program to convert temperature in Celsius to Fahrenheit
- C program to calculate gross salary of an employee
- C program to count number of digits in a positive integer
- C program to count number of digits in binary representation
- C program to count number of digits in base ‘K’
- C program to convert kilometer to meter, feet, inches and centimeters
- C program to find first and last digit of a number
- C program to find minimum number of currency denominations
- C program to convert cartesian coordinates to polar coordinates
- C program to find distance between two places on earth in nautical miles
- C program to find slope and distance between two points
- C program to add 1 to the number using ‘+’ operator
- C program to find maximum of two numbers using ternary operator
- C program to find maximum of three numbers using ternary operator
- C program to find kth bit of a number
- C program to find last four bits of a byte
- C Program to reset right most set bit of a number
Multiplicative operators in C
There are three multiplicative operators.
We shall discuss these one by one.
You may also refer to the video posted at the end of this page.
Multiplication (*)
This operator multiplies two numbers.
Ex:
2*3 = 6,
2*3.1 = 6.2
Division (/)
This operator divides the two numbers. There are some rules for division.
- integer / integer = integer
- integer / real_number = real_number
- real_number / integer = real_number
- real_number / real_number = real_number
It means if either of numerator or denominator is real_number the final answer is real_number.
Let us understand this with some examples:
2/3 = 0.67 (mathematically), but we see that 2 is an integer (no decimal point) and 3 is an integer. And we know that integer/integer = integer. Therefore,
2/3 = 0 (truncate all the digits after decimal point)
2.0/3 = 0.67 (mathematically), but we see that 2.0 is a real number (decimal point) and 3 is an integer. And we know that real_number/integer = real_number. Therefore,
2.0/3 = 0.67 (since 0.67 is a real number)
2/3.0 = 0.67 (mathematically), but we see that 2 is an integer (no decimal point) and 3.0 is a real number. And we know that integer/real_number = real_number. Therefore,
2/3.0 = 0.67 (since 0.67 is a real number)
2.0/3.0 = 0.67 (mathematically), but we see that 2.0 is a real number (decimal point) and 3.0 is a real number. And we know that real_number/real_number = real_number. Therefore,
2.0/3.0 = 0.67 (since 0.67 is a real number)
Similarly,
- 4.0/2 = 2.0
- 8/3 = 2
- -3/2 = -1
Modulo operator (%)
This operator gives the remainder when ‘a’ is divided by ‘b’ and is represented as a%b.
This operator can only be applied on integers. To get the remainder on real numbers there is a separate mathematical function called “fmod”.
There are some rules with respect to modulo operator,
- Always take the sign of numerator (‘a’)
- a%b = a, if |a|<|b|
Let us understand these by some examples.
r = 8 % 3
r = 2
We get remainder as 2, when 8 is divided by 3.
r = 4 % 4
r = 0
We get remainder as 0, when 4 is divided by 4.
r = 3 % 4
r = 3. (a%b = a, if |a|<|b|)
r = -7 % 4
r = -3. (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)
r = 7 % -4
r = 3. (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)
r = -7 % -4
r = -3. (we divide 7 by 4 and get the remainder as 3, and always take the sign of first number)
r = -4 % -7
r = -4. (Take sign of 1st number and a%b = a, if |a|<|b|)
The following expressions are invalid.
1) 2.3 % 2 (Error: since modulo can only be applied on integers)
2) 4 % 2.0 (Error: since modulo can only be applied on integers)