Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts
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

Popular Posts