Pages

Monday, March 31, 2025

Function Overloading in Functions in C++

 int sum(int a , int b) {

    cout << (a+b) << endl;
    return a + b;
}

double sum(double a, double b) {
    cout << (a+b) << endl;
    return a + b;
}


int sum(int a, int b, int c){
    cout << (a+b+c) << endl;
    return a + b+ c;
}
int main(){

    sum(2,3);
    sum(12, 13);
    sum(1.5, 2.5); //  4
    sum(1,2,3);

    return 0;
}

OUTPUT-
5 25 4 6

Binomial Coefficient (Factorial) in Functions in C++

 


int factorial(int n) {
    int fact = 1;
    for(int i =1; i<=n; i++){
        fact *= i;

    }
    return fact;
}

int binCoeff(int n, int r) {
    int val1 = factorial(n);
    int val2 = factorial(r);
    int val3 = factorial(n-r);

    int result = val1/(val2*val3);
    return result;
}

int main(){

    cout << binCoeff(4, 2) << endl;
   

    return 0;
}

Check if Prime or not in Functions in C++

 bool isPrime(int n) {

    if( n==1 ) {
        return false;
    }
    for(int i =2; i<= n-1; i++){
        if(n%i == 0) { // non-prime
            return false;
           
        }
    }

    return true;
}


bool isPrime2(int n) {
    if(n == 1){
        return false;
    }
    for(int i = 2; i*i <= n; i++){
        if(n % i == 0) {
            return false;
        }
    }
    return true;
}
int main(){
    cout << isPrime2(4) << endl;

    return 0;
}

Scope (local vs global) functions in C++

 #include <iostream>

using namespace std;


int num = 25;
void sum(int a, int b) {
    cout << num << endl;
    int s = a + b;
    cout << s << endl;
}


int main(){
    sum(5, 4);
    int s = 10;

    cout << num << endl;

    return 0;
}

Factorial in Functions in C++

 #include <iostream>

using namespace std;

// int prod(int a, int b) {
//     return a * b;
// }

// // even -> true; odd -> false
// bool isEven(int n) {
//     if(n % 2 == 0){
//         return true;
//     } else {
//         return false;
//     }
// }



int factorial(int n){
    int fact = 1;
    for(int i = 1; i <= n; i++) {
        fact = fact * i;

    }
    cout << "factorial(" << n << ") = " << fact << endl;
    return fact;
}


int main(){
    factorial(0);
    factorial(1);
    factorial(2);
    factorial(3);
    factorial(4);
    factorial(5);

    return 0;
}

OUTPUT-
factorial(0) = 1 factorial(1) = 1 factorial(2) = 2 factorial(3) = 6 factorial(4) = 24 factorial(5) = 120

Check odd or even in functions in C+++

 CASE 1- Odd Number


#include <iostream>

using namespace std;


int prod(int a, int b) {
    return a * b;
}

// even -> true; odd -> false
bool isEven(int n) {
    if(n % 2 == 0){
        return true;
    } else {
        return false;
    }
}

int main(){
     cout << isEven(19) << endl;

    return 0;
}

OUTPUT-

0
CASE 2- Even Number

Output- > 1

Multiplication in Functions in C++

 #include <iostream>

using namespace std;


int prod(int a, int b) {
    return a * b;
}
int main(){
     cout << prod(10, 20) << endl;

    return 0;
}


Output-
200

Syntax with parameters in c++

 #include <iostream>

using namespace std;

int sum(int a, int b) {  // a, b are parameters
    int sum = a + b;
    return sum;
}

int main() {
    int s = sum(2, 4); // 2, 4 are arguments
    cout << "sum = " << s << endl;

    return 0;
}

Getting started with Functions in C++

 #include <iostream>

using namespace std;

void sayHello() {
    cout << "Hello :)\n";
}

int main() {
    sayHello(); // function call
    return 0;
}



Hmmm interesting....


#include <iostream>
using namespace std;

void sayHello() {
    cout << "Hello :) \n";
}

void assistant() {
    sayHello();
    cout << "work done \n";
}


int main() {
    assistant();
    return 0;
}

Palindromic Numbers problem in C++ but unique (MyLearnings!!)

 #include <iostream>

using namespace std;

/*
MyLearnings:-
1. I lacked visualization for spacing, and should've started from backward to forward which is correct way to do this code
*/
int main() {
    int n = 5;

    // outer loop
    for(int i = 1; i <= n; i++){
        // inner loop

        for(int space = 1; space <= n-i; space++){
            cout << " ";
        }
        // nums backward
        for(int j = i; j >= 1; j--){
            cout << j;
        }
        // nums forward
        for(int j = 2; j <= i; j++){
            cout << j;
        }
        cout << endl;
    }




    return 0;

}

OUTPUT-
1 212 32123 4321234 543212345

Print the 0-1 Triangle Pattern in C++

 Print#include <iostream>

using namespace std;
int main() {
    // outer loop (rows) -> 1 to n
    for(int i = 1; i <= 5; i++){
        // numbers
        for(int j = i; j >= 1; j-- ){
            cout << j%2 << " ";
        }
        cout << endl;
    }
   
    return 0;
}


OUTPUT-
1 0 1 1 0 1 0 1 0 1 1 0 1 0 1

Butterfly Pattern in C++ (Learn this MISTAKE to BOOST up!!)

 #include <iostream>

using namespace std;
// quote- break down bigger problems into smaller problems
// Butterfly Pattern -


/*
pseudocode
1. build first pyramid
then second pyramid
*/
int main() {
    int n = 4;

    // upper half
    for(int i=1; i <= n; i++){
        //stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }   //spaces
            for(int j =1; j <= 2*(n-i); j++){
                cout << " ";
            }   //stars
            for(int j = 1; j <= i; j++){
                cout << "*";
            }
            cout << endl;
    }

    // lower half , Mylearnings- just change outer loop to make it opposite pattern
    for(int i = n; i >= 1; i--){ // outer loop(n to 1) so
// int i = n; i >= 1; i--
        // stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }
        // spaces
        for(int j = 1; j <= 2*(n-i); j++ ){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }
        cout << endl;
    }
    return 0;

}

OUTPUT-
* * ** ** *** *** ******** ******** *** *** ** ** * *

Sunday, March 30, 2025

Diamond Pattern problem in C++

 #include <iostream>

using namespace std;
// quote- break down bigger problems into smaller problems
// Diamond pattern -


/*
pseudocode
1. build first pyramid
then second pyramid
*/
int main() {
    int n = 4;

    // first pyramid
    for(int i = 1; i <= n; i++){
        // spaces
        for(int j = 1; j <= n-i; j++){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= 2*i-1; j++){
            cout << "*";
        }
        cout << endl;
       
    }
    // second pyramid
    for(int i = n; i >= 1; i--){

        // spaces
        for(int j = 1; j <= n-i; j++){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= 2*i-1; j++){
            cout << "*";
        }
       

        cout << endl;

    }

    return 0;

}

OUTPUT-
* *** ***** ******* ******* ***** ***
*

Floyd's Pattern problem in C++

 #include <iostream>

using namespace std;

// floyd's triangle pattern problem
int main() {
    int n = 5;
    int num = 1;

    // outer loop - rows
    for(int i= 1; i<= n; i++){
        // inner loop - each rows
        for(int j =1; j <= i; j++){
            cout << num++ << " ";
        }
        cout << endl;
    }

    return 0;

}

OUTPUT-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Inverted & Rotated Half-pyramid pattern problem in C++

#include <iostream>
using namespace std;

// code to print inverted & rotated half-pyramid pattern
int main() {
    int n = 4;
    for(int i = 1; i <= n; i++){

        // spaces
        for(int j = 1; j <= n-i; j++){
            cout << " ";
        } // stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }

        cout << endl;        
    }

    return 0;

}

OUTPUT- * ** *** ****

Saturday, March 29, 2025

Inverted Star Patterns problem in C++

 #include <iostream>

#include <cmath>
using namespace std;
// inverted star pattern problem in C++

int main() {
    int n = 4;
   
    //outer loop
    for(int i=1; i <= n; i++){
        // inner loop
        for(int j = 1; j <= n-i+1; j++){
            // work
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

OUTPUT-
* * * * * * * * * *

patterns - lecture 1 (one mistake to avoid!!)

 #include <iostream>

#include <cmath>
using namespace std;

int main() {
    int n = 4;
    int i;
    // outer loop
    for(i = 1; i <= n; i++){ // mistake int i was present, so 5 was not printing
due to this reason
        int val = i;
        // inner loop
        for(int j = 1; j <= n; j++){
            //work
            cout << val << " ";
        }
        cout << endl;
    }
    cout << i << endl;

    return 0;
}


OUTPUT:-
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5

Friday, March 28, 2025

code to check Armstrong numbers in C++

 #include <iostream>

#include <cmath>

using namespace std;

int main() {
    int n = 153;  // Change this number to test other values
    int num = n;
    int cubeSum = 0;

    while (num > 0) {
        int lastDig = num % 10; // Gets the last digit
        cubeSum += lastDig * lastDig * lastDig; // Adds the cube of the digit
        num /= 10;  // Removes the last digit
    }

    if (n == cubeSum) {
        cout << n << " is an Armstrong number." << endl;
    } else {
        cout << n << " is not an Armstrong number." << endl;
    }

    return 0;
}

code to print multiplication table of a number entered by an user in C++

#include <iostream>
#include <cmath>

// code to print multiplication table of a number entered by an user.
using namespace std;

int main(){
    int num;

    cout << "enter a num : ";
    cin >> num;

    for(int i = 1; i <= 10; i++){

        int multiply = num*i;

        cout << num << " " << "x " << i << " is " << multiply << endl;
    }
    return 0;

}

Find factorial of a number entered by the user in C++

 #include <iostream>

#include <cmath>
using namespace std;
/*
pseudocode:
1. user enters the value
2. the factorial-> 5! - 1 x 2 x 3 x 4 x 5
so, it is the addition of that number + multiplied after, until value entered reached.
*/

// Find factorial of a number entered by the user.
int main(){
    int n = 6;
    int fact = 1;

    for(int i = 1; i <= n; i++){
        fact *= i;
    }

    return 0;

}

Thursday, March 27, 2025

Check if a Number is Prime(OPTIMIZED)

#include <iostream>
#include <cmath>
using namespace std;
// check if a number is prime or not (OPTIMIZED)
int main(){
    int n = 13;
    bool isPrime = true;

    for (int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0) { // i is a factor of n; i completely divides n; n is non-prime
            isPrime = false;
            break;
        }
    }

    if(isPrime){
        cout << "It is a Prime Number " << endl;

    } else {
        cout << "It is not a Prime number " << endl;
    }
   
   

    return 0;

}

check prime no. in C++

 #include <iostream>

using namespace std;
int main(){
    int n = 7;
    bool isPrime = true; // assumes all numbers are prime

    for(int i = 2; i <= n-1; i++){
        if(n % i == 0){ // i is a factor of n; i completely divides n; n is non-prime
            isPrime = false;
            break;
        }
    }
   
    if(isPrime){ // or if(isPrime == true)
        cout << "number is prime " << endl;
    } else{
        cout << "number is not prime" << endl;
    }
   

    return 0;

}

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...