Advertisement
Dani_info

Recursive

Jan 20th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. // Functii recursive.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. using namespace std;
  11.  
  12. int max(int, int);
  13. int min(int, int);
  14. int cmmdc(int, int);
  15. int cmmdc2(int, int);
  16. int sumaCifrelor(int);
  17. int compNumar(int);
  18. int invers(int, int);
  19. int isPrime(int, int);
  20. void divizori(int, int);
  21. void factor(int, int, int);
  22.  
  23. int main()
  24. {  
  25.     factor(18, 2, 0);
  26.     _getch();
  27.     return 0;
  28. }
  29.  
  30. int max(int a, int b) {
  31.     if (a > b) return a;
  32.     else
  33.         return max(b, a);
  34. }
  35.  
  36. int min(int a, int b) {
  37.     if (a < b) return a;
  38.     else
  39.         return min(b, a);
  40. }
  41.  
  42. int cmmdc(int a, int b) {
  43.     if (a == b || a == 0)
  44.         return b;
  45.     else
  46.         if (b == 0)
  47.             return a;
  48.         else
  49.             if (a > b)
  50.                 return cmmdc(a - b, b);
  51.             else
  52.                 return cmmdc(a, b - a);
  53. }
  54.  
  55. int cmmdc2(int a, int b) {
  56.     if (a == 0)
  57.         return b;
  58.     if (b == 0)
  59.         return a;
  60.     else
  61.         return cmmdc(b, a%b);
  62. }
  63.  
  64. int sumaCifrelor(int n){
  65.     if (n == 0)
  66.         return 0;
  67.     else
  68.         return n % 10 + sumaCifrelor(n / 10);
  69. }
  70.  
  71.  
  72. int compNumar(int nr) {
  73.     int cif;
  74.     cin >> cif;
  75.     if (cif<0 || cif>9) return nr;
  76.     else
  77.           return compNumar(nr * 10 + cif);
  78. }
  79.  
  80. int invers(int n, int p) {
  81.     if (n < 10)  return n+p;
  82.     else
  83.         return invers(n / 10, 10 * (p + n % 10));
  84. }
  85.  
  86. int isPrime(int nr, int i) {
  87.     if (i == 1) return 1;
  88.     else
  89.         return (nr%i != 0) && (isPrime(nr, i - 1));
  90. }
  91.  
  92. void divizori(int n, int d) {
  93.     if (d <= n / 2) {
  94.         if (n%d == 0) cout << d << " ";
  95.         divizori(n, d + 1);
  96.     }
  97. }
  98.  
  99. void factor(int n, int f, int p) {
  100.     if (n > 1)
  101.         if (n%f == 0) factor(n / f, f, p + 1);
  102.         else{
  103.             if (p!=0)
  104.                 cout << f << " la puterea " << p << endl;
  105.                 factor(n, f + 1, 0);
  106.             }  
  107.     else if (p!=0) cout << f << " la puterea " << p << endl;
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement