Design and test a C program to swap two numbers using a third variable and without using a third variable.
With third variable (temp variable)
Output#include <stdio.h>int main(){int first, second, temp;printf("Enter first number: ");scanf("%d", &first);printf("Enter second number: ");scanf("%d", &second);// value of first is assigned to temptemp = first;// value of second is assigned to firstfirst = second;// value of temp (initial value of first) is assigned to secondsecond = temp;printf("\nAfter swapping, first number = %d\n", first);printf("After swapping, second number = %d\n", second);return 0;}
Without third variable
Output#include <stdio.h>int main(){int first, second;printf("Enter first number: ");scanf("%d", &first);printf("Enter second number: ");scanf("%d", &second);first = first + second;second = first - second;first = first - second;printf("\nAfter swapping, first number = %d\n", first);printf("After swapping, second number = %d\n", second);return 0;}
0 Comments
Post a Comment