Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Teza Info

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

1.subprogram care calculeaza suma cifrelor unui numar.

#include <iostream>
using namespace std;
void suma(int n)
{
int s=0;
while(n!=0){

s=s+n%10;
n=n/10;
}
cout<<s;
}
int main()
{
int n;
cin>>n;
suma(n);
return 0;
}

2.subprogram care calculeaza suma cifrelor pare ale unui numar.


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

3.subprogram care calculeaza suma cifrelor impare ale unui numar.


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

4.subprogram care sa returneze numarul cifrelor lui n.


#include <iostream>
using namespace std;
void nr_cif(int n)
{
int cnt=0;
while(n!=0){
cnt++;
n=n/10;
}
cout<<cnt;
}
int main()
{
int n;
cin>>n;
nr_cif(n);
return 0;
}

5.subprogram care sa returneze numarul cifrelor pare ale lui n.


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

6.subprogram care sa returneze numarul cifrelor impare ale lui n.


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

7.subprogram care sa returneze oglinditul numarului.


#include <iostream>
using namespace std;
int oglindit(int n)
{
int ogl=0;
while(n!=0)
{
ogl=ogl*10+n%10;
n=n/10;
}
cout<<ogl;
}
int main()
{
int n;
cin>>n;
oglindit(n);
}

8.subprogram care verifica daca un numar este palindrom.


#include <iostream>
using namespace std;
int palindrom_n(int n)
{
int ogl=0,cn;
cn=n;
while(cn!=0)
{
ogl=ogl*10+cn%10;
cn=cn/10;
}
if(n==ogl)
return 1;
else return 0;
}
int main()
{
int n;
cin>>n;
cout<<palindrom_n(n);
return 0;
}

9.subprogram care sa returneze cifra de control.


#include <iostream>
using namespace std;
void cif_control(int n)
{
while(n>9)
{
n=n/10+n%10;
}
cout<<n;
}
int main()
{
int n;
cin>>n;
cif_control(n);
return 0;
}

10.subprogram care verifica daca n e prim.


int prim(int n)
{
int d;
if(n==1||n==0)
return 0;
for(d=2;d<=n/2;d++)
if(n%d==0)
return 0;
return 1;
}

11.subprogram care sa afiseze toti divizorii unui numar.


#include <iostream>
using namespace std;
int div(int n)
{
int d;
for(d=2;d<=n/2;d++)
if(n%d==0)
cout<<d<<" ";
}
int main()
{
int n;
cin>>n;
div(n);
return 0;
}

12.subprogram care sa afiseze divizorii primi ai unui numar.


#include <iostream>
using namespace std;
int div_prim(int n)
{
for(int d = 2; d * d <= n; d++)
{
if(n % d == 0)
{
int putere = 0;
while(n % d == 0)
{
putere++;
n = n / d;
}
cout << d << "^" << putere << " ";
}
}
if(n > 1)
{
cout << n << "^1";
}
}
int main()
{
int n;
cin>>n;
div_prim(n);
return 0;
}

13.subprogram cel mai mare divizor comun.


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

14.citire vector.
void citire(int v[100], int& n)
{
cin>>n;
for(int i=1; i<=n;i++)
cin>>v[i];
}

15.afisare vector
void afisare(int v[100], int n)
{
for(int i=1; i<=n;i++)
cout<<v[i]<<" ";
}

16.suma elementelor unui vector.


#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n;i++)
cin>>v[i];
}
int suma(int v[100], int n)
{
int s=0;
for(int i=0; i<=n;i++)
s=s+v[i];
cout<<s;
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
suma(v,n);
return 0;
}

17.suma elementelor pare ale unui vector.


#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n;i++)
cin>>v[i];
}
int suma_pare(int v[100], int n)
{
int s=0;
for(int i=0; i<=n;i++)
if(v[i]%2==0)
s=s+v[i];
cout<<s;
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
suma_pare(v,n);
return 0;
}

18.suma elementelor impare ale unui vector.


#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n;i++)
cin>>v[i];
}
int suma_impare(int v[100], int n)
{
int s=0;
for(int i=0; i<=n;i++)
if(v[i]%2==1)
s=s+v[i];
cout<<s;
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
suma_impare(v,n);
return 0;
}

19.subprogram care returneaza media aritmetica a elementelor vectorului.


#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n;i++)
cin>>v[i];
}
int ma(int v[100], int n)
{
int s=0,i;
for(i=0; i<=n;i++)
s=s+v[i];
cout<<float(s/n);
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
ma(v,n);
return 0;
}

20.cautarea secventiala.
#include<iostream>
using namespace std;
int main()
void main()
{
int v[10], i, n, gasit=0, k, poz;
cout<<"Nr. elemente = ";
cin>>n;
for ( i=1; i<=n; i++)
{
cout<<"Elementul = ";
cin>>v[i];
}
cout<<“\n Cautam elementul : ";
cin>>k;
for (i=1; i<=n; i++)
if (v[i]==k)
{
poz=i;
gasit=1;
}
if (gast == 1)
cout<<“Gasit pe pozitia "<<poz;
else cout<<k<<“ nu e in vector";
return 0;
}
21.cautarea binara.
#include<iostream>
using namespace std;
int main()
{
int a[9], n,i, x, gasit=0, st, dr, mij;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i]<<" ";
dr=n;
st=0;
cout<<"\nElementul cautat = ";
cin>>x;
while (st<=dr && !gasit)
{
mij=(st+dr)/2;
if(a[mij]==x) gasit=1;
else if (x<a[mij]) dr=mij-1;
else st=mij+1;
}
if (gasit==0) cout<<"Element negasit !";
else cout<<"Gasit pe pozitia "<<mij;
return 0;

22.sortare crescatoare

#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n; i++)
cin>>v[i];
}
void sortare_crescator(int v[], int n)
{
int i, j, aux;
for (i=1; i<n; i++)
for(j=i+1; j<=n; j++)
if (v[i]>v[j])
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
}
for (i=1; i<=n; i++)
cout<<" "<<v[i];
}

int main()
{
int v[100],n;
cin>>n;
citire(v,n);
sortare_crescator(v,n);
return 0;
}
23.sortare descrescatoare
#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n; i++)
cin>>v[i];
}
void sortare_descrescator(int v[], int n)
{
int i, j, aux;
for (i=1; i<n; i++)
for(j=i+1; j<=n; j++)
if (v[i]<v[j])
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
}
for (i=1; i<=n; i++)
cout<<" "<<v[i];
}

int main()
{
int v[100],n;
cin>>n;
citire(v,n);
sortare_descrescator(v,n);
return 0;
}

24.suma pe liniile unei matrice.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],s;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare suma
for (i=1; i<=m; i++)
{
s=0;
for (j=1; j<=n; j++)
s=s+a[i][j];
cout<<"suma pe linia"<<i<<"="<<s<<endl;
}
return 0;
}

25.suma pe coloanele unei matrici.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],s;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare suma
for (j=1; j<=n; j++)
{
s=0;
for (i=1; i<=m; i++)
s=s+a[i][j];
cout<<"suma pe coloana"<<j<<"="<<s<<endl;
}
return 0;
}

26.elementul maxim de pe fiecare linie.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],max;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare maxim
for (i=1; i<=m; i++)
{
max=a[i][1];
for (j=1; j<=n; j++)
if(a[i][j]>max) max=a[i][j];
cout<<"elementul maxim de pe linia"<<i<<"="<<max<<endl;
}
return 0;
}

27.elementul minim de pe fiecare linie.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],min;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare maxim
for (i=1; i<=m; i++)
{
min=a[i][1];
for (j=1; j<=n; j++)
if(a[i][j]<min)
min=a[i][j];
cout<<"elementul minim de pe linia"<<i<<"="<<min<<endl;
}
return 0;
}

28.elementul maxim de pe fiecare coloana.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],max;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare maxim
for (j=1; j<=n; j++)
{
max=a[1][j];
for (i=1; i<=m; i++)
if(a[i][j]>max) max=a[i][j];
cout<<"elementul maxim de pe coloana"<<j<<"="<<max<<endl;
}
return 0;
}

29.elementul minim de pe fiecare coloana.


#include<iostream>
using namespace std;
int main()
{
int m,n,i,j,a[10][10],min;
cout<<"m=";
cin>>m;//nr de linii
cout<<"n=";
cin>>n;//nr de coloane
//citirea
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
cout<<"a["<<i<<','<<j<<"]=";
cin>>a[i][j];
}
//calculare maxim
for (j=1; j<=n; j++)
{
min=a[1][j];
for (i=1; i<=m; i++)
if(a[i][j]<min)
min=a[i][j];
cout<<"elementul minim de pe coloana"<<j<<"="<<min<<endl;
}
return 0;
}

30.zonele unei matrici nord sud est vest.


N:i<j && i+j<n-1
S:i>j && i+j>n-1
E:i<j && i+j>n-1
V:i>j && i+j<n-1

You might also like