C Programming 3
C Programming 3
Reverse an Integer
C program to reverse a number using for loop
#include <stdio.h>
int main()
{
int n, reverse_Number = 0,
int rem,Original_number=0;
printf("Enter a number to get reverse number ");
scanf("%d", &n);
Original_number=n;
for(;n != 0;)
{
rem = n%10;
reverse_Number = reverse_Number*10 + rem;
n /= 10;
}
printf("Reversed Number of %d is = %d",Original_number,reverse_Number);
getch();
}
Input: num
(1) Initialize rev_num = 0
(2) Loop while num > 0
(a) Multiply rev_num by 10 and add remainder
of num
divide by 10 to rev_num
rev_num = rev_num*10 + num%10;
(b) Divide num by 10
(3) Return rev_num
C Program to Reverse a Number
• Example:
num = 4562
rev_num = 0
• rev_num = rev_num *10 + num%10 = 2
num = num/10 = 456
• rev_num = rev_num *10 + num%10 = 20 + 6 = 26
num = num/10 = 45
• rev_num = rev_num *10 + num%10 = 260 + 5 = 265
num = num/10 = 4
• rev_num = rev_num *10 + num%10 = 265 + 4 = 2654
num = num/10 = 0
C Program to Reverse a Number (while loop)
• Reverse an Integer #include <stdio.h>
• This program takes an integer input from the int main() {
int n, rev = 0, remainder;
user. Then the while loop is used until n !=
printf("Enter an integer: ");
0 is false (0). scanf("%d", &n);
• In each iteration of the loop, the remainder while (n != 0) {
when n is divided by 10 is calculated and the remainder = n % 10;
rev = rev * 10 + remainder;
value of n is reduced by 10 times. n /= 10;
• Inside the loop, the reversed number is }
printf("Reversed number = %d", rev);
computed using:
return 0;
• rev = rev*10 + remainder; }
Output
Enter an integer: 2345
Reversed number = 5432
Program code for Factorial of a Number in C
#include<stdio.h> • Working:
#include<conio.h> • First the computer reads the number to find the factorial of
void main() the number from the user.
{ • Then using while loop the value of ‘i’ is multiplied with the value of
int n,i=1,f=1; ‘f’.
clrscr(); • The loop continues till the value of ‘i’ is less than or equal to ‘n’.
printf("\n Enter The Number:"); • Finally the factorial value of the given number is printed.
scanf("%d",&n); • Step by Step working of the above Program Code:
• Let us assume that the number entered by the user is 6.
//LOOP TO CALCULATE FACTORIAL OF A NUMBER • It assigns the value of n=6 , i=1 , f=1
while(i<=n) • Then the loop continues till the condition of the while loop is true.
{ • 2.1. i<=n (1<=6) , while loop condition is true.
f=f*i; • f=f*i (f=1*1) So f=1
• i++ (i=i+1) So i=2
i++; • 2.2. i<=n (2<=6) , while loop condition is true.
} • f=f*i (f=1*2) So f=2
printf("\n The Factorial of %d is %d",n,f); • i++ (i=i+1) So i=3
• 2.3. i<=n (3<=6) , while loop condition is true.
getch(); • f=f*i (f=2*3) So f=6
} • i++ (i=i+1) So i=4
• 2.4. i<=n (4<=6) , while loop condition is true.
• f=f*i (f=6*4) So f=24
• i++ (i=i+1) So i=5
• 2.5. i<=n (5<=6) , while loop condition is true.
• f=f*i (f=24*5) So f=120
• i++ (i=i+1) So i=6
• 2.6. i<=n (6<=6) , while loop condition is true.
• f=f*i (f=120*6) So f=720
• i++ (i=i+1) So i=7
• 2.7. i<=n (7<=6) , while loop condition is false.
• It comes out of the while loop.
• Finally it prints as given below
• The Factorial of 6 is 720
• Thus the program execution is completed.
C program to check Least Significant Bit (LSB) of a
number is set or not
Logic to check Least Significant Bit (LSB) of a number
• To check the status of Least Significant Bit (LSB) of any
/** * C program to check Least Significant Bit
number we will use bitwise AND & operator. (LSB) of a number using bitwise operator
• Even for checking the current status of any bit we use bitwise */ #include <stdio.h>
AND operator. int main()
• We know that bitwise AND operator evaluates each bit of the {
result to 1 if the corresponding bits of both operands are 1. int num;
• Now to check the LSB of any number we are going to perform // Input a number from user
bitwise AND operation with the number and 1. printf("Enter any number: ");
• The bitwise AND operation number & 1 will evaluate to 1 if and scanf("%d", &num);
only if the LSB of number is 1 otherwise will evaluate to 0. //If (num & 1) evaluates to 1
if(num & 1)
printf("LSB of %d is set (1).", num);
else
printf("LSB of %d is unset (0).", num);
return 0;}
C program to swap two numbers using bitwise operator
• Logic to swap two numbers using bitwise • Program to swap two numbers
operator #include <stdio.h>
• We can perform swapping of two numbers using int main()
bitwise XOR ^operator. { int num1, num2;
• Bitwise XOR operator evaluates each bit of the result printf("Enter any two numbers: ");
to 1 if each corresponding bits of the two operands scanf("%d%d",&num1,&num2);
differ else evaluates to 0. printf("Original value of num1 = %d\n", num1);
• Suppose two integer values a and b printf("Original value of num2 = %d\n", num2);
Let's suppose x = a ^ b num1 ^= num2;
num2 ^= num1;
Then again x ^ b will evaluate to a and x ^ a will num1 ^= num2;
evaluate to b. printf("Num1 after swapping = %d\n", num1);
printf("Num2 after swapping = %d\n", num2);
return 0;}
C program to check even or odd using bitwise operator
• Logic to check even or odd numbers using bitwise • Program to check even or odd using bitwise operator
#include <stdio.h>
• In the above image we can see that if the number is even int main()
then its LSB is 0 otherwise LSB is 1. {
• To check the status of any bit we can use bitwise AND int num;
operator. If num & 1 evaluates to 1 then the number is // Input a number from user
odd otherwise even. printf("Enter any number: ");
scanf("%d", &num); (num & 1) ?
printf("%d is odd.", num) : printf("%d is even.", num);
return 0;
}