Write a program to print the following pattern.
Example:
1 1 1 1 12 2 2 2 23 3 3 3 34 4 4 4 45 5 5 5 5
Approach:
Java
public class SquareNumberPrint {public static void main(String[] args) {int n = 5;int m=5;for (int i = 1; i <=n; i++) {for (int j = 0; j <=m; j++) {System.out.print(i+" ");}System.out.println();}}}
C++
#include <bits/stdc++.h>using namespace std;int main(){int n=5,m=5;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)cout<<i<<" ";cout<<"\n";}return 0;}
No comments:
Post a Comment