You are on page 1of 7

1) Program to enter an integer and print if it is

prime or composite in c++.

#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(int n)
{
if (n == 1)
return false;

for (int i = 2; i <= sqrt(n); i++)


{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int n;
bool prime = false;
cout << " Enter a positive integer other than 1 : ";
cin >> n;
prime = isPrime(n);
if (prime)
{
cout << "\n\nThe entered number " << n << " is a Prime number.";
}
else
{
cout << "\n\nThe entered number " << n << " is Composite number.";
}
cout << "\n\n\n";
return 0;
}
OUTPUT :-

2) Write program to print swap variables without


using third variable in c++.
#include <iostream>
using namespace std;
int main()
{
int a=21, b=31;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
a=a*b;
b=a/b;
a=a/b;
cout<<"After swap a= "<<a<<" b= "<<b<<endl;
return 0;
}
OUTPUT :-

3) Program to check whether the given number is


palindrome or not in c++.

#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;
}

OUTPUT :-
4) Program to check whether the given number is
Armstrong or not in c++.

#include <iostream>
using namespace std;

int main() {
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;

while (originalNum != 0) {

remainder = originalNum % 10;

result += remainder * remainder * remainder;

originalNum /= 10;
}

if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";

return 0;
}
OUTPUT :-

5) Write a program to reverse element of array in


c++.

#include<iostream>
using namespace std;
int main()
{
int arr[5], i;
cout<<"Enter 5 Array Elements: ";
for(i=0; i<5; i++)
cin>>arr[i];
cout<<"\nThe Original Array is:\n";
for(i=0; i<5; i++)
cout<<arr[i]<<" ";
cout<<"\n\nThe Reverse of Given Array is:\n";
for(i=(5-1); i>=0; i--)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
OUTPUT :-

You might also like