⯈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
Algorithm to swap two numbers without using extra variable
In the previous section, we have seen how to swap two integer numbers using extra variable. In this section we will see how we swap two numbers without using extra variable.
Let the two numbers be A and B.
Let A = 10 and B =20.
We add the values of A and B. Since we cannot use an extra variable, the sum has to be stored either in A or B. Let us store the answer (A+B) in A.
A = A + B (read as, A is assigned with the value of A + B, compute A + B and store in A)
A = A + B, evaluate the RHS first,
the answer 10 + 20 = 30, is stored in A. The previous value of A (10) is overwritten.
Note: A variable can store only one value at a given point of time.
Now the question is, can we get back the value 10, which is lost. Definitely yes and you guessed it right.
We subtract B from A to get back 10. (A = 30 and B = 20, the recent values). The value 10 after swapping we want to store in B,
Therefore, we write.
B = A – B [read as compute A – B and store in B]
At the last we need to get back 20, which we want to store in A. And we get 20 as,
A = A – B [read as compute A – B and store in A]
At end, we see that the values of A and B are swapped.
Algorithm:
Name of Algorithm: Swap two numbers
Step 1: Start
Step 2: Read two numbers as A and B
Step 3: A = A + B
B = A – B
A = A – B
Step 4: Display A and B
Step 5: Stop
In the next section, we shall discuss the basics of C programming language.