0% found this document useful (0 votes)
57 views4 pages

Algoritmi Fundamentali

The document contains C++ code snippets for various algorithms related to numbers including: finding the maximum and minimum value in a list, determining if a number is prime, finding all divisors of a number, extracting digits of a number, composing a number from its digits, finding the inverse of a number, calculating greatest common divisor using Euclid's and repeated subtraction algorithms, finding the least common multiple, and factorizing a number.

Uploaded by

Zlatescu Teodor
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)
57 views4 pages

Algoritmi Fundamentali

The document contains C++ code snippets for various algorithms related to numbers including: finding the maximum and minimum value in a list, determining if a number is prime, finding all divisors of a number, extracting digits of a number, composing a number from its digits, finding the inverse of a number, calculating greatest common divisor using Euclid's and repeated subtraction algorithms, finding the least common multiple, and factorizing a number.

Uploaded by

Zlatescu Teodor
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/ 4

Maxim

#include<iostream>
using namespace std;
int main()
{
int n, a, i, maxi=0;
cout<<"n="; cin>>n;
for (i=1; i<=n; i++)
{
cout<<"a=";cin>>a;
if (a>maxi)
maxi=a;
}
cout<<"max="<<maxi;
return 0;
}
Minim
#include<iostream>
using namespace std;
int main()
{
int n, a, i, mini=99999999;
cout<<"n="; cin>>n;
for (i=1; i<=n; i++)
{
cout<<"a=";cin>>a;
if (a<mini)
mini=a;
}
cout<<"mini="<<mini;
return 0;
}
prim
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n, d;
int prim=1;
cout<<"n="; cin>>n;
for(d=2;d<=sqrt(n); d++)
if (n%d==0)
{prim=0;break;}// break opreste bucla
if (prim!=0)
cout<<n<<" este numar prim";
else
cout<<n<<" nu este numar prim";
return 0;
}

divizori
#include<iostream>
using namespace std;
int main()
{
int n, i;
cout<<"n="; cin>>n;
for (i=2; i<n; i++)
if (n % i == 0)
cout<<i<<endl;
return 0;
}
extragere a cifrelor unui numar
#include <iostream>
using namespace std;
int n;
int main()
{
cin>>n;
while(n!=0)
{
cout<<n%10<<" ";
n=n/10;
}
return 0;
}
compunere a unui numar dandu-se cifrele
#include <iostream>
using namespace std;
int n,i,p=1,nr,c;
int main()
{
cin>>n;
p=1;
for(i=1;i<n;i++)
p=p*10;
nr=0;
for(i=1;i<=n;i++)
{
cin>>c;
nr=nr+c*p;
p=p/10;
}
cout<<nr;
return 0;
}
inversului unui numar

#include <iostream>
using namespace std;
int n,inv=0;
int main()
{
cin>>n;
while(n!=0)
{
inv=inv*10+n%10;
n=n/10;
}
cout<<inv;
return 0;
}
cmmdc Euclid
#include <iostream>
using namespace std;
int a,b,r;
int main()
{
cin>>a>>b;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
cout<<a;
return 0;
}
cmmdc scaderi repetate
#include <iostream>
using namespace std;
int a,b,r;
int main()
{
cin>>a>>b;
while(b!=a)
if(a>b)
a=a-b;
else
b=b-a;
cout<<a;
return 0;
}
cmmmc
#include <iostream>
using namespace std;
int a,b,r,auxa,auxb;

int main()
{
cin>>a>>b;
auxa=a;
auxb=b;
while(b!=a)
if(a>b)
a=a-b;
else
b=b-a;
cout<<(auxa*auxb)/a;
return 0;
}
descompunere in factori
#include <iostream>
using namespace std;
int n,i=2,fm;
int main()
{
cin>>n;
do{
fm=0;
while(n%i==0)
{
fm++;
n=n/i;
}
if(fm!=0)
cout<<i<<"^"<<fm<<endl;
i++;
}while(n!=1);
return 0;
}

You might also like