Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Task 1:
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- using namespace std;
- void findLargeNumber (int a, int b)
- {
- if (a < b)
- {
- cout << b << " is greater than " << a << endl;
- }
- else
- {
- cout << a << " is greater than " << b << endl;
- }
- }
- void add (int a, int b)
- {
- // finding larger number
- findLargeNumber (a, b);
- cout << a << " + " << b << " = " << a+b << endl;
- }
- void sub (int a, int b)
- {
- // finding larger number
- findLargeNumber (a, b);
- int num1, num2;
- if (a < b)
- {
- num1 = b;
- num2 = a;
- }
- else
- {
- num1 = a;
- num2 = b;
- }
- cout << num1 << " - " << num2 << " = " << num1-num2 << endl;
- }
- void multiplcate (int a, int b)
- {
- // finding larger number
- findLargeNumber (a, b);
- cout << a << " * " << b << " = " << a*b << endl;
- }
- void divide(int a, int b)
- {
- // finding larger number
- findLargeNumber (a, b);
- int num1, num2;
- if (a < b)
- {
- num1 = b;
- num2 = a;
- }
- else
- {
- num1 = a;
- num2 = b;
- }
- cout << num1 << " / " << num2 << " = " << num1/num2 << " , " << num1 << " % " << num2 << " = " << num1 % num2 << endl;
- }
- int main ()
- {
- int a, b;
- cout << "Enter first number: ";
- cin >> a;
- cout << "Enter second number: ";
- cin >> b;
- add (a, b);
- sub (a, b);
- multiplcate (a, b);
- divide(a, b);
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Task 2
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- using namespace std;
- int n = 1;
- int sum = 0;
- void sumofN(){
- if(n==51){
- cout << "Sum of first 50 natural number is: " << sum << endl;
- return;
- }else{
- sum+=n++;
- sumofN();
- }
- }
- int main()
- {
- sumofN();
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Task: 3
- /////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- using namespace std;
- int fact(int n) {
- if (n == 0 || n == 1)
- return 1;
- else
- return n * fact(n - 1);
- }
- int main() {
- int n, r, comb, per;
- cout<<"Enter n : ";
- cin>>n;
- cout<<"\nEnter r : ";
- cin>>r;
- comb = fact(n) / (fact(r) * fact(n-r));
- cout << "\nCombination : " << comb;
- per = fact(n) / fact(n-r);
- cout << "\nPermutation : " << per;
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Task: 4
- /////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- using namespace std;
- static int n = 1; // static variable
- int sum = 0; // global variable
- void sumofN(){
- if(n==51){
- cout << "Sum of first 50 natural number is: " << sum << endl;
- string str = "I am a local variable";
- cout << "Hi, " << str << endl;
- return;
- }else{
- sum+=n++;
- sumofN();
- }
- }
- int main()
- {
- sumofN();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement