राइट ए प्रोग्राम टो डिस्पले दफा टर्न ऑफ 1223 334 444 55555
Answers
Answered by
2
Required Answer:-
Correct Question:
Write a program to display the pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Solution:
1. In Java.
public class Pattern {
public static void main(String[] args) {
int i, j;
for(i=1;i<=5;i++) {
for(j=1;j<=i;j++)
System.out.print(i);
System.out.println();
}
}
}
2. In Python.
for i in range(1,6):
for j in range(1,i+1):
print(i,end=" ")
print()
3. In C
#include <stdio.h>
int main() {
int i, j;
for (i = 1;i<=5;i++) {
for (j = 1;j<=i;j++) {
printf("%d",i);
}
printf("\n");
}
return 0;
}
4. In C++
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i = 1; i <= 5; i++)
{
for(j = 1; j <= i; j++)
{
cout << i;
}
cout << "\n";
}
return 0;
}
Explanation:
- Patterns are solved using nested loops. Two loops are created here. Outer loop iterates for i in range 1 - 5. Inner loop iterates for j in range 1 to i. Inside the inner loop, value of 'i' is displayed. A new line is displayed after completing iteration of inner loop.
Attachments:
Similar questions