- Prime Number or Not
- Palindrome Number or Not
- Fibonacci Series
- Armstrong Number or Not
- Anagram String
Check The Number is Prime or Not
You have to create a program that checks whether the number which is entered by the user is a prime number or a not prime number.
Example Test Case :
Input: 7
Output: Prime Number
Input: 20
Output: Not Prime Number
Program :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n=13, i, flag = 0;
cout<<”Enter a Number : “;
cin>>n;
for(i=2; i<=n/2; ++i) {
if(n%i==0) {
flag=1;
break;
}
}
if (flag==0)
cout<<”Prime Number";
else
cout<<n<<" is not a prime number";
return 0;
}
The Number is Palindrome or Not
In this program, you have to check whether the number is palindrome or not where the number is entered by the user.
Example Test Case :
Input: 12321
Output: Palindrome
Input: 987
Output: Not Palindrome
Program :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter a number : ";
cin >> n;
int temp=n, rev=0;
while(temp>0)
{
rev = rev*10 + temp%10;
temp = temp/10;
}
if(n == rev)
cout << "\n Palindrome number ";
else
cout << "\n Not a Palindrome number \n";
return 0;
}
Fibonacci Series
Fibonacci Series is famous series that starts with 0 and 1. It is simply created with the sum of the previous two elements. Let’s say it print to the number 5 so it starts with 0,1,1,2,3. Like that Fibonacci work. Here is the program to create the Fibonacci series.
Example Test Case :
Input: 7
Output: 0 1 1 2 3 5 8
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
Program :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n1=0, n2=1, n3, i, n;
cout<<"Enter a number : ";
cin>>number;
cout<<n1<<" "<<n2<<" "; // print 0 and 1
for( i = 2 ; i < n ; ++i )
{
n3 = n1 + n2;
cout << n3 << " " ;
n1 = n2 ;
n2 = n3 ;
}
return 0;
}
Check Whether The Number is Armstrong Number or Not
Armstrong Number is the number which sum of cube of digits itself. Let’s say the number 153 has the sum of a cube of digits itself 153.
153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
Here is the program that checks the Armstrong number or not.
Example Test Case :
Input: 153
Output: Yes
Input: 371
Output: Yes
Input: 120
Output: No
Program :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, num, remainder, n = 0, result = 0, power;
cout << "Enter an integer: ";
cin >> a;
num = a;
while (num != 0)
{
num /= 10;
++n;
}
num = a;
while (num != 0)
{
remainder = num % 10;
// pow() returns a double value
// round() returns the equivalent int
power = round(pow(remainder, n));
result += power;
num /= 10;
}
if (result == a)
cout << a << "Yes";
else
cout << a << "No";
return 0;
}
Check Two Strings are Anagram String or Not
Anagram String in which all the characters of one string as the same as in another string. Let’s say string one has “abcd” and another string has “bcda”.
Here is the program that checks the two string is an anagram or not.
Example Test Case :
Input: string1 = “LISTEN” , string2 = “SILENT”
Output: Yes
Input: string1 = “AMA” , string2 = “AAM”
Output: Yes
Input: string1 = “GOOD” , string2 = “GODS”
Output: No
Program :
#include <bits/stdc++.h>
using namespace std;
bool areAnagram(string str1, string str2)
{
int n1 = str1.length();
int n2 = str2.length();
if (n1 != n2)
return false;
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
for ( int i = 0; i < n1; i++ )
{
if ( str1[ i ] ! = str2 [ i ] )
return false;
}
return true;
}
int main()
{
string str1;
string str2;
cin>>str1;
cin>>str2;
if (areAnagram(str1, str2))
cout << "Yes";
else
cout << "No";
return 0;
}
You can also read about What is Kali Linux and How to install it in Windows?
0 Comments