Computer Science, asked by kamalrajatjoshi94, 2 months ago

Challenge:-Write a program to print the pattern in java.Kindly attach the program and the attachment.Dont copy from websites.
a a a a a
b b b b b
A A A A A
B B B B B
Hint:ASCII value of a is 97 and A is 65.​

Answers

Answered by anindyaadhikari13
4

Solution:

The given cøde is written in Java.

public class Main {

 public static void main(String[] args) {

     char x='a';

     int i,j;

     for(i=1;i<=4;i++){

       for(j=1;j<=5;j++)

          System.out.print(x+" ");

       x++;

       if(x=='c')

         x='A';

       System.out.println();

     }

 }

}

Logic:

  • Initially, x = 'a'. Then, inner loop displays 'a' five times. Now, value of x is incremented by 1. x becomes 'b'. Again, 'b' is displayed 5 times. Now, 'b' becomes 'c' as x is incremented by 1. As soon as x becomes 'c', the condition x=='c' becomes true. So, x becomes 'A'. Now, 'A' and 'B' are displayed on the screen.

See the attachment for output.

Attachments:
Similar questions