You are on page 1of 5

Q1.

Write a program in C++ to swap the value two variable using third
variables.
#include <iostream>
using namespace std;

int main()
{
int a , b , c;
cout<<"Enter 1st Number: ";
cin>>a;
cout<<"Enter 2nd Number: ";
cin>>b;

cout << "Before swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

c = a;
a = b;
b = c;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;

return 0;
}

O/P
Enter 1st Number: 54
Enter 2nd Number: 34
Before swapping.
a = 54, b = 34

After swapping.
a = 34, b = 54
Q2. Write a program in C++ to check the year is leap or not.
#include <iostream>
using namespace std;

int main() {

int year;
cout << "Enter a year: ";
cin >> year;

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
cout << year << " is a leap year.";
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
cout << year << " is not a leap year.";
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
cout << year << " is a leap year.";
}
// all other years are not leap years
else {
cout << year << " is not a leap year.";
}

return 0;
}

O/P
Enter a year: 1978
1978 is not a leap year.
Q3.Write programs in C++ to illustrate C++ operators.

#include <iostream>
using namespace std;

int main()
{
int a = 8, b = 3;

// Addition operator
cout << "a + b = " << (a + b) << endl;

// Subtraction operator
cout << "a - b = " << (a - b) << endl;

// Multiplication operator
cout << "a * b = " << (a * b) << endl;

// Division operator
cout << "a / b = " << (a / b) << endl;

// Modulo operator
cout << "a % b = " << (a % b) << endl;

return 0;
}

O/P
a + b = 11
a-b=5
a * b = 24
a/b=2
a%b=2
Q4. Write a program in C++ to Check whether any integer number is
palindrome number or not.

#include <iostream>
using namespace std;

int main()
{
int n, num, digit, rev = 0;

cout << "Enter a positive number: ";


cin >> num;

n = num;

do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);

cout << " The reverse of the number is: " << rev << endl;

if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";

return 0;
}

O/P
Enter a positive number: 121212
The reverse of the number is: 212121
The number is not a palindrome.
Q5.Write a program in C++ to print the sum of digits of any integer no.

#include<iostream>
using namespace std;
int main() {
int x, s = 0;
cout << "Enter the number : ";
cin >> x;
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
cout << "\nThe sum of the digits : "<< s;
}

O/P
Enter the number : 2027
The sum of the digits : 11

You might also like