0% found this document useful (0 votes)
58 views7 pages

Functii Elementare

The document contains 9 algorithms written in C++ for solving problems related to numbers. The algorithms include: 1) checking if a number is prime, 2) finding the greatest common divisor of two numbers, 3) reversing a number, 4) summing the digits of a number, 5) finding the minimum and maximum digits of a number, 6) finding the sum and number of divisors of a number, 7) finding the minimum and maximum divisors of a number, 8) checking if a number contains non-zero digits, and 9) counting the number of prime divisors of a number. Each algorithm is presented with its C++ code and a short main function to test it.

Uploaded by

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

Functii Elementare

The document contains 9 algorithms written in C++ for solving problems related to numbers. The algorithms include: 1) checking if a number is prime, 2) finding the greatest common divisor of two numbers, 3) reversing a number, 4) summing the digits of a number, 5) finding the minimum and maximum digits of a number, 6) finding the sum and number of divisors of a number, 7) finding the minimum and maximum divisors of a number, 8) checking if a number contains non-zero digits, and 9) counting the number of prime divisors of a number. Each algorithm is presented with its C++ code and a short main function to test it.

Uploaded by

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

Functii elementare

1. ALGORITM DE NUMAR PRIM

#include <iostream>

using namespace std;

int prim(int n)

{ int i,ok;

for(i=2;i<=n/2;i++)

if(n%i==0)

{ok=0;

break;}

else

ok=1;

if(ok==0)

return 0;

else

return 1;

int main()

{ int n;

cin>>n;

cout<<prim(n);

return 0;

2. ALGORITM CMMDC 2 NR

#include <iostream>

using namespace std;

int cmmdc(int a,int b)

{ int i,ibun=0;

for(i=2;i<=b;i++)

if(a%i==0 && b%i==0)

ibun=i;
return ibun;

int main()

{ int a,b;

cin>>a>>b;

cout<<cmmdc(a,b);

return 0;

3. ALGORITM DE INVERS NUMAR

#include <iostream>

using namespace std;

int inv(int n)

{ int uc,inv=0;

while(n>0)

uc=n%10;

n=n/10;

inv=inv*10+uc;

return inv;

int main()

{ int n;

cin>>n;

cout<<inv(n);

return 0;

4. ALGORITM PENTRU SUMA CIFRELOR UNUI NR

#include <iostream>

using namespace std;

int suma(int n)
{ int uc,s=0;

while(n>0)

uc=n%10;

n=n/10;

s=s+uc;

return s;

int main()

{ int n;

cin>>n;

cout<<suma(n);

return 0;

5. ALGORITM CEA MAI MICA SI CEA MAI MARE CIFRA A UNUI NR

#include <iostream>

using namespace std;

void minmaxnr(int x)

{ int i,uc,a[10001];

for(i=1;i<=1000;i++)

while(x>0)

{uc=x%10;

x=x/10;

a[uc]++;}

for(i=0;i<=9;i++)

if(a[i]>0)

cout<<i<<' ';
break;

for(i=9;i>=0;i--)

if(a[i]!=0)

cout<<i;

break;

int main()

{ int x;

cin>>x;

minmaxnr(x);

return 0;

6. ALGORITM NUMAR SI SUMA DE DIVIZORI

#include <iostream>

using namespace std;

void sumanr(int x)

{ int i,s=0,nr=0;

for(i=1;i<=x/2;i++)

if(x%i==0)

s=s+i;

nr++;

s=s+x;

nr++;

cout<<s<<' '<<nr;

int main()
{ int x;

cin>>x;

sumanr(x);

return 0;

7. ALGORITM MIN SI MAX DIVIZORI DE N

void sumanr(int n,int &a,int &b)

{ int i;

for(i=2;i<=n/2;i++)

if(n%i==0)

{a=i;

break;}

for(i=n/2;i>=2;i--)

if(n%i==0)

{b=i;

break;}

int main()

{ int x,a,b;

cin>>x;

sumanr(x,a,b);

cout<<a<<' '<<b;

return 0;

8. ALGORITM CIFRE NENULE

int sumanr(int n)

{ int ok=1,uc;

while(n>0)

{ uc=n%10;

n=n/10;

if(uc==0)
{ok=0;

break;}

return ok;

int main()

{ int x;

cin>>x;

cout<<sumanr(x);

return 0;

9. ALGORITM NR DIV PRIMI

void nrdivprim(int n)

{ int d=2,nr=0,p;

while(n>1)

{ p=0;

while(n%d==0)

++p;

n/=d;

if(p!=0)

nr++;

++d;

if(n>1 && d*d>n)

d=n;

}
cout<<nr;

int main()

{ int x;

cin>>x;

nrdivprim(x);

return 0;

You might also like