Write a print to print the following
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Answers
Refer to this attachment
Required Answer:-
Correct Question:
Write a program to print the following pattern.
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Solution:
Here comes the program.
1. In Java
public class Pattern {
public static void main(String[] args) {
for(int i=5;i>=1;i--) {
for(int j=5;j>=i;j--)
System.out.print(j+" ");
System.out.println();
}
}
}
2. In Python
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print(j,end=" ")
print()
3. In C
#include <stdio.h>
int main() {
for (int i = 5; i >= 1;i--) {
for (int j = 5; j >= i;j--) {
printf("%d ",j);
}
printf("\n");
}
return 0;
}
4. In C++
#include <iostream>
using namespace std;
int main()
{
for(int i = 5; i >= 1; i--)
{
for(int j = 5; j >= i;j--)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Algorithm:
- Iterate outer loop from i = 5 to 1.
- Iterate inner loop from j = 5 to i and display values of j.
- Display a new line after printing each row.
Refer to the attachment.