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

Popular Posts