1.positive or negative number program.
#include<iostream>
using namespace std;
string number(int n)
{
if(n>=0)
{
return "positive";
}
else
{
return "negative";
}
}
int main()
{
int num;
cin>>num;
cout<<number(num);
return 0;
}
2.even or odd number program.
#include<iostream>
using namespace std;
string evenodd(int n)
{
if(n%2==0)
{
return "even";
}
else
{
return "odd";
}
}
int main()
{
int num;
cin>>num;
cout<<evenodd(num);
return 0;
}
3.sum of first natural number.
#include<iostream>
using namespace std;
int SumOfNaturalNumber(int n)
{
int sum;
sum = n*(n + 1)/2;
return sum;
}
int main()
{
int number;
cin>>number;
cout<<SumOfNaturalNumber(number);
return 0;
}
4.sum of n natural no in given range.
#include<iostream>
using namespace std;
int SumOfNumberInRange(int n1,int n2)
{
int sum=0;
for(int i = n1 ; i <= n2 ; i++)
{
sum=sum+i;
}
return sum;
}
int main()
{
int number1,number2;
cin>>number1>>number2;
cout<<SumOfNumberInRange(number1,number2);
return 0;
}
5.greatest of 2 numbers.
#include<iostream>
using namespace std;
string GreatestNumber(int n1,int n2)
{
if(n1>n2)
{
return "n1 is greater";
}
else if(n1==n2)
{
return "n1 and n2 is equal";
}
else
{
return "n2 is greater";
}
}
int main()
{
int n1,n2;
cin>>n1>>n2;
cout<<GreatestNumber(n1,n2);
return 0;
}
6.greatest of 3 number.
#include<iostream>
using namespace std;
string GreatestOfThreeNumber(int n1,int n2,int n3)
{
if(n1>n2&&n1>n3)
{
return "n1 is greater";
}
else if(n2>n3)
{
return "n2 is greater";
}
else
{
return "n3 is greater";
}
}
int main()
{
int n1,n2,n3;
cin>>n1>>n2>>n3;
cout<<GreatestOfThreeNumber(n1,n2,n3);
return 0;
}
7.leap year or not.
#include<iostream>
using namespace std;
string leapyear(int n)
{
if((n%4==0)&&(n%100!=0)||(n%400==0))
{
return "leap year";
}
else
{
return "not leap year";
}
}
int main()
{
int year;
cin>>year;
cout<<leapyear(year);
return 0;
}
8.prime or not.
#include<iostream>
#include<cmath>
using namespace std;
string prime(int n)
{
for(int i = 2 ; i <=sqrt(n) ; i++)
{
if(n%i==0)
{
return "not prime";
}
}
return "prime";
}
int main()
{
int number;
cin>>number;
cout<<prime(number);
return 0;
}
9.prime number in given range.
#include<iostream>
#include<cmath>
using namespace std;
void prime(int n1,int n2)
{
for(int i = n1 ; i <= n2 ;i++)
{
int flag=0;
for(int j = 2 ; j <=sqrt(i) ; j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<i<<" ";
}
}
}
int main()
{
int n1,n2;
cin>>n1>>n2;
prime(n1,n2);
return 0;
}
10.sum of digits of a number.
#include<iostream>
using namespace std;
int SumOfDigit(int n)
{
int sum=0,rem; // for calculating no of digit formula is K = floor(log10(N) + 1)
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
return sum;
}
int main()
{
int num;
cin>>num;
cout<<SumOfDigit(num);
return 0;
}
11.reverse the number.
#include<iostream>
using namespace std;
int ReverseNo(int n)
{
int rev=0,rem;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
return rev;
}
int main()
{
int num;
cin>>num;
cout<<ReverseNo(num);
return 0;
}
12.Palindrome (if the reverse of that number is equal to the original number).
#include<iostream>
using namespace std;
string Palindrome(int n)
{
int temp=n;
int rev=0,rem;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
return "Palindrome";
else
return "Not Palindrome";
}
int main()
{
int num;
cin>>num;
cout<<Palindrome(num);
return 0;
}
13.Narcissistic number(sum of its own digits each raised to the power of the number of digits).
#include<iostream>
#include<cmath>
using namespace std;
string Armstrong(int n)
{
int pw = floor(log10(n) + 1);
int sum=0,rem;
int temp=n;
while(n>0)
{
rem=n%10;
sum=sum+pow(rem,pw);
n=n/10;
}
if(temp==sum)
return "Armstrong";
else
return "Not Armstrong";
}
int main()
{
int num;
cin>>num;
cout<<Armstrong(num);
return 0;
}
14.Armstrong number (the sum of its own digits,each raised to the power 3).
#include<iostream>
#include<cmath>
using namespace std;
void Armstrong(int n)
{
int sum=0,rem;
int temp=n;
while(n>0)
{
rem=n%10;
sum=sum+pow(rem,3);
n=n/10;
}
if(temp==sum)
cout<<temp<<endl;
}
int main()
{
int num1,num2;
cin>>num1>>num2;
for(int i=num1;i<=num2;i++)
Armstrong(i);
return 0;
}
15.fibonaci series of nth term.
#include<iostream>
using namespace std;
void fibonacci(int n)
{
int first=0, second=1, next;
for(int i = 0;i < n; i++)
{
if (i <= 1)
next = i;
else
{
next = first + second;
first = second;
second = next;
}
if(next>=n)
break;
else
cout<<next<<" ";
}
}
int main()
{
int num;
cin>>num;
fibonacci(num);
return 0;
}
16.factorial of number.
#include<iostream>
using namespace std;
int factorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
int no;
cin>>no;
cout<<factorial(no);
return 0;
}
17.power of number.
#include<iostream>
using namespace std;
int power(int no,int pow)
{
int ans=1;
while(pow>0)
{
ans=ans*no;
pow--;
}
return ans;
}
int main()
{
int no,pow;
cin>>no>>pow;
cout<<power(no,pow);
return 0;
}
18.factor of a number.
#include<iostream>
using namespace std;
void factor(int n)
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
cout<<i<<" ";
}
}
}
int main()
{
int no;
cin>>no;
factor(no);
return 0;
}
19.strong number(the sum of factorial of individual digits of the numbers is equal to the number itself.).
#include<iostream>
using namespace std;
int factorial(int n)
{
int fact=1;
for(int i=n;i>0;i--)
{
fact=fact*i;
}
return fact;
}
string strong(int n)
{
int ans=0,rem,temp;
temp=n;
while(n>0)
{
rem=n%10;
ans=ans+factorial(rem);
n=n/10;
}
if(ans==temp)
{
return "strong number";
}
else
{
return "not strong number";
}
}
int main()
{
int no;
cin>>no;
cout<<strong(no);
return 0;
}
20.Perfect number(sum of the proper positive divisors of the number is equal to the number itself.).
#include<iostream>
using namespace std;
string perfect(int n)
{
int sum=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
{
return "Perfect number";
}
else
{
return "Not Perfect number";
}
}
int main()
{
int no;
cin>>no;
cout<<perfect(no);
return 0;
}
21.Automorphic number(the last digits of the square of the number is equal to the number itself).
#include<iostream>
#include<cmath>
using namespace std;
string automorphic(int n)
{
int square=pow(n,2),flag=0;
while(square>0&&n>0)
{
if(square%10==n%10)
{
flag=1;
}
else
{
flag=0;
break;
}
square/=10;
n/=10;
}
if(flag==1)
{
return "Automorphic number";
}
else
{
return "Not Automorphic number";
}
}
int main()
{
int no;
cin>>no;
cout<<automorphic(no);
return 0;
}
22.Harshad number(positive integer which is divisible by the sum of the digits of the integer. It is also called the Niven number).
#include<iostream>
using namespace std;
string harshad(int n)
{
int rem,sum=0,temp=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
if(temp%sum==0)
{
return "Harshad number";
}
else
{
return "Not Harshad number";
}
}
int main()
{
int no;
cin>>no;
cout<<harshad(no);
return 0;
}
23.Abundant number(the sum of the proper divisors of the number is greater than the number itself).
#include<iostream>
using namespace std;
string abundunt(int n)
{
int sum=0;
for(int i=1;i<=n/2;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum>n)
{
return "Abundant number";
}
else
{
return "Not Abundant number";
}
}
int main()
{
int no;
cin>>no;
cout<<abundunt(no);
return 0;
}
24.Friendly pair(Amicable numbers are two different numbers related in a way such that the sum of the proper divisors of each is equal to
the other number).
#include<iostream>
using namespace std;
string FriendlyPair(int n1,int n2)
{
int n=n1>n2?n1:n2;
int sum1=0,sum2=0;
for(int i=1;i<=n/2;i++)
{
if(n1%i==0)
{
sum1=sum1+i;
}
if(n2%i==0)
{
sum2=sum2+i;
}
}
if(sum1==n2&&sum2==n1)
{
return "Friendly Pair";
}
else
{
return "Not Friendly Pair";
}
}
int main()
{
int no1,no2;
cin>>no1>>no2;
cout<<FriendlyPair(no1,no2);
return 0;
}
25.gcd or hcf of a two numeber.
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int main()
{
int no1,no2;
cin>>no1>>no2;
cout<<gcd(no1,no2);
return 0;
}
26.LCM of two number.
#include<iostream>
using namespace std;
int findLCM(int first, int second)
{
static int fact = first;
if(fact % first == 0 && fact % second == 0)
{
return fact;
}
else
{
fact=fact + first;
findLCM(first,second);
}
return fact;
}
int main()
{
int no1,no2;
cin>>no1>>no2;
cout<<findLCM(no1,no2);
return 0;
}
27.binary to decimal conversion.
#include<iostream>
#include<cmath>
using namespace std;
int binarytodecimal(int n)
{
int rem,ans=0,pw=0;
while(n>0)
{
rem=n%10;
ans=ans+rem*pow(2,pw);
pw++;
n=n/10;
}
return ans;
}
int main()
{
int no;
cin>>no;
cout<<binarytodecimal(no);
return 0;
}
28.binary to octal conversion.
#include<iostream>
#include<cmath>
using namespace std;
int binarytooctal(int n)
{
int rem,ans=0,pw=0,oct=0;
while(n>0)
{
rem=n%10;
ans=ans+rem*pow(2,pw);
pw++;
n=n/10;
}
int i=1;
while(ans>0)
{
oct=oct+(ans%8)*i;
ans=ans/8;
i=i*10;
}
return oct;
}
int main()
{
int no;
cin>>no;
cout<<binarytooctal(no);
return 0;
}
29.decimal to binary.
#include<iostream>
using namespace std;
int decimaltobinary(int n)
{
int rem,ans=0,i=1;
while(n>0)
{
rem=n%2;
ans=ans+rem*i;
i=i*10;
n=n/2;
}
return ans;
}
int main()
{
int no;
cin>>no;
cout<<decimaltobinary(no);
return 0;
}
30.decimal to octal.
#include<iostream>
using namespace std;
int decimaltooctal(int n)
{
int rem,ans=0,i=1;
while(n>0)
{
rem=n%8;
ans=ans+rem*i;
i=i*10;
n=n/8;
}
return ans;
}
int main()
{
int no;
cin>>no;
cout<<decimaltooctal(no);
return 0;
}
31.octal to decimal.
#include<iostream>
#include<cmath>
using namespace std;
int octaltodecimal(int n)
{
int rem,ans=0,i=0;
while(n>0)
{
rem=n%10;
ans=ans+rem*pow(8,i);
i++;
n=n/10;
}
return ans;
}
int main()
{
int no;
cin>>no;
cout<<octaltodecimal(no);
return 0;
}
32.octal to binary.
#include<iostream>
#include<cmath>
using namespace std;
int octaltobinary(int n)
{
int rem,ans=0,bin=0,i=0;
while(n>0)
{
rem=n%10;
ans=ans+rem*pow(8,i);
i++;
n=n/10;
}
i=1;
while(ans>0)
{
rem=ans%2;
bin=bin+rem*i;
i=i*10;
ans=ans/2;
}
return bin;
}
int main()
{
int no;
cin>>no;
cout<<octaltobinary(no);
return 0;
}
33.Program to find the Quadrant in which Coordinates lie.
#include<iostream>
using namespace std;
string position(int n1,int n2)
{
if(n1>0&&n2>0)
return "Coordinate lies in 1st Quadrant";
else if(n1<0&&n2>0)
return "Coordinate lies in 2nd Quadrant";
else if(n1<0&&n2<0)
return "Coordinate lies in 3rd Quadrant";
else if(n1>0&&n2<0)
return "Coordinate lies in 4th Quadrant";
else if(n1>0)
return "Coordinate lies on Positive x-axis";
else if(n1<0)
return "Coordinate lies on Negative x-axis";
else if(n2>0)
return "Coordinate lies on Positive y-axis";
else if(n2<0)
return "Coordinate lies on Negative y-axis";
else
return "it's an origin";
}
int main()
{
int no1,no2;
cin>>no1>>no2;
cout<<position(no1,no2);
return 0;
}
34.find the number of ways N people can occupy R seats.
#include<iostream>
using namespace std;
int factorial(int no)
{
int fact=1;
for(int i = 1; i <= no ; i++)
{
fact=fact*i;
}
return fact;
}
int permutation(int n,int r)
{
int ways;
ways=factorial(r)/factorial(r-n); //nPr = n! / (n-r)!
return ways;
}
int main()
{
int n,r;
cin>>n>>r;
if(r<n)
{
cout<<"Invalid input";
}
else
{
cout<<permutation(n,r);
}
return 0;
}
35.maximum number of handshakes which can happen between N number of people given the fact that any two people shake hands exactly once.
#include<iostream>
using namespace std;
int maxhandshake(int n)
{
int handshake;
for(int i=n-1;i>=1;i--)
{
handshake=handshake+i; // instead of for loop you can use formula handshake=n(n-1)/2
}
return handshake;
}
int main()
{
int no;
cin>>no;
cout<<maxhandshake(no);
return 0;
}
36.Addition two fraction.
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int main()
{
int num1,num2,den1,den2;
cin>>num1>>num2>>den1>>den2;
int num3 = (num1*den2)+(num2*den1);
int den3 = den1*den2;
cout<<num1<<"/"<<den1<<" + "<<num2<<"/"<<den2<<" = "<<num3/gcd(num3,den3)<<"/"<<den3/gcd(num3,den3);
return 0;
}
37.replacing all '0' with '1' in given number.
#include<iostream>
using namespace std;
int replace_zero_with_one(int n)
{
int new_no=0,rem;
if(n == 0)
new_no=1;
while(n>0)
{
rem=n%10;
if(rem==0)
{
rem=1;
}
n=n/10;
new_no=new_no*10+rem;
}
return new_no;
}
int main()
{
int no;
cin>>no;
cout<<replace_zero_with_one(no);
return 0;
}
38.Program to check whether a number be expressed as a sum of two prime numbers.
#include<iostream>
#include<cmath>
using namespace std;
int Prime(int n)
{
for(int i = 2 ; i <=sqrt(n) ; i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
int main()
{
int no,check=0;
cin>>no;
for(int i = 0 ; i < no ; i++)
{
if(Prime(i))
{
if(Prime(no-i))
{
cout<<no <<" = "<< i <<" + " << no-i<< endl;
check = 1;
}
}
}
if(check == 0)
{
cout<<no<<" cannot be expressed as the sum of two prime numbers.";
}
return 0;
}
39.Counting possible decoding of a given digit sequence Here we will discuss how to count all the possible decodings of a given digit sequence.
Before counting the number of decodings first let’s see how the numbers are assumed to be coded.
1 = A, 2 = B, 3 = C, . . . . , 26 = Z.
Now let’s take an example for reference,
Sequence = 131
Possible decoding (1, 3, 1,) = ACA
Possible decoding (13, 1) = MA
So, the total possible decodings of sequence 131 is 2.
#include<iostream>
#include<cstring>
using namespace std;
int countDecoding(char *digit,int n)
{
int decodings[n+1];
decodings[0]=1;
decodings[1]=1;
for(int i =1 ; i <= n ; i++)
{
int q = digit[i]-48;
int p = digit[i-1]-48;
if(q>0 && q<=26)
{
decodings[i+1] = decodings[i];
}
if((q+p*10) > 0 && (q+p*10 <= 26))
{
decodings[i+1] = decodings[i+1] + decodings[i-1];
}
}
return decodings[n];
}
int main()
{
char digit[20];
cin.getline(digit,20);
int n = strlen(digit);
cout<<"Number of decoding of the sequence "<<digit<<" are "<<countDecoding(digit,n);
return 0;
}
40.Check Character is a vowel or consonant.
#include<iostream>
using namespace std;
string vowel(char c)
{
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
{
return "it is a vowel";
}
else
{
return "it is consonant";
}
}
int main()
{
char c;
cin>>c;
cout<<vowel(c);
return 0;
}
41.Checking whether a Character is Alphabet or Not?
#include<iostream>
using namespace std;
string alphabet(char c)
{
if((c >= 65 && c <= 90) || (c >= 97 && c <= 122))
{
return "it is an alphabet";
}
else
{
return "it is not an alphabet";
}
}
int main()
{
char c;
cin>>c;
cout<<alphabet(c);
return 0;
}
42.C++ Program to calculate the area of a circle.
#include<iostream>
#define PI 3.14
using namespace std;
float AreaOfCircle(int r)
{
float area = PI*r*r;
return area;
}
int main()
{
float radius;
cin>>radius;
cout<<AreaOfCircle(radius);
return 0;
}
43.find ASCII value of a Character.
#include<iostream>
using namespace std;
int ascii(char c)
{
int ascii_value = (int)c;
return ascii_value;
}
int main()
{
char c;
cin>>c;
cout<<ascii(c);
return 0;
}
44.Number of times x digit occurs in given number.
#include<iostream>
#include<cstring>
using namespace std;
int Occurence(string no,char digit)
{
int counter = 0;
int i =0;
while(no[i]!='\0')
{
if(no[i] == digit)
{
counter++;
}
i++;
}
return counter;
}
int main()
{
string no;
getline(cin,no);
char digit;
cin>>digit;
cout<<Occurence(no,digit);
return 0;
}
45.Roots of a quadratic equation.
#include<iostream>
#include<cmath>
using namespace std;
void roots(int a,int b,int c)
{
float val = (b*b) -(4*a*c);
float root = sqrt(val);
if(val > 0)
{
cout<<(-b + root)/(2*a)<<endl;
cout<<(-b - root)/(2*a)<<endl;
}
else if( val == 0)
{
cout<< - b/(2*a)<<endl;
}
else
{
cout<< -b /(2*a)<<" +i "<<root<<endl;
cout<< -b /(2*a)<<" -i "<<root<<endl;
}
}
int main()
{
int a,b,c;
cin>>a>>b>>c;
roots(a,b,c);
return 0;
}
46.smallest number in array.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int mini = 0;
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
if(arr[i]<mini || i == 0)
{
mini = arr[i];
}
}
cout<<mini;
}
47.second smallest number in array.
#include<iostream>
using namespace std;
int sort_array(int *arr,int n)
{
int temp;
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr[1];
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
}
cout<<sort_array(arr,n);
}
48.largest element in the array.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int maxi = 0;
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
if(arr[i]>maxi || i == 0)
{
maxi = arr[i];
}
}
cout<<maxi;
}
49.smallest and largest number in array.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int maxi = 0;
int mini = 0;
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
if(arr[i] > maxi || i == 0)
{
maxi = arr[i];
}
if(arr[i] < mini || i == 0)
{
mini = arr[i];
}
}
cout<<maxi<<endl;
cout<<mini<<endl;
}
50.sum of element of the array.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
int sum = 0;
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
sum = sum + arr[i];
}
cout<<sum;
return 0;
}
51.Reverse Array.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
}
for(int i = 0 , j = n - 1; i < n/2 ; i++,j--)
{
arr[i] = arr[i] + arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
for(int i = 0 ; i < n ; i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
52.sort first half array in ascending order and second half array in descending order.
#include<iostream>
using namespace std;
void sort_array(int *arr,int n)
{
int temp;
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if((arr[i]>arr[j]) && (i < n/2))
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
else if((arr[i] < arr[j]) && (i > n/2))
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i =0 ;i < n; i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
}
sort_array(arr,n);
return 0;
}
53.sort an array in ascending and descending order.
#include<iostream>
using namespace std;
void sort_array(int *arr,int n)
{
int temp;
// ascending order
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i =0 ;i < n; i++)
{
cout<<arr[i]<<" ";
}
// descending order
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
cout<<"\n";
for(int i =0 ;i < n; i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr[i];
}
sort_array(arr,n);
return 0;
}
54.Program to count frequency of elements in an array.
#include<iostream>
using namespace std;
void frequency(int *arr,int n)
{
int counter;
for(int i = 0 ; i < n ; i=i+counter)
{
counter = 0;
for(int j = i; j < n ;j++)
{
if(arr[i]==arr[j])
{
counter++;
}
else
{
break;
}
}
cout<<arr[i]<<" occurs "<<counter<<" times"<<endl;
}
}
int main()
{
int n,temp;
cin>>n;
int arr[n];
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}
for(int i = 0 ; i < n ;i++)
{
for(int j = i+1; j < n ;j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
frequency(arr,n);
return 0;
}
55.the Longest Palindrome in an Array.
#include<iostream>
using namespace std;
int isPalindrome(int n)
{
int temp = n;
int rev = 0;
while(temp>0)
{
rev = rev*10+(temp%10);
temp = temp/10;
}
if(rev == n)
{
return 1;
}
return 0;
}
int longest_palindrome(int *arr,int n)
{
int temp;
for(int i = 0 ; i < n ;i++)
{
for(int j = i+1; j < n ;j++)
{
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i = n - 1; i >= 0; --i)
{
if(isPalindrome(arr[i]))
{
return arr[i];
}
}
return -1;
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}
int result = longest_palindrome(arr,n);
if(result == -1)
{
cout<<"There is no palindrome number in the array";
}
else
{
cout<<"longest palindrome number in the array "<<result;
}
return 0;
}
56.Print distinct element in an array.
#include<iostream>
using namespace std;
void Distinct(int *arr,int n)
{
int flag;
for(int i = 0 ; i < n ;i++)
{
flag = 0;
for(int j = 0; j < n ;j++)
{
if(arr[i] == arr[j] && i != j)
{
flag=1;
break;
}
}
if(flag == 0)
{
cout<<arr[i]<<" ";
}
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}
Distinct(arr,n);
return 0;
}
57.Repeating element of an array.
#include<iostream>
using namespace std;
void Repeat_element(int *arr,int n)
{
for(int i = 0 ; i < n ;i++)
{
for(int j = i + 1; j < n ;j++)
{
if(arr[i] == arr[j] && i != j)
{
cout<<arr[i]<<" ";
break;
}
}
}
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0 ; i < n ; i++)
{
cin>>arr[i];
}
Repeat_element(arr,n);
return 0;
}
58.remove duplicate elements in Array.
#include<iostream>
using namespace std;
void Remove_duplicate(int *arr,int Length )
{
int i, j;
int NewLength = 1;
for(i=1; i< Length; i++)
{
for(j=0; j< NewLength ; j++)
{
if(arr[i] == arr[j])
{
break;
}
}
if (j==NewLength )
{
arr[NewLength++] = arr[i];
}
}
for(int i = 0 ; i < NewLength ;i++)
{
cout<<arr[i]<<" ";
}
}
int main()
{
int Length;
cin>>Length;
int arr[Length];
for(int i = 0 ;i < Length ; i++)
{
cin>>arr[i];
}
Remove_duplicate(arr,Length);
return 0;
}
59.Minimum scalar product of two vector in an array.
(multiply the minimum values of array 1 to the maximum values of the array 2)
#include<iostream>
using namespace std;
int MinimumScalarProduct(int *arr1,int *arr2,int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr1[i]*arr2[i];
}
return sum;
}
void sort_array(int *arr1,int *arr2,int n)
{
int temp;
// ascending order
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr1[i] > arr1[j])
{
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
// descending order
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr2[i] < arr2[j])
{
temp = arr2[i];
arr2[i] = arr2[j];
arr2[j] = temp;
}
}
}
}
int main()
{
int n;
cin>>n;
int arr1[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr1[i];
}
int arr2[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr2[i];
}
sort_array(arr1,arr2,n);
cout<<MinimumScalarProduct(arr1,arr2,n);
return 0;
}
60.Maximum scalar product of two vector.
(multiply the minimum values of array 1 to the minimum values of the array 2)
#include<iostream>
using namespace std;
int MaximumScalarProduct(int *arr1,int *arr2,int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+arr1[i]*arr2[i];
}
return sum;
}
void sort_array(int *arr,int n)
{
int temp;
// ascending order
for(int i =0 ; i < n ; i++)
{
for(int j = i + 1 ; j < n ; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int n;
cin>>n;
int arr1[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr1[i];
}
int arr2[n];
for(int i = 0 ;i < n ; i++)
{
cin>>arr2[i];
}
sort_array(arr1,n);
sort_array(arr2,n);
cout<<MaximumScalarProduct(arr1,arr2,n);
return 0;
}
More comming soon!