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

Informatica: 2) Produsul A N Nr. Intregi

The document contains 14 code snippets written in C++ for solving various math and logic problems. The code snippets include programs to calculate the sum and product of the first n natural numbers, find the greatest common divisor of two numbers, determine if a number is prime, calculate the sum and product of the digits of a number, and other numerical calculations and comparisons.

Uploaded by

Gabi Dragomir
Copyright
© Attribution Non-Commercial (BY-NC)
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)
44 views4 pages

Informatica: 2) Produsul A N Nr. Intregi

The document contains 14 code snippets written in C++ for solving various math and logic problems. The code snippets include programs to calculate the sum and product of the first n natural numbers, find the greatest common divisor of two numbers, determine if a number is prime, calculate the sum and product of the digits of a number, and other numerical calculations and comparisons.

Uploaded by

Gabi Dragomir
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 4

Informatica

1)1 la n, ( s=1+2+3+.....+n).

#include<iostream.h> #include<conio.h> int n, x, s; void main() {cout<<"n="; cin>>n; s=0; for (x=1; x<=n; x++) s=s+x; cout<<"s="<<s; getch(); }
2) Produsul a n nr. intregi

#include<iostream.h> #include<conio.h> int n, x, p; void main() {cout<<"n="; cin>>n; p=1; for (x=1; x<=n; x++2) p=p*x; cout<<"p="<<p; getch(); }
30Suma a n nr. Intregi

#include<iostream.h> int m,n,x,p; void main() { cout<<"n="; cin>>n; s=0; for (x=1; x<=n; x++) {cin>>m; s=s+m;} getch(); }
4. n este patrat perfect

#include<iostream.h> #include<math.h> int x; void main() { cout<<"x="; cin>>x; if (x=int(sqrt(x*x))) cout<<x<<" patrat perfect"; else cout<<x<"nu este patrat perfect"; getch(); }
5. cel mai mare divizor comun a 2 nr. Date

#include<iostream.h> int n,m,r; void main() { cout<<"n="; cin>>n; while (m!=0) {r=n%m; n=m; m=n;} cout<<"cmmdc="<<n; }

7. Sa se citeasca de la tastatura caractere pana se introduce valoarea 2. Afisati caracterele citite.

#include<iostream.h> char x; void main() { do {cout<<x=; cin>>x; cout<<x<< ; } while(x!=2); cout<<ai tastat caracterul 2,deci STOP.;}
8. Sa se calculeze suma numerelor pare, suma numerelor pozitive si maximul numerelor.

#include<iostream.h> int n, i ,s , sp; void main() {cout<<n=; cin>>n; i=1; s=0; sp=0; while(i<=n) { cin>>x; if(x%2=0) s=s+x; if(x>0) sp=sp+x; if(i=1) max=x; else if(max<x) max=x; i++; } }
10. Afiseaza divizorii nr. x si calculati suma si numarul lor, si aflati daca x este nr prim sau nu.

#include<iostream.h> int x,i,nr,s; void main() { cin>>x; for(i=1; i<=x; i++) if(x%i==0) {cout<<i<< ; nr=nr+1; s=s+i;} cout<<nr=<<nr<<s; if(nr==2) cout<<x<< este prim; else cout<<x<<este neprim; }
11. Sa se calculeze suma divizorilor pozitivi ai nr. x.

#include<iostream.h> int x,i,s; void main() {cin>>x; for(i=1; i<=x; i++) if(x%i==0) s=s+i; cout<<suma divizorilor pozitivi=<<s; }
12. Sa se calculeze produsul p=1*2*3*....*n.

#include<iostream.h> int n,x,p; void main() {cin>>n; p=1; for(x=1;x<=n; x++) p=p*x; cout<<p=<<p; }

13. Sa se calculeze suma cifrelor nr. x.

#include<iostream.h> #include<conio.h> int c,x,s; void main() {clrscr; cout<<x=; cin>>x; s=0; while(x!=0) {c=x%10; x=s+c; x=x/10; } cout<<s=<<s; getch(); }
14. Sa se calculeze cate cifre are nr. x.

#include<iostream.h> #include<conio.h> int x,c,nr; void main() {clrscr; cout<<x=; cin>>x; nr=0; while(x!=0) {c=x%10;

nr=nr+1; x=x/10; } cout<<nr=<<nr; getch(); }


15. Sa se afiseze cea mai mare cifra a nr. x.

#include<iostream.h> #include<conio.h> int x,max,c; void main() { cin>>x; max=x%10; while(x!=0) {x=x/10; c=x%10; if(max<c) max=c; } cout<<max=<<max; }
17. Sa se afiseze toate nr. prime de la 1 la x.

#include<iostream.h> #include<conio.h> int x,i,nr,n; {cin>>n; for(x=1;x<=n; x++) {nr=0; for (i=1;i<=x; i++) if(x%i=0) {cout<<i<< ; nr=nr+1; } if (nr==2) cout<<x<<nr prim<<endl; else cout<<x<<nr neprim<<endl; } }

18. Sa se afiseze cel mai mic si cel mai mare divizor propriu al lui x.

#include<iostream.h> #include<conio.h> int x, nr=0, i, max, min; void main() {clrscr; cin>>x; for (i=2; i<=x/2; i++) if (x%i==0) {nr=nr+1; if (nr==1) {min=i; max=i; } else { if(max<I) max=i; if (min>i) min=i; } } cout<<div min=<<min<<endl; cout<,div max=<<max<<endl; }

You might also like