Pages

Saturday, March 29, 2025

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

No comments:

Post a Comment

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...