0% found this document useful (0 votes)
18 views2 pages

5

The document contains C++ code to: 1) Write functions to calculate the sum and factorial of a positive integer entered by the user. 2) Write functions to convert a binary number entered by the user to its decimal and octal equivalents. 3) The sum, factorial, and binary to octal conversion functions use recursion by calling themselves with decreasing arguments to calculate the final values.

Uploaded by

Neha Raza
Copyright
© © All Rights Reserved
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)
18 views2 pages

5

The document contains C++ code to: 1) Write functions to calculate the sum and factorial of a positive integer entered by the user. 2) Write functions to convert a binary number entered by the user to its decimal and octal equivalents. 3) The sum, factorial, and binary to octal conversion functions use recursion by calling themselves with decreasing arguments to calculate the final values.

Uploaded by

Neha Raza
Copyright
© © All Rights Reserved
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/ 2

1. #include <stdio.h> 1.

#include<iostream>
2. int addNumbers(int n) 2. using namespace std;
3. int main() { 3. int add(int n);
4. int num; 4. int main() {
5. printf("Enter a positive integer: "); 5. int n;
6. scanf("%d", &num); 6. cout << "Enter a positive integer: ";
7. printf("Sum = %d", addNumbers(num)); 7. cin >> n;
8. return 0; 8. cout << "Sum = " << add(n);
} 9. return 0;
9. int addNumbers(int n) { }
10. if (n != 0) 10. int add(int n) {
11. return n + addNumbers(n - 1); 11. if(n != 0)
12. else 12. return n + add(n - 1);
13. return n; 13. else
} 14. return n;
}

1. #include<stdio.h> 1. #include<iostream>
2. long int multiplyNumbers(int n); 2. using namespace std;
3. int main() { 3. int factorial(int n);
4. int n; 4. int main() {
5. printf("Enter a positive integer: "); 5. int n;
6. scanf("%d",&n); 6. cout << "Enter a positive integer: ";
7. printf("Factorial of %d = %ld", n, 7. cin >> n;
multiplyNumbers(n)); 8. cout << "Factorial of " << n << " = " <<
8. return 0; factorial(n);
} 9. return 0;
9. long int multiplyNumbers(int n) { }
10. if (n>=1) 10. int factorial(int n) {
11. return n*multiplyNumbers(n-1); 11. if(n > 1)
12. else 12. return n * factorial(n - 1);
13. return 1; 13. else
} 14. return 1;
}

1. #include <math.h> 1. #include <iostream>


2. #include <stdio.h> 2. #include <cmath>
3. int convert(long long bin); 3. using namespace std;
4. int main() { 4. int convertBinarytoOctal(long long);
5. long long bin; 5. int main()
6. printf("Enter a binary number: "); {
7. scanf("%lld", &bin); 6. long long binaryNumber;
8. printf("%lld in binary = %d in octal", bin, 7. cout << "Enter a binary number: ";
convert(bin)); 8. cin >> binaryNumber;
9. return 0; 9. cout << binaryNumber << " in binary = " <<
} convertBinarytoOctal(binaryNumber) << " in
10. int convert(long long bin) { octal ";
11. int oct = 0, dec = 0, i = 0; 10. return 0;
12. while (bin != 0) { }
13. dec += (bin % 10) * pow(2, i); 11. int convertBinarytoOctal(long long
14. ++i; binaryNumber)
15. bin /= 10; {
} 12. int octalNumber = 0, decimalNumber = 0, i =
16. i = 1; 0;
17. while (dec != 0) { 13. while(binaryNumber != 0)
18. oct += (dec % 8) * i; {
19. dec /= 8; 14. decimalNumber += (binaryNumber%10) *
20. i *= 10; pow(2,i);
} 15. ++i;
21. return oct; 16. binaryNumber/=10;
} }
17. i = 1;
18. while (decimalNumber != 0)
{
19. octalNumber += (decimalNumber % 8) * i;
20. decimalNumber /= 8;
21. i *= 10;
}
22. return octalNumber;
}

You might also like