Write a C++ program to swap the values of two numbers using if else statement
Write a C++ program to swap the values of two number if values of both variables are not the same using if-else statement
e.g
before swapping a=5; b=6
after swapping a=6, b=5
Write a C++ program to swap the values of two numbers using if else statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// C++ program to swap two numbers using 3rd variable #include <bits/stdc++.h> using namespace std; int main() { int a = 2, b = 3; cout << "Before swapping a = " << a << " , b = " << b << endl; // temporary variable int temp; // appying swapping algorithm temp = a; a = b; b = temp; cout << "After swapping a = " << a << " , b = " << b << endl; return 0; } |
Flowchart of the program to swap the values of two number without third variable using if-else statement
C++ Source code of swapping the values of two variables by the using the if-else statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<iostream> using namespace std; int main () { int a,b; cout<<"enter two numbers"<<endl; cin>>a>>b; if(a!=b) { a=a+b; b=a-b; a=a-b; cout<< "After swapping the values,"<<"value of a is:"<<a<<"and value of b is:"<<b; } else cout<<"two number are equal"; } |
Output
enter two numbers
3
4
After swapping the values, Value of a is: 4 and Value of b is:3
SFT (Shamil’s Flow Table )
Are you interested to Read about SFT(Shamil’s Flow Table)?
C++ Exercise | If else Statement
- calculate the bill
- character is small, capital or a special character
- a number is even or odd
- 0 is a positive or negative number
- a positive and negative number
- Enter Range of numbers and replaced them
- a greater number among three numbers
- Armstrong Number
- ASCII codeÂ
- Find the Maximum value program in C++ (C Plus Plus).
- maximum number
- Maximum Number between two numbersÂ
- Student Grade
- the number is divisible by 11 or 5 or not
- TriangleÂ
- a triangle is an equilateral, isosceles or scalene
- Leap yearÂ
- character is an alphabet or not
- Grade Percentage
- character is an alphabet, digit, or special character
- character is an uppercase or lowercase.
- Weekdays
- a prime or composite number
- hours and minutes as AM or PM
- swap the values of two numbers
- update even to odd
- Profit Loss
- centimeter into meter and kilometerÂ
- TriangleÂ
- Salary
- Even odd with goto statement.
- area of the circle