C Programming
C Programming
SIMPLE PROGRAMS,
EXECUTED IN UNKNOWN ,
UNEXPLORED /
UNDISCOVERED,
YET THE MOST EFFICIENT
APPROACH.
1. Print Hello World ( in 4 ways )
1. Using printf function ( 100% known )
2. Using puts function ( 90% known )
3. Using putchar function ( 60% known )
4. Using write function ( 5% known )
2. Add 2 numbers ( in 3 ways )
Content 1. Using + Operator ( 100% known )
Using the increment operator ( 70% known )
Summary 2.
If the condition is true, it means i is a divisor of n, and the program increments cnt by 1.
Logic for divisors:
A prime number has exactly two divisors: 1 and the number itself.
If cnt > 2 after the loop, it means the number is not prime because it has more than two divisors.
For n = 29:
Summary:
Input: n = 29
Logic:
The program iterates from 1 to n to count all divisors of n.
If the number of divisors (cnt) is exactly 2, the number is prime. Otherwise, it is not.
Output: "29 is prime"
Summary:
Input: n = 29
Logic:
The program uses a loop to check for divisors up to the square root of n.
If divisors are found, n is not prime. Otherwise, it is prime.
Output: "29 is prime"
This program is efficient for checking primality due to the optimized loop running only up to the square root
of the number.
Swap Two Numbers - Using Temporary
Variable
Input given is a = 5 and b = 10.
Expected output is a = 10 and b = 5.
Step 1: Initialize variables a = 5 and b = 10, and declare a temporary variable temp.
temp is used as a placeholder to temporarily store the value of one variable during the swapping
process.
Step 4: Assign the value of temp (which contains the original value of a) to b.
b = temp; → b = 5.
The value of temp is now transferred to b, completing the swap.
Summary:
Input: a = 5, b = 10
Operation: Swap values of a and b using a temporary variable temp.
Output: a = 10, b = 5
This program successfully swaps the values of two variables by using a temporary variable.
Swap Two Numbers - No Temporary
Variable
Input given is a = 5 and b = 10.
Expected output is a = 10 and b = 5.
Step 3: Subtract the new value of b (10) from a (15) and assign the result to b.
b = a - b; → b = 15 - 10; → b = 5.
At this step, b is updated to hold the original value of a.
Step 4: Subtract the new value of b (5) from a (15) and assign the result to a.
a = a - b; → a = 15 - 5; → a = 10.
At this step, a is updated to hold the original value of b.
Summary:
Input: a = 5, b = 10
Operation: Swap values of a and b using arithmetic operations without using a temporary variable.
Output: a = 10, b = 5
This program successfully swaps the values of two variables using only addition and subtraction
operations, without the need for any extra space.
Swap Two Numbers - Using Bitwise XOR
Operator
Input given is a = 5 and b = 10.
Expected output is a = 10 and b = 5.
Summary:
Input: a = 5, b = 10
Operation: Swap values of a and b using bitwise XOR operations without using any temporary
variable or arithmetic operations.
Output: a = 10, b = 5
This program successfully swaps the values of two variables using the XOR bitwise operator. It
leverages the property of XOR where applying it twice with the same number restores the original
Subscribe and
Press the bell Icon