Swap two numbers without Using third variable in C

Swap two numbers without Using third variable in C

Swap two numbers without Using third variable in C

In this program we will swap two numbers without using third variable. Lets understand common ways to swap two numbers without using third variable:
  1. By + and -
  2. By * and /

First we will learn swapping of two numbers

Program 1: Using Third Variable 

Let's write a program in C to swap two numbers using third variable.

  1. #include <stdio.h>
  2. int main()
  3. {
  4.       double firstNumber, secondNumber, temporaryVariable;

  5.       printf("Enter first number: ");
  6.       scanf("%lf", &firstNumber);

  7.       printf("Enter second number: ");
  8.       scanf("%lf",&secondNumber);

  9.       // Value of firstNumber is assigned to temporaryVariable
  10.       temporaryVariable = firstNumber;

  11.       // Value of secondNumber is assigned to firstNumber
  12.       firstNumber = secondNumber;

  13.       // Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
  14.       secondNumber = temporaryVariable;

  15.       printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
  16.       printf("After swapping, secondNumber = %.2lf", secondNumber);

  17.       return 0;
  18. }
Output:
Before swap a=10 b=20
After swap a=20 b=10

Program 1: Using + and -

Let's write a program in C to swap two numbers without using third variable.

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. int a=10, b=20;    
  6. clrscr();    
  7. printf("Before swap a=%d b=%d",a,b);    
  8.   
  9. a=a+b;//a=30 (10+20)  
  10. b=a-b;//b=10 (30-20)  
  11. a=a-b;//a=20 (30-10)  
  12.   
  13. printf("\nAfter swap a=%d b=%d",a,b);  
  14. getch();  
  15. }  
Output:
Before swap a=10 b=20
After swap a=20 b=10

Program 2: Using * and /

Let's see another example to swap two numbers using * and /.

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. int a=10, b=20;    
  6. clrscr();    
  7. printf("Before swap a=%d b=%d",a,b);    
  8.   
  9. a=a*b;//a=200 (10*20)  
  10. b=a/b;//b=10 (200/20)  
  11. a=a/b;//a=20 (200/10)  
  12.   
  13. printf("\nAfter swap a=%d b=%d",a,b);   
  14. getch();  
  15. }  
Output:
Before swap a=10 b=20
After swap a=20 b=10
reverse number Program in C

reverse number Program in C

C Program to reverse number

In this Program we will using loop and arithmetic operators and make a Program to reverse Integer Numbers. First, we will getting number as input from the user and reversing that number.

Lets Understand Program Steps

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. int n, reverse=0, rem;  
  6. clrscr();  
  7. printf("Enter a number: ");  
  8.   scanf("%d", &n);  
  9.   while(n!=0)  
  10.   {  
  11.      rem=n%10;  
  12.      reverse=reverse*10+rem;  
  13.      n/=10;  
  14.   }  
  15.   printf("Reversed Number: %d",reverse);  
  16. getch();  
  17. }  
Output:
Enter a number: 123
Reversed Number: 321
Sum of digits program in C

Sum of digits program in C

Sum of digits program in C With Example

C program to sum each digit: Let's Make a program in C language to the sum of digits program with the help of loop and mathematical operation.

Steps to design algorithm sum of Integers

To get sum of each digits by c program, use the following algorithm:
  • Step 1: Take number by user as Input
  • Step 2: Get the modulus/remainder of the number
  • Step 3: sum the remainder of the number
  • Step 4: Divide the number by 10
  • Step 5: Repeat the step 2 while number is greater than 0.
Let's see the sum of digits program in C.

  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main()  
  4. {  
  5. int n,sum=0,m;  
  6. clrscr();  
  7. printf("Enter a number:");  
  8. scanf("%d",&n);  
  9. while(n>0)  
  10. {  
  11. m=n%10;  
  12. sum=sum+m;  
  13. n=n/10;  
  14. }  
  15. printf("Sum is=%d",sum);  
  16. getch();  
  17. }     

Output:

Enter a number:654
Sum is=15

Enter a number:123
Sum is=6

Armstrong Number in C

Armstrong Number in C Language

Before writing a program in C language to check whether the number is Armstrong or not, First we will understand what is Armstrong number.

What is the Armstrong Number

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Note: there are six Armstrong numbers in the range of 0 and 999.

Armstrong number 1: 0
Armstrong number 2: 1
Armstrong number 3: 153
Armstrong number 4: 370
Armstrong number 5: 371
Armstrong number 6: 407


Let's try to understand why 153 is an Armstrong number.



Let's try to understand why 371 is an Armstrong number.

Let's see the c program to check any number provided by user is Armstrong Number or not

Output:
enter the number=153
armstrong number
enter the number=5
not armstrong number
Factorial Program in C

Factorial Program in C

Factorial Program in C

Factorial Program in C: Factorial of n is the result of all positive diving numbers. Factorial of n is indicated by n!. For Example:

  1. 5! = 5*4*3*2*1 = 120  
  2. 3! = 3*2*1 = 6  
Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek".

The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in c language. Let's see the 2 ways to write the factorial program.
  • Factorial Program using loop
  • Factorial Program using recursion

Factorial Program using loop

Let's see the factorial Program using loop.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4.   int i,fact=1,number;  
  5.   clrscr();  
  6.   printf("Enter a number: ");  
  7.   scanf("%d",&number);  
  8.   
  9.   for(i=1;i<=number;i++){  
  10.       fact=fact*i;  
  11.   }  
  12.   printf("Factorial of %d is: %d",number,fact);  
  13.   getch();  
  14. }  
Output:
Enter a number: 5
Factorial of 5 is: 120

Factorial Program using recursion in C

Let's see the factorial program in c using recursion.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3.   
  4. long factorial(int n)  
  5. {  
  6.   if (n == 0)  
  7.     return 1;  
  8.   else  
  9.     return(n * factorial(n-1));  
  10. }  
  11.    
  12. void main()  
  13. {  
  14.   int number;  
  15.   long fact;  
  16.   clrscr();  
  17.   printf("Enter a number: ");  
  18.   scanf("%d", &number);   
  19.    
  20.   fact = factorial(number);  
  21.   printf("Factorial of %d is %ld\n", number, fact);  
  22.   getch();  
  23. }  
Output:
Enter a number: 6
Factorial of 5 is: 720
Palindrome program in C

Palindrome program in C

What is Palindrome Number, 

Palindrome number in c language: A palindrome number is a number that is same after reverse. For example: 121, 131, 151, 34543, 343, 48984 are the palindrome numbers.

Palindrome number Program are asked interviewer for freshers and Experience holders.

How to Write a Palindrome number Program & its Execution Steps 

  • Take the number from user Input
  • Hold these number in temporary variable
  • Reverse Input number
  • Compare the temporary number with the reversed number
  • If both numbers are same, print palindrome number
  • Else print not palindrome number
Lets see how to write a Program of palindrome number using in C langusge. In this c program, we will get an input from the user and check whether number is palindrome or not.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. main()  
  4. {  
  5. int n,r,sum=0,temp;  
  6. clrscr();  
  7. printf("enter the number=");  
  8. scanf("%d",&n);  
  9. temp=n;  
  10. while(n>0)  
  11. {  
  12. r=n%10;  
  13. sum=(sum*10)+r;  
  14. n=n/10;  
  15. }  
  16. if(temp==sum)  
  17. printf("palindrome number ");  
  18. else  
  19. printf("not palindrome");  
  20. getch();  
  21. }  
Output:
enter the number=151
palindrome  number
enter the number=5621
not palindrome  number

Prime Number program in C language With Example

Prime Number program in C language

Prime numberPrime number is a whole number that is larger than one (1) and divided by 1 or itself.

 In other words, 

prime number has only two Factor that are 1 and itself. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.

Note: Zero (0) and 1 are not considered as prime numbers. Two (2) is the only one even prime number because all the Even numbers can be divided by 2.



Let's see how to write prime number program in C language and Execution of C Program. In this c program, we will get an input from the user and check This the number is prime or not.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main()  
  4. {  
  5. int n,i,m=0,flag=0;  
  6. clrscr();  
  7. printf("Enter the number to check prime:");  
  8. scanf("%d",&n);  
  9. m=n/2;  
  10. for(i=2;i<=m;i++)  
  11. {  
  12. if(n%i==0)  
  13. {  
  14. printf("Number is not prime");  
  15. flag=1;  
  16. break;  
  17. }  
  18. }  
  19. if(flag==0)  
  20. printf("Number is prime");  
  21. getch();  
  22. }   
Output:
Enter the number to check prime:56
Number is not prime
Enter the number to check prime:23
Number is prime

Fibonacci Series in C language with Example

Fibonacci Series in C

Fibonacci Series: Fibonacci sequence or Fibonacci Series is a series where the next number is the sum of pervious two numbers. The first two terms of the Fibonacci sequence is 0 followed by 1. 

For Example

The Fibonacci sequence or Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.

Lets understan using This Image:


There are two ways to write the fibonacci series program:
  • Fibonacci Series without recursion
  • Fibonacci Series using recursion

Fibonacci Series in C without recursion

Let's see the fibonacci series program in c without recursion.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main()  
  4. {  
  5.  int n1=0,n2=1,n3,i,number;  
  6.  clrscr();  
  7.  printf("Enter the number of elements:");  
  8.  scanf("%d",&number);  
  9.  printf("\n%d %d",n1,n2);//printing 0 and 1  
  10.   
  11.  for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed  
  12.  {  
  13.   n3=n1+n2;  
  14.   printf(" %d",n3);  
  15.   n1=n2;  
  16.   n2=n3;  
  17.  }  
  18. getch();  
  19. }  
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Fibonacci Series using recursion in C

Let's see the fibonacci series program in c using recursion.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void printFibonacci(int n){  
  4.     static int n1=0,n2=1,n3;  
  5.     if(n>0){  
  6.          n3 = n1 + n2;  
  7.          n1 = n2;  
  8.          n2 = n3;  
  9.          printf("%d ",n3);  
  10.          printFibonacci(n-1);  
  11.     }  
  12. }  
  13. void main(){  
  14.     int n;  
  15.     clrscr();  
  16.     printf("Enter the number of elements: ");  
  17.     scanf("%d",&n);  
  18.   
  19.     printf("Fibonacci Series: ");  
  20.     printf("%d %d ",0,1);  
  21.     printFibonacci(n-2);//n-2 because 2 numbers are already printed  
  22.   
  23.     getch();  
  24. }  
Output:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Popular Posts