0% found this document useful (0 votes)
9 views

C Programming

hi

Uploaded by

nilay thakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C Programming

hi

Uploaded by

nilay thakkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

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.

3. Using Bitwise Operators ( 5% known )


3. Check weather the number is prime or not ( in 2
ways )
1. Using Simple Trial Division ( 100% known )
2. Using square root ( 10% known )
4. Swap two numbers ( 3 ways )
1. Using Temporary Variable ( 100% known )
2. Without Using a Temporary Variable ( 90% known )
3. Using Bitwise XOR Operator ( 5% known )
Print “Hello World” – using printf
function
Print “Hello World” - Using puts()
Function
Print “Hello World” - Character-by-Character
Printing
Print “Hello World” - Using write()
System Call
Add 2 numbers - Using + operator
Add 2 numbers - Using Increment
Operator
Add 2 numbers - Using Bitwise
Operator
Input given is a = 5 and b = 3. Expected output is Sum = 8.

Step 1: Initialize variables a = 5 and b = 3.


We will use bitwise XOR (^) and AND (&) operations along with a left shift (<<) to calculate the sum of two
numbers without using the + operator.

Step 2: Perform the first iteration of the while loop.


Calculate the carry:
carry = a & b; → carry = 5 & 3; → carry = 1
(binary operation: 0101 & 0011 = 0001)
The carry represents overlapping bits that need to be added in the next step.

Update a with the partial sum (ignoring the carry):


a = a ^ b; → a = 5 ^ 3; → a = 6
(binary operation: 0101 ^ 0011 = 0110)

Shift the carry to the left:


b = carry << 1; → b = 1 << 1; → b = 2
(binary operation: 0001 << 1 = 0010)

Step 3: Perform the second iteration of the while loop.


Calculate the carry:
carry = a & b; → carry = 6 & 2; → carry = 2
(binary operation: 0110 & 0010 = 0010)

Update a with the partial sum (ignoring the carry):


a = a ^ b; → a = 6 ^ 2; → a = 4
(binary operation: 0110 ^ 0010 = 0100)
Shift the carry to the left:
b = carry << 1; → b = 2 << 1; → b = 4
(binary operation: 0010 << 1 = 0100)

Step 4: Perform the third iteration of the while loop.


Calculate the carry:
carry = a & b; → carry = 4 & 4; → carry = 4
(binary operation: 0100 & 0100 = 0100)

Update a with the partial sum (ignoring the carry):


a = a ^ b; → a = 4 ^ 4; → a = 0
(binary operation: 0100 ^ 0100 = 0000)

Shift the carry to the left:


b = carry << 1; → b = 4 << 1; → b = 8
(binary operation: 0100 << 1 = 1000)

Step 5: Perform the fourth iteration of the while loop.


Calculate the carry:
carry = a & b; → carry = 0 & 8; → carry = 0
(binary operation: 0000 & 1000 = 0000)

Update a with the partial sum (ignoring the carry):


a = a ^ b; → a = 0 ^ 8; → a = 8
(binary operation: 0000 ^ 1000 = 1000)

Shift the carry to the left:


b = carry << 1; → b = 0 << 1; → b = 0
The loop terminates since b = 0.
Step 6: Print the result.
The output message displayed is:
Sum = 8
Summary:
Input: a = 5, b = 3
Operation: Add the values of a and b using bitwise operations:

XOR (^) for sum without carry.


AND (&) for carry.
Left shift (<<) to propagate the carry.
Output: Sum = 8
This program successfully computes the sum of two integers without using the + operator, leveraging
binary arithmetic at the bitwise level.
Check Prime Number - Simple Trial
Division
Input given is n = 29.
Expected output is 29 is prime.

Step 1: Initialize the variables.


n = 29: This is the number being checked for primality.
cnt = 0: This variable counts the number of divisors of n.

Step 2: Handle the case where n <= 1.


If n <= 1, it is not prime. The program prints:
"n is NOT prime" and terminates.
In this case, n = 29, so the program skips this condition.

Step 3: Iterate to count the divisors of n.


The for loop runs from i = 1 to i = n (inclusive).

For each i, the program checks if n % i == 0.

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:

The loop checks divisibility for i = 1, 2, 3, ..., 29.


Divisors are found only for i = 1 and i = 29.
cnt becomes 2.
Step 4: Check the number of divisors.
After the loop, if cnt > 2:
The program prints: "n is NOT prime".
Otherwise, if cnt == 2, the program prints: "n is prime".

Step 5: Print the result.


In this case, for n = 29:
cnt = 2 because divisors are only 1 and 29.
The program outputs: "29 is prime".

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"

Comparison with Efficient Methods:


This method uses a simple brute force approach, which is less efficient than methods checking divisors only
up to the square root of n. However, it is straightforward and works well for small numbers.
Check Prime Number – Using Square
Root
Input given is n = 29.
Expected output is 29 is prime.

Step 1: Initialize n = 29 and cnt = 0.


n is the number to check for primality.
cnt will keep track of whether the number has any divisors other than 1 and itself.

Step 2: Check if n is less than or equal to 1.


A number less than or equal to 1 is not prime.
If n <= 1, the program directly prints:
"n is NOT prime" and terminates.
In this case, n = 29, so the program skips this check.

Step 3: Iterate through potential divisors to check if n is divisible.


A for loop runs from i = 2 to i * i <= n (i.e., up to the square root of n).
This optimization reduces the number of iterations, as no divisor larger than the square root of n is needed
for primality checks.
For each i, the program checks if n % i == 0.
If n is divisible by i, cnt is incremented.

Step 4: Check if any divisors were found (cnt > 0).


If cnt > 0, it means n has divisors other than 1 and itself, and hence is not prime. The program prints:
"n is NOT prime".
If no divisors were found (cnt == 0), the program prints:
"n is prime".
Step 5: Print the result.
In this case, for n = 29:
The loop runs for i = 2, 3, 4, 5 (as i * i <= 29).
No divisors are found (cnt == 0).
The program outputs: "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 2: Assign the value of a to temp.


temp = a; → temp = 5.
At this step, the value of a is stored in temp for safekeeping.

Step 3: Assign the value of b to a.


a = b; → a = 10.
Now, the value of b has been transferred to a.

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.

Step 5: Print the swapped values of a and b using printf.


The output message displayed is: "a = 10, b = 5".

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 1: Initialize variables a = 5 and b = 10.


We will use arithmetic operations (+ and -) to swap the values of a and b without using any
temporary variable.

Step 2: Add the values of a and b and assign the result to a.


a = a + b; → a = 5 + 10; → a = 15.
Now, a holds the sum of the original values of a and b.

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.

Step 5: Print the swapped values of a and b using printf.


The output message displayed is: "a = 10, b = 5".

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.

Step 1: Initialize variables a = 5 and b = 10.


We will use bitwise XOR (^) operations to swap the values of a and b without using any temporary
variable or arithmetic operations.

Step 2: Perform the first XOR operation to update a.


a = a ^ b; → a = 5 ^ 10; → a = 15 (binary operation: 0101 ^ 1010 = 1111).
Now, a holds the XOR of the original values of a and b.

Step 3: Perform the second XOR operation to update b.


b = a ^ b; → b = 15 ^ 10; → b = 5 (binary operation: 1111 ^ 1010 = 0101).
At this step, b is updated to hold the original value of a.

Step 4: Perform the third XOR operation to update a.


a = a ^ b; → a = 15 ^ 5; → a = 10 (binary operation: 1111 ^ 0101 = 1010).
At this step, a is updated to hold the original value of b.

Step 5: Print the swapped values of a and b using printf.


The output message displayed is: "a = 10, 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

You might also like