0% found this document useful (0 votes)
5 views5 pages

Info

The document contains multiple C++ code snippets that demonstrate various programming concepts, including finding the minimum and maximum of a series of numbers, manipulating digits of integers, calculating the greatest common divisor (GCD) and least common multiple (LCM), and determining prime numbers. Each code snippet is self-contained and illustrates a specific algorithm or technique. The overall focus is on basic number manipulation and arithmetic operations in C++.

Uploaded by

Stefan0666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Info

The document contains multiple C++ code snippets that demonstrate various programming concepts, including finding the minimum and maximum of a series of numbers, manipulating digits of integers, calculating the greatest common divisor (GCD) and least common multiple (LCM), and determining prime numbers. Each code snippet is self-contained and illustrates a specific algorithm or technique. The overall focus is on basic number manipulation and arithmetic operations in C++.

Uploaded by

Stefan0666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Det min

cin >> n >> x;


min = x;
for(int i =1 ; i < n ; i ++)
{
cin >> x;
if(x < min)
min = x;
}

Det max
cin >> x;
max = x;
cin >> x;
while(x != 0)
{
if(x > max)
max = x;
cin >> x;
}

Cifrele unui nr
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while(n != 0) // cat timp n este nenul - mai are cifre
{
int uc = n % 10;
cout << uc << " ";
n= n / 10;
}
return 0;
}

Determinarea oglinditului unui număr dat


#include <iostream>
using namespace std;
int main(){
int n;
cin >> n;
int ogl= 0;
while(n){
ogl =10*ogl + n%10;
n /= 10;
}
cout << ogl << endl;
return 0;
}
micsorarea cu o unitate a cifrelor impare
#include <iostream>
using namespace std;
int main()
{
int n , R = 0, p = 1;
cin >> n;
while(n)
{
int uc = n % 10;
if(uc % 2 == 0)
R = R + p * uc;
else
R = R + p * (uc - 1);
p = p * 10;
n = n / 10;
}
cout << R << endl;
return 0;
}

Construirea unui număr de la început la sfârșit


#include <iostream>
using namespace std;
int main(){
int n, nr = 0, p = 1;
cin >> n;
for(int i = 1; i <= n; ++i) {
int cif;
cin >> cif;
nr = cif * p + nr;
p = p * 10;
}
cout << nr << "\n";
}

Numărul de cifre
#include <iostream>
using namespace std;
int main()
{
int n, nrcif = 0;
cin >> n;
do {
nrcif++;
n = n / 10;
} while(n != 0);
cout << nrcif;
return 0;
}

CMMDC
#include <iostream>
int main()
{
int n , m;
cin >> n >> m;
while(m != 0)
{
int r = n % m;
n = m;
m = r;
}
cout << n << endl;
return 0;
}

CMMMC
#include <iostream>
using namespace std;
int main(){
int a, b;
cin >> a >> b;
int cmmdcNr;
int copiea = a, copieb = b;
while(copieb) {
int aux = copiea % copieb;
copiea = copieb;
copieb = aux;
}
cmmdcNr = copiea;
int cmmmcNr = a * b / cmmdcNr;
cout << "cmmmc(" << a << ", " << b << ") = " << cmmmcNr;
return 0;
}

Descompunere in factori primi ITERATIV


#include <iostream>
using namespace std;
int main(){
int n,f,p=0;
cin>>n;
f=2;
while(n%f==0){
n=n/f;
p++;}
if(p>0)
cout<<f<<" "<<p<<"\n";
f=3;
while(n>1){
p=0;
while(n%f==0){
n=n/f;
p++;}
if(p>0)
cout<<f<<" "<<p<<"\n";
f=f+2;}
return 0;
}

CU UN SINGUR WHILE
#include <iostream>
using namespace std;
int main(){
int n,f,p;
cin>>n;
f=2;
p=0;
while(n>=f){
if(n%f==0){
n=n/f;
p++;}
else{
if(p>0){
cout<<f<<" "<<p<<"\n";
p=0;}
f++;}
}
if(p>0){
cout<<f<<" "<<p<<"\n";
p=0;}
return 0;
}

Divizorii unui număr -naiv


#include <iostream>
int main()
{
int n;
cin >> n;
for(int d =1 ; d <= n ; d ++ )
if(n % d == 0)
cout << d << " ";
return 0;
}

Divizorii unui număr- eficient


#include <iostream>
int main()
{
int n;
cin >> n;
for(int d =1 ; d * d <= n ; d ++ )
if(n % d == 0)
{
cout << d << " ";
if(d * d < n)
cout << n / d << " ";
}
return 0;
}
Determinare prim neeficient
#include <iostream>
using namespace std;
int S = 0;
void divizori(int n) {
for (int d = 1; d <= n; d++)
if (n % d == 0)
S = S + d;
}
int main() {
int n;
cin >> n;
divizori(n);
cout << S;
return 0;
}

Determinare prim
#include <iostream>
int main()
{
int n;
cin >> n;
bool prim = true;
if(n < 2)
prim = false;
for(int d =2 ; d * d <= n ; d ++)
if(n % d == 0)
prim = false;
if(prim)
cout << n << " este prim";
else
cout << n << " nu este prim";
return 0;
}

Determinare prim eficient


#include <iostream>
int main(){
int n,i,ok=1;
cin>>n;
for(i=2;i*i<=n&&ok==1;i++)
{
if(n%i==0)ok=0;
}
if(ok==1)cout<<“Da”;
else cout<<“Nu”;
return 0;}

You might also like