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

remove duplicates from sorted array - two pointer approach (leetcode)

  class Solution {     public int removeDuplicates ( int [] nums ) {         // base case: return if array have no el.         if ( nums...