225
>Operatii vector:
– numere prime
– cifra de control
– numarul de triplete
– inversare
– ordonare
#include <stdio.h>
#include <conio.h>
#include <math.h>
void citire(int n, int v[20])
{
for(int i=1;i<=n;i++)
{
printf("v[%d]=",i);
scanf("%d",&v[i]);
}
}
void afisare(int n, int v[20])
{
for(int i=1;i<=n;i++)
printf(" %d ",v[i]);
}
void prime(int n, int v[20])
{
int poz=0,ok,i=1;
do
{
ok=1;
for(int j=2;j<=sqrt(v[i]);j++)
if(v[i]%j==0) ok=0;
if(ok==1)
{
printf(" %d ",v[i]); poz++; i++;
}
else i++;
} while (n>=i);
printf("nNr de nr prime=%d ",poz);
}
int suma_cifre(int numar)
{
int suma=0;
do
{
suma=suma+numar%10;
numar=numar/10;
}
while(numar!=0);
return suma;
}
void cifra_control(int n, int v[20], int control)
{
int nr=0;
for(int i=1;i<=n;i++)
if(suma_cifre(v[i]==control))
{
nr++;
printf("nNr de nr=cifra control este %d",nr);
}
}
int triplete(int n, int v[20])
{
int tri=0;
for (int i=2;i<n-1;i++)
if(v[i]==(v[i-1]+v[i+1])/2)
{
printf("nTriplete sunt: %d %d %d ",v[i-1],v[i],v[i+1]);
tri++;
}
return tri;
}
void invers(int n, int v[20])
{
int aux[20];
for(int i=1;i<=n;i++)
aux[i]=v[n+1-i];
for(i=1;i<=n;i++)
v[i]=aux[i];
}
void sortare(int n, int v[20])
{
int i,j,aux;
for(i=1;i<=n-1;i++)
for(j=i+1;j<=n;j++)
if (v[i]>v[i+1])
{
aux=v[i];
v[i]=v[i+1];
v[i+1]=aux;
}
}
main()
{
int n, control;
int v[20];
clrscr();
printf("dati n: ");
scanf("%d",&n);
citire(n,v);
afisare(n,v);
printf("nNr primen");
prime(n,v);
printf("nControln");
scanf("%d",&control);
cifra_control(n,v,control);
printf("nNr de triplete:%d", triplete(n,v));
printf("nInversare sir:n");
invers(n,v);
afisare(n,v);
printf("nOrdonare:n");
sortare(n,v); sortare(n,v);
afisare(n,v);
getch();
return 0;
}