Write a C program to swapping of two numbers with and without third variable.

Source Code:

without third variable.

#include <stdio.h> int main() { int num1,num2; printf("Enter num1 \n"); scanf("%d",&num1); printf("Enter num2 \n"); scanf("%d",&num2); printf("Number before swapping:\n"); printf("num1:%d\n",num1); printf("num2:%d\n",num2); num1=num1+num2; num2=num1-num2; num1=num1-num2; printf("Number after swapping:\n"); printf("num1:%d\n",num1); printf("num2:%d\n",num2); return 0; }

Output:

Enter num1 67 Enter num2 45 Number before swapping: num1:67 num2:45 Number after swapping: num1:45 num2:67

With third variable:

#include <stdio.h> int main() { int num1,num2,num3; printf("Enter num1 \n"); scanf("%d",&num1); printf("Enter num2 \n"); scanf("%d",&num2); printf("Number before swapping:\n"); printf("num1:%d\n",num1); printf("num2:%d\n",num2); num3=num1; num1=num2; num2=num3; printf("Number after swapping:\n"); printf("num1:%d\n",num1); printf("num2:%d\n",num2); return 0; }

Output:

Enter num1 90 Enter num2 99 Number before swapping: num1:90 num2:99 Number after swapping: num1:99 num2:90

Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.