write a program to print
@@@@1
@@@2@
@@3@@
@4@@@
5@@@@
Plz give me the answer fast
Answers
As no language is mentioned, I am writing in Java.
Here comes the code.
public class Brainly{
public static void main(String s[]){
int i,j,n=5;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i+j==n+1)
System.out.print(i+" ");
else
System.out.print("@ ");
}
System.out.println();
}
}
}
- Line 1: Class declaration.
- Line 2: Start of main() method.
- Line 3: Declared 3 variables, to be used while printing the pattern.
- Line 4: Start of outer loop.
- Line 5: Start of inner loop.
- Line 6: A condition which checks whether numbers should be printed or not.
- Line 7: If condition in line 6 is true, number is printed.
- Line 8-9: If condition is false, '@' symbol is printed.
- Line 10: End of inner loop.
- Line 11: A new line is inserted so as to move to the next row.
- Line 12: End of outer loop.
- Line 13: End of main() method.
- Line 14: End of class.
See attachment for output.
Answer:
Program:-
public class Main
{
public static void main(String args[])
{
int p=5,c=1;
System.out.println("The pattern:");
for(int i=1;i<=5;i++)
{
for(int j=1;j<=5;j++)
{
if(j==p)
System.out.print(c+" ");
else
System.out.print("@"+" ");
}
System.out.println();
p--;
c++;
}
}
}
Logic:-
- Simple first make a program that prints five rows and five columns
- Then make a condition that if iteration number is 5 then it prints 1 followed by 4th iteration 2
- If condition is false it prints "@".