You are on page 1of 38

1.

int a=4, b=2, c=10;


a+=b; // atribuirea este echivalenta cu a=a+b; a=6
b=++a+4; // b=(7)+4=11; a=7;
c=b++-4; //c=(11)-4=7; b=12;
a=b--+5; //a=(12)+5=17; b=11;
(a!=b)&&(b=c); // FALSE
(a>b) || (b>c); // TRUE
(c<b) &&(b<a) // TRUE
(c!=a) && (c>b) // FALSE
(b=a) || (c=a) // FALSE
c=++b-10; // c=(12)-10=2; b=12;

2.

1. Se citesc doua numere. Sa se schimbe continutul:

citeste a, b
aux<-a
a<-b
b<-aux
scrie a,b

#include<iostream >
using namespace std ;
int main ()
{int a, b, aux ;
cin>>a>>b ;
{aux=a ;
a=b ;
b=aux ; }
cout << a<<b ;
return 0 ;}

fisier ‘’date.in’’
#include <fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out”)
int main ()
{int a, b, aux ;
f>>a>>b;
{aux=a;
a=b;
b=aux;}
g<<a<<b
f.close ();
g.close ();
return 0 } #include<iostream>
using namespace std ;
int main ()
{int n, a, b, c, aux;
cin>>n;
a=n/100;
b=n/10%10;
c=n%10;
if (a>b)
{aux=a;
a=b;
b=aux; }
if (b>c)
2. Se citeste un numar de 3 cifre. Sa se schimbe {aux=b;
continutul: b=c;
c=aux; }
citeste n if (a>b)
a<-n/100 {aux=a;
b<-n/10%10 a=b;
b=aux; }
c<-n%10
n=a*100+b*10+c
daca (a>b) atunci aux<-a
cout<<n;
a<-b return 0; }
b<-aux

daca (b>c) atunci aux<-b


b<-c
c<-aux

daca (a>b) atunci aux<-a


a<-b
b<-aux

n<-a*100+b*10+c
scrie n

fisier ‘’date.in‘’
#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, a, b, c, aux;
f>>n;
if(a>b)
{aux=a;
a=b;
b=aux; }
if (b>c)
{aux=b;
b=c;
c=aux; }
if (a>b)
{aux=a;
a=b;
b=aux; }
n=a*100+b*10+c
g<<n ;
f.close () ;
g.close () ;
return 0 ; }

3. Se citesc 2 numere naturale m si n. Sa se calculeze n la puterea m:

citeste m, n
p<-1
pentru i<-1 la m executa
p<-p*n fisier ‘’date.in’’

scrie p #include<fstream>
ifstream f(‘’date.in’’)
#include<iostream> ofstream g(‘’date.out’’)
using namespace std ; int main ()
int main () {int m, n, i, p ;
{int m, n, i, p; f>>m>>n ;
cin>>m>>n; p=1 ;
p=1; for(i=1;i<=n;i++)
for(i=1;i<=n;i++) {p=p*n ;
{p=p*n; }
} g<<n;
cout<<n; f.close() ;
return 0; } g.close () ;
return 0; }

4. Se citesc de la tastatura n numere. Sa se afle cate numere sunt negative:

#include<iostream>
using namespace std ;
int main()
{int n, k, a;
citeste n cin>>n ;
k<-0 k=0;
pentru i<-1 la n executa for(i=1;i<=n;i++)
citeste a {cin>>a
if (a>0)
daca (a<0) atunci k<-k+1
{k=k+1
}
}
scrie k cout<< k
return o; }

fisier “date.in”

#include<fstream>
ifstream f(‘’date.in”)
ofstream g(‘’date.out’’)
int main ()
{int n, k, a;
f>>n;
for(i=1;i<=n;i++)
{f>>a;
if (a>0)
{k=k+1;
}
}
g<<k;
f.close ();
g.close ();
return 0; }

5. Se citesc de la tastatura n numere intregi. Sa se calculeze media aritmetica a numerelor pare:

citeste n
S<-0
k<-0
pentru i<-1 la n executa
citeste a
daca (a%2=0) atunci S<-S+a
k<-k+1
m<-S/k

scrie m

#include<iostream
using namespace std;
int main ()
{int n, S, k, a, m;
cin>>n;
S=0
k=0
for(i=1;i<=n;i++)
{cin>>a;
if (a%2=0)
{S=S+a
k=k+1
m=S/k}
}
cout<<m
return 0; }

fisier “date.in”

#include<fstream>
ifstream f(‘’date.in”)
ofstream g(‘’date.out’’)
int main ()
{int n, S, k, a, m;
f>>n;
for(i=1;i<=n;i++)
{f>>a
if (a%2=0)
{S=S+a
k=k+1
m=S/k}
}
g<<m
f.close ();
g.close ();
return 0; }

6. Se citesc 2 nr a si b. Sa se afiseze maximul celor 2:

citeste a, b
daca (a>b) atunci max<-a
altfel max<-b

scrie max

#include<iostream>
using namespace std;
int main ()
{int a, b
cin>>a>>b
{if (a>b)
{max=a;
}
else max=b; }
cout<<max;
return 0; }

fisier ‘’date.in’’
#include<fstream>
ifstream f(‘’date.in”)
ofstream g(‘’date.out’’)
int main ()
{int a, b;
f>>a>>b;
{if (a>b)
{max=a;
}
else max=b; }
g<<max;
f.close ();
g.close ();
return 0; }

7. Se dau 3 nr a, b, c. Sa se afiseze maximul celor 3:


citeste a, b, c
max<-a
daca (b>max) atunci max<-b

daca (c>max) atunci max<-c

scrie max

#include<iostream>
using namespace std ;
int main ()
{int a, b, c;
cin>>a>>b>>c;
max=a
if (b>max)
{max=b;
}
if (c>max)
{max=c;
}
cout<<max;
return 0; }

Fisier ‘’date.in’’
#include<fstream>
ifstream f(“date.in”)
ofstream g(‘’date.out’’)
int main ()
{ int a, b, c;
f>>a>>b>>c;
max=a
if (b>max)
{max=b;
}
if (c>max)
{max=c;
}
g<<max;
f.close ();
g.close ();
return 0; }

8. Se dau n numere. Sa se afiseze maximul:


citeste n
citeste a
max<-a
pentru i<-2 la n executa
citeste a
daca (a>max) atunci max<-a

scrie max

#include<iostream>
using namespace std ;
int main ()
{int n, a, i;
cin>>n;
cin>>a;
for(i=2;i<=n;i++)
{cin>>a;
if (a>max)
{max=a;
}
}
cout<<max;
return 0; }

Fisier “date.in”

#include<fstream>
ifstream f(“date.in”)
ofstream g(‘’date.out’’)
int main ()
{int n, a, i;
f>>n;
f>>a;
for(i=2;i<=n;i++)
{f>>a
if (a>max)
{max=a
}
}
g<<max;
f.close ();
g.close ();
return 0; }

9. Se citesc n numere. Sa se afiseze maximul si de cate ori apare:

citeste n
citeste a
max<-a
k<-1
pentru i<-2 la n executa
citeste b
daca (b=max) atunci k<-k+1
altfel daca (b>max) atunci max<-b
k<-1

scrie max, k

#include<iostream>
using namespace std ;
int main ()
{int n, a, k, i, b;
cin>>n;
cin>>a;
max=a;
k=1;
for(i=2;i<=n;i++)
{cin>>b;
if(b=max)
{k=k+1;
}
else if (b>max)
{max=b;
k=1;
}
}
cout<<max<<k;
return 0; }

Fisier “date.in”

#include<fstream>
ifstream f’(’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, a, k, i, b;
f>>n;
f>>a;
max=a
k=1
for(i=2;i<=n;i++)
{f>>b;
if(b=max)
{k=k+1;
}
else if (b>max)
{max=b;
k=1;
}
}
g<<max<<k;
f.close ();
g.close ();
return 0; }

10. Se citesc numere pana la intalnirea valorii 0. Sa se afiseze maximul:

citeste a
max<-a
cat timp (a !=0) executa
daca (a>max) atunci max<-a

citeste a

scrie max

#include<iostream>
using namespace std ;
int main()
{int a;
cin>>a;
max=a
while (a!=0)
{if (a>max)
{max=a;
}
}
cin>>a;
cout<<max;
return 0;}

#include<fstream>
ifstream f (‘’date.in”);
ofstream g (‘’date.out”)
int main ()
{int a;
f>>a;
max=a
while (a!=0)
{if (a>max)
{max=a;
}
}
f>>a;
g<<max;
f.close ();
g.close ();
return 0;}

11. Se citesc numere pana la intalnirea valorii 0. Sa se afiseze maximul si de cate ori apare:

citeste a
max<-a
k<-1
cat timp (a !=0) executa
daca (a=max) atunci k<-k+1
altfel daca (a>max) atunci max<-a
k<-1

scrie max, k

#include<iostream>
using namespace std;
int main()
{int a, k
cin>>a
max=a
k=1
while (a!=0)
{if (a=max)
{k=k+1
}
else if (a>max)
{max=a;
k=1;
}
}
cout<<max<<k;
return 0; }

Fisier “date.in”

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int a, k
f>>a
max=a
k=1
while (a!=0)
{if (a=max)
{k=k+1
}
else if (a>max)
{max=a;
k=1;
}
}
g<<max<<k;
f.close ();
g.close ();
return 0; }

12. Se citeste un numar n. Sa se afiseze cifrele sale:

citeste n
cat timp (n !=0) executa
C<-n%10
scrie C
n<-n/10
#include<iostream>
using namespace std;
int main ()
{int n;
cin>>n
while (n!=0)
{C=n%10
cout<<C
}
n=n/10
return 0;}

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n;
f>>n
while (n!=0)
{C=n%10
g<<C
}
n=n/10;
f.close ();
g.close ();
return 0;}

13. Se citeste un numar n. Sa se afiseze suma cifrelor sale:

citeste n
S<-0
cat timp (n !=0) executa
C<-n%10
S<-S+C
n<n/10

scrie S

#include<iostream>
using namespace std’
int main()
{int n, S
cin>>n;
S=0;
while (n!=0)
{C=n%10
S=S+C
n=n/10
}
cout<<S;
return 0;}

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S
f>>n;
S=0;
while (n!=0)
{C=n%10
S=S+C
n=n/10
}
g<<S;
f.close ();
g.close ();
return 0;}

14. Se da un numar n. Sa se calculeze produsul cifrelor sale.

citeste n
p<-1
cat timp (n !=0) executa
C<-n%10;
p<-p*C;
n<-n/10;

scrie p
#include<iostream>
using namespace std;
int main ()
{int n, p
cin>>n;
p=1;
while (n!=0)
{C=n%10;
p=p*C;
n=n/10;
}
cout<<S;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, p
f>>n;
p=1;
while (n!=0)
{C=n%10;
p=p*C;
n=n/10;
}
g<<S;
f.close ();
g.close ();
return 0; }

15. Se da un numar n. Sa se calculeze suma cifrelor pare:

citeste n
S<-0
cat timp (n !=0) executa
C<-n%10
daca (C%2=0) atunci S<-S+C

n<-n/10

scrie S
#include<iostream>
using namespace std;
int main ()
{int n, S
cin>>n;
S=0;
while (n!=0)
C=n%10
{ if (C%2=0)
{S=S+C
}
}
n=n/10
cout<<S;
return 0 ;}

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S
f>>n;
S=0;
while (n!=0)
C=n%10
{ if (C%2=0)
{S=S+C
}
}
n=n/10
g<<S;
f.close ();
g.close();
return 0 ;}

16. Se da un numar n. Sa se culculeze produsul cifrelor pare:

citeste n
p<-1
cat timp (n !=0) executa
C<-n%10
daca (C%2=0)atunci p<-p*C
n<-n/10

scrie p

#include<iostream>
using namespace std;
int main ()
{int n, p
cin>>n;
p=1;
while(n!=0)
C=n%10
{if (C%2=0)
{p=p*C;
}

}
n=n/10 ;
cout<<p ;
return 0 ; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, p
f>>n;
p=1;
while(n!=0)
C=n%10
{if (C%2=0)
{p=p*C;
}

}
n=n/10 ;
g<<p ;
f.close ();
g.close ();
return 0; }

17. Se da un numar n. Sa se afiseze suma cifrelor impare:


citeste n
S<-0
cat timp (n !=0) executa
C<-n%10
daca (C%2!=0) atunci S<-S+C

n<-n/10

scrie S

#include<iostream>
using namespace std;
int main()
{int n, S
cin>>n;
S=0;
while(n!=0)
C=n%10;
{if (C%2!=0)
{S=S+C;
}
}
n=n/10;
cout<<S;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S
f>>n;
S=0;
while(n!=0)
C=n%10;
{if (C%2!=0)
{S=S+C;
}
}
n=n/10;
g<<S;
f.close ();
g.close ();
return 0; }

18. Se da un numar n. Sa se afiseze produsul cifrelor impare:

citeste n
p<-1
cat timp (n !=0) executa
C<-n%10
daca (C%2!=0) atunci p<-p*C

n<-n/10

scrie p

#include<iostream>
using namespace std;
int main()
{int n, p
cin>>n;
p=1;
while (n!=0)
C=n%10
{if (C%2!=0)
{p=p*c;
}
}
n=n/10;
cout<<p;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, p
f>>n;
p=1;
while (n!=0)
C=n%10
{if (C%2!=0)
{p=p*c;
}
}
n=n/10;
g<<p;
f.close ();
g.close ();
return 0; }

19. Se da un numar n. Sa se afiseze numarul format din cifrele pare:

citeste n
nr<-0
p<-1
cat timp (n !=0) executa
C<-n%10
daca (C%2=0) atunci nr<-nr+C*p
p<-p*10

n<-n/10

scrie nr

#include<iostream>
using namespace std ;
int main ()
{int n, nr, p
cin>>n
nr=0;
p=1;
while (n!=0)
C=n%10
{if (C%2=0)
{nr=nr+C*p;
p=p*10;
}
}
n=n/10;
cout<<nr ;
return 0 ; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, nr, p
f>>n
nr=0;
p=1;
while (n!=0)
C=n%10
{if (C%2=0)
{nr=nr+C*p;
p=p*10;
}
}
n=n/10;
g<<nr ;
f.close () ;
g.close () ;
return 0 ; }

20. Se citeste un numar n. Sa se afiseze inversul:

citeste n
inv<-0
cat timp (n !=0) executa
C<-n%10
inv<-inv*10+c
n<-n/10

scrie inv

#include<iostream>
using namespace std ;
int main ()
{int n, inv
cin>>n;
inv=0
while(n !=0)
{C=n%10;
inv=inv*10+c;
n=n/10;
}
cout<<inv;
return 0;}

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, inv
f>>n;
inv=0
while(n !=0)
{C=n%10;
inv=inv*10+c;
n=n/10;
}
g<<inv;
f.close ()
g.close ()
return 0;}

21. Se da un numar n. Sa se afiseze ultima cifra:

citeste n
C<-n%10
scrie C

#include<iostream>
using namespace std ;
int main ()
{int n
cin>>n;
C=n%10
cout<<C;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n
f>>n;
C=n%10
g<<C;
f.close ();
g.close ();
return 0; }

22. Se citeste un numar n. Sa se afiseze suma primelor n numere:

citeste n
S<-0
pentru i<-1 la n executa
S<-S+i

scrie S

#include<iostream>
using namespace std;
int main ()
{int n, S, i
cin>>n;
S=0;
for(i=1;i<=n;i++)
{S=S+i;
}
cout<<S;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S, i
f>>n;
S=0;
for(i=2;i<=n;i++)
{S=S+i;
}
g<<S;
f.close ();
g.close ();
return 0; }

23. Se citeste un sir de numere. Sa se afiseze palindroamele:


citeste n
pentru i<-1 la n executa
citeste a
x<-a
inv<-0
cat timp (x !=0) executa
inv<-inv*10+x%10
x<-x/10

daca (a=inv) scrie a

#include<iostream>
using namespace std ;
int main ()
{int n, I , a, x, inv
cin>>n;
for(i=2;i<=n;i++)
{cin>>a;
x=a;
inv=0;
{while (x!=0)
{inv=inv*10+x%10;
x=x/10;
}
}
}
if(a==inv) cout<<a
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, I , a, x, inv
f>>n;
for(i=1;i<=n;i++)
{f>>a;
x=a;
inv=0;
{while (x!=0)
{inv=inv*10+x%10;
x=x/10;
}
}
}
if(a==inv) g<<a;
f.close ();
g.close ();
return 0; }

24. Se da un numar n. Sa se afiseze produsul divizorilor numarului:

citeste n
p<-1
pentru i<-1 la n executa
daca (n%i=0) atunci p<-p*i

scrie p

#include<iostream>
using namespace std ;
int main()
{int n, p, i ;
cin>>n;
p=1;
for(i=1;i<=n;i++)
{if (n%i=0)
{p=p*1;
}
}
cout<<p;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, p, i ;
f>>n;
p=1;
for(i=1;i<=n;i++)
{if (n%i=0)
{p=p*1;
}
}
g<<p;
f.close ();
g.close ();
return 0; }

25. Sa se determine cel mai mare numar natural nenul p cu proprietatea ca 3 la puterea p sa fie mai
mic sau egal decat n unde n este un numar mai mare sau egal cu 3:

citeste n
p<-0
t<-1
cat timp(t<=n) executa
t<-t*3
p<-p+1

scrie p-1

#include<iostream>
using namespace std ;
int main ()
{int n, p, t
cin>>n ;
p=0 ;
t=1 ;
while (t<=n)
{ t=t*3;
p=p+1;
}
cout<<p-1 ;
return 0 ; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, p, t
f>>n ;
p=0 ;
t=1 ;
while (t<=n)
{ t=t*3;
p=p+1;
}
g<<p-1 ;
f.close () ;
g.close () ;
return 0 ; }

26. Se citesc n numere nenule. Sa se determine in cate zerouri se termina produsul lor fara a se face
inmultirea:

citeste n
x<-0
y<-0
pentru i<-1 la n executa
citeste a
doi<-0
cinci<-0
cat timp (a%2=0)
a<-a/2
doi<-doi+1

cat timp (a%5=0)


a<-a/5
cinci<-cinci+1

x<-x+doi
y<-y+cinci
daca (x<y) atunci scrie ‘’x zerouri’’
‘’y zerouri’’

#include<iostream>
using namespace std ;
int main ()
{int x, y, a, i
cin>>n;
x=0;
y=0;
for(i=1;i<=n;i++)
{cin>>a;
doi=0;
cinci=0;
{while (a%2=0)
{a=a/2;
doi=doi+1;
}
}
{while (a%5=0)
{a=a/5;
cinci=cinci+1;
}
}
x=x+doi;
y=y+cinci;
if (x<y)
cout<<”x zerouri”<<”y zerouri” ;
return 0 ; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int x, y, a, i
f>>n;
x=0;
y=0;
for(i=1;i<=n;i++)
{f>>a;
doi=0;
cinci=0;
{while (a%2=0)
{a=a/2;
doi=doi+1;
}
}
{while (a%5=0)
{a=a/5;
cinci=cinci+1;
}
}
x=x+doi;
y=y+cinci;
if (x<y)
g<<”x zerouri”<<”y zerouri” ;
f.close () ;
g.close () ;
return 0 ; }

27. Algoritm care calculaza suma marimilor a 2 intervale de timp exprimate in ore, min si sec.

citeste h1m1s1, h2m2s2


h<-h1+h2
m<-m1+m2
s<-s1+s2
daca (s>60) atunci m<-m+1
s<-s-60

daca (m>60) atunci h<-h+1


m<-m-60

scrie h, m, s

#include<iostream>
using namespace std;
int main ()
{int h, m, s
cin>>h1m1s1, h2m2s2;
h=h1+h2;
m=m1+m2;
s=s1+s2;
if(s>60)
{m=m+1;
s=s-60;
}
if(m>60)
{h=h+1;
m=m-60;
}
cout<<h, m, s;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int h, m, s
f>>h1m1s1, h2m2s2;
h=h1+h2;
m=m1+m2;
s=s1+s2;
if(s>60)
{m=m+1;
s=s-60;
}
if(m>60)
{h=h+1;
m=m-60;
}
g<<h, m, s;
f.close ();
g.close ();
return 0; }

28. Se citeste un numar natural nenul. Scrie un algoritm pentru produsul primelor numere pare
nenule:

citeste n
k<-0
a<-2
p<-1
cat timp (k<=n) executa
daca (a%2=0) atunci p<-p*a
k<-k+1

a<-a+1

scrie p

#include<iostream>
using namespace std;
int main ()
{int n, k, a, p
cin>>n;
k=0;
a=2;
p=1;
while (k<=n)
{if (a%2=0)
{p=p*a;
k=k+1;
}
}
a=a+1
cout<<p;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, k, a, p
f>>n;
k=0;
a=2;
p=1;
while (k<=n)
{if (a%2=0)
{p=p*a;
k=k+1;
}
}
a=a+1
g<<p;
f.close ();
g.close ();
return 0; }

29. Se citeste un numar natural n. Sa se calculeze ultima cifra a sumei tuturor numerelor naturale
cel mult egale cu n:

citeste n
S<-0
a<-1
cat timp (S<=n) executa
S<-S+a
a<-a+1
c<-S%10
scrie c

#include<iostream>
using namespace std;
int main ()
{int n, S, a, c
cin>>n;
S=0;
a=1 ;
while (S<=n)
{S=S+a;
a=a+1;
}
c=S%10;
cout<<c
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S, a, c
f>>n;
S=0;
a=1 ;
while (S<=n)
{S=S+a;
a=a+1;
}
c=S%10;
g<<c
f.close ();
g.close ();
return 0; }

30. Se citesc succesiv numere pana la intalnirea valorii 0. Sa se scrie un algoritm care sa afiseze cel
mai mare si cel mai mic numar:

citeste a
max<-a
min<-a
cat timp (a !=0) executa
citeste a
daca (a>max) atunci max<-a

daca (a<min) atunci min<-a


citeste a

scrie max, min

#include<iostream>
using namespace std ;
int main ()
{int a, max,min
cin>>a
max=a
min=a
while (a!=0)
cin>>a
{if (a>max)
{max=a;
}
}
{if (a<min)
{min=a;
}
}
cin>>a;
cout<<max<<min;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int a, max,min
f>>a
max=a
min=a
while (a!=0)
f>>a
{if (a>max)
{max=a;
}
}
{if (a<min)
{min=a;
}
}
f>>a;
g<<max<<min;
f.close ();
g.close ();
return 0; }

31. Sa se calculeze suma:


S=1+2+3…+n

citeste n
S<-0
pentru i<-1 la n executa
S<-S+i

scrie S

#include<iostream>
using namespace std;
int main ()
{int n, S, i
cin>>n;
S=0
for(i=1;i<=n;i++)
{S=S+i
}
cout<<S;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, S, i
f>>n;
S=0
for(i=1;i<=n;i++)
{S=S+i
}
g<<S;
f.close ();
g.close ();
return 0; }
32. Se citesc numere naturale crescator pana la intalnirea valorii 0. Sa se afiseze numerele distincte
si frecventa lor:

citeste nr
k<-0
cat timp (nr !=0) executa
citeste a, b
daca a=b atunci
k<-k+1
citeste a, b

scrie nr, k

#include<iostream>
using namespace std;
int main ()
{int nr, k, a, b
cin>>nr
k=0;
while (nr!=0)
cin>>a>>b
{if a==b
{k=k+1;
cin>>a>>b;
}
}
cout<<nr<<k;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int nr, k, a, b
f>>nr
k=0;
while (nr!=0)
f>>a>>b
{if a==b
{k=k+1;
f>>a>>b;
}
}
g<<nr<<k;
f.close ();
g.close ();
return 0; }

33. Se citesc n numere. Sa se afiseze c.m.m.d.c.(scaderi):

citeste n
cat timp (a !=b) executa
daca (a>b) atunci a<-a-b
altfel b<-b-a

scrie a

#include<iostream>
using namespace std;
int main ()
cin>>n
while (a!=b)
{if (a>b)
{a=a-b;
}
else b=b-a; }
cout<<a;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
f>>n
while (a!=b)
{if (a>b)
{a=a-b;
}
else b=b-a; }
g<<a;
f.close ();
g.close ();
return 0; }

34. Se citesc n numere. Sa se afiseze c.m.m.d.c.(rest):


citeste a, b
r<-a%b
cat timp (r !=0) executa
a<-b
b<-r
r<-a%b

scrie b

#include<iostream>
using namespace std;
int main ()
{int a, b, r
cin>>a>>b;
r=a%b;
while (r!=0)
{a=b;
b=r;
r=a%b;
}
cout<<b;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int a, b, r
f>>a>>b;
r=a%b;
while (r!=0)
{a=b;
b=r;
r=a%b;
}
g<<b;
return 0; }

35. Algoritm pentru precizarea numarului de cifre al unui numar:

citeste n
nr<-0
cat timp (n>0) executa
nr<-nr+1
n<-n/10

scrie nr

#include<iostream>
using namespace std;
int main ()
{int n, nr
cin>>n;
nr=0;
while (n>0)
{nr=n+1;
n=n/10;
}
cout<<nr;
return 0; }

#include<fstream>
ifstream f(‘’date.in’’)
ofstream g(‘’date.out’’)
int main ()
{int n, nr
f>>n;
nr=0;
while (n>0)
{nr=n+1;
n=n/10;
}
g<<nr;
f.close ();
g.close ();
return 0; }