Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Prime Number From 2 to N Check if N is Prim or Not

#include <iostream> #include <iostream>


#include <math.h> using namespace std;

using namespace std; int main(){


int n, i;
int main(){ bool isPrime = true;
int x;
cin>>x; cin >> n;
for (int i=2; i<x; i++) { for(i = 2; i <= n / 2; ++i){
bool prime=true; if(n % i == 0){
for (int j=2; j*j<=i; j++) isPrime = false;
{ break;
if (i % j == 0) }
{ }
prime=false;
break; if (isPrime){
} cout << "PRIME";
} }else{
if(prime){ cout << "NOT PRIME”;
cout << i << " "; }
} return 0;
} }
return 0;
}
Factorial Anagrams

#include <iostream> #include <iostream>

using namespace std; using namespace std;

int main(){ int main(){


int x; int t;
int count=1; cin>>t;
cin>>x; while(t--){
for(int i = 1; i <=x; ++i){ string x,y;
count *= i; cin>>x>>y;
} int c=0;
cout<<count<<endl;
} for(int i=0;i<x.length();i++){
for(int j=0;j<y.length();j++){
if(x[i]==y[j]){
x[i]='-';
y[j]='-';
break;
}
}
Toggle String }

#include <iostream> for(int i=0;i<x.length();i++){


#include <string> if(x[i]!='-'){
#include <locale> c++;
}
using namespace std; }

int main(){ for(int i=0;i<y.length();i++){


locale loc; if(y[i]!='-'){
string x; c++;
char c; }
cin>>x; }
for(int i=0;i<x.length();i++){ cout<<c<<endl;
c=x[i]; }
if(islower(c,loc)){ return 0;
c=toupper(c,loc); }
cout<<c;
}else if(isupper(c,loc)){
c=tolower(c,loc);
cout<<c;
}
}
cout<<endl;
}
Cipher Right Triangle

#include <iostream> #include <iostream>


#include <fstream>
#include <cctype> using namespace std;
#include <locale>
int main(){
using namespace std; int t;
cin>>t;
int main(){ while(t--){
string x; int x;
locale loc; cin>>x;
getline(cin,x); if(x==1){
int add; cout<<'*'<<endl;
cin>>add; }else if(x>1){
for(int i=1,k=0;i<=x;++i,k=0){
string a for(int sp=1; sp<=x-i; sp++){
="abcdefghijklmnopqrstuvwxyz"; cout <<" ";
string num="0123456789"; }

for(int i=0;i<x.length();i++){ while(k != 2*i-1){


char c=x[i]; cout << "*";
for(int j=0;j<a.length();j++){ k++;
if(tolower(c,loc)==a[j]){ }
if(isupper(x[i],loc)){ cout << endl;
x[i]=toupper(a[(j+add)%26],loc); }
}else{ }
x[i]=a[(j+add)%26]; }
} return 0;
} }
}
} Input Output
2 *
for(int i=0;i<x.length();i++){ 1 *
char c=x[i]; 3 ***
for(int j=0;j<num.length();j++){ *****
if(c==num[j]){
x[i]=num[(j+add)%10];
}
}
}
cout<<x<<endl;
}
Min - Max Explanation:
Least = 1, Highest = 6, all
#include <iostream> numbers given are between or equal
#include <algorithm> to those 2 numbers, without anyone
of the numbers repeating itself.
using namespace std;

int main(){ Digit Problem


int t;
cin>>t; #include <iostream>

int x[t]; using namespace std;

for(int i=0;i<t;i++){ int main(){


cin>>x[i]; int k;
} string x;
cin>>x>>k;
sort(x,x+t); int i=0;
int max=x[t-1]; if(k>0){
for(int i=0;i<x.length();i+
for(int i=0;i<t;i++){ +){
if(x[i]!=x[i+1]){ if(x[i]!='9'){
x[i]=0; x[i]='9';
if(i==t-1){ k--;
x[i+1]=0; }
} if(k==0){
} break;
} }
int c=0; }
for(int i=0;i<t;i++){ }
if(x[i]==0){ cout<<x<<endl;
c++; }
}
} Input Output
4483 2 9983
if(c==max){
cout<<"YES"<<"\n";
}else{
cout<<"NO"<<"\n";
}
return 0;
}

Input Output
6 YES
4 2 1 3 5 6
Spiral Square

#include<iostream>
using namespace std;

int main(){
int n;
cout<<"Enter the size of the array :";
cin>>n; /*n Size of the array*/
int A[n][n];
int len=n,k=1,p=0,i;
/*k is to assign the values to the array from 1...n*n */
/*len is used to update(decrease) array size
so that values cans be assign to them */
while(k<=n*n){
/*Loop to access the first row of the array*/
for(i=p;i<len;i++){
A[p][i]=k++;
}
/*Loop tp access the last column of the array*/
for(i=p+1;i<len;i++){
A[i][len-1]=k++;
}
/*Loop to access the last row of the array*/
for(i=len-2;i>=p;i--){
A[len-1][i]=k++;
}
/*Loop to access the first column of the array*/
for(i=len-2;i>p;i--){
A[i][p]=k++;
}
p++,len=len-1;

}
/*This block will run only if n is even*/
if(!n%2){
A[(n+1)/2][(n+1)/2]=n*n;
/*It will assign the last value to the centermost element*/
}
/*This loop will print the array in matrix format*/
for(i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<A[i][j]<<"\t";
}
cout<<endl;
}
return 0;
}
Input
5

Output
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
#include <algorithm>

string x;

sort(x.begin(),x.end());
reverse(x.begin(),x.end());
count(x.begin(),x.end(),*target*)

int y[5]

sort(y,y+5);
reverse(y,y+5);
result = count(5,y+5,*target*);
result = max_element(y,y+5);
result = min_element(y,y+5);

#include <cctype>

char x;

islower(x) //lowercase
isupper(x) //uppercase
isdigit(x) //number
ispunct(x) //punctuation
isalpha(x) //alphabetic
isalnum(x) //alphanumeric
isspace(x) //space
toupper(x)
tolower(x)

You might also like