Skip to main content

Pattern Program in interview || How to do pattern programming very fast






Pattern Programming in java

=======================================================================

 It is one of the pattern Printing program so, many interviewer are asking these problem in java interview      

   Can you print this Pattern ===========================

1              

2  6               This the gap of number -4

3  7  10                                                   -4,3  

4  8  11  13                                              -4,3,2

5  9  12  14  15                                         -4,3,2,1

------------------------------------------------  

You have to print Like that pattern 

  x=1,2,3,4,5 y=x=1,2,3,4,5

Coding program of this Above program

=============================

  class Pattern{
   public static void main(String args[]){
   //int a=0;
   for(int i=1;i<=5;i++){
          for(int j=i,a=4,p=i;j>=1;j--,p=p+a,a--){
            System.out.print(p+" ");
             
  }
  System.out.println();
   }
    
   }
}

=================================================================
* Java Interviewer can so  many types of question in java.

Programs to print triangles using *, numbers and characters

Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i, j, rows;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=1; i<=rows; ++i) {
  7. for (j=1; j<=i; ++j)
  8. { printf("* "); }
  9. printf("\n");
  10. }
  11. return 0;
  12. }

Program to print half pyramid a using numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i,j,rows;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=1; i<=rows; ++i) {
  7. for (j=1; j<=i; ++j)
  8. { printf("%d ",j); }
  9. printf("\n");
  10. }
  11. return 0;
  12. }

Program to print half pyramid using alphabets

A
B B
C C C
D D D D
E E E E E
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i, j;
  4. char input, alphabet='A';
  5. printf("Enter the uppercase character you want to print in last row: ");
  6. scanf("%c", &input);
  7. for (i=1; i<=(input-'A'+1); ++i) {
  8. for (j=1; j<=i; ++j)
  9. { printf("%c", alphabet); }
  10. ++alphabet;
  11. printf("\n");
  12. }
  13. return 0;
  14. }

Programs to print inverted half pyramid using * and numbers

Inverted half pyramid using *

* * * * *
* * * *
* * * 
* *
*
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i, j, rows;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=rows; i>=1; --i) {
  7. for (j=1; j<=i; ++j)
  8. { printf("* "); }
  9. printf("\n");
  10. }
  11. return 0;
  12. }

Inverted half pyramid using numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i ,j, rows;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=rows; i>=1; --i) {
  7. for (j=1; j<=i; ++j)
  8. { printf("%d ",j); }
  9. printf("\n");
  10. }
  11. return 0;
  12. }

Programs to display pyramid and inverted pyramid using * and digits

Program to print full pyramid using *

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i, space, rows, k=0;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=1; i<=rows; ++i,k=0) {
  7. for (space=1; space<=rows-i; ++space)
  8. { printf(" "); }
  9. while (k!=2*i-1) {
  10. printf("* ");
  11. ++k;
  12. }
  13. printf("\n");
  14. }
  15. return 0;
  16. }

Program to print pyramid using numbers

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int i, space, rows, k=0, count=0, count1=0;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=1; i<=rows; ++i) {
  7. for (space=1; space<=rows-i; ++space) {
  8. printf(" ");
  9. ++count;
  10. }
  11. while (k!=2*i-1) {
  12. if (count <= rows-1)
  13. { printf("%d ", i+k);
  14. ++count;
  15. }
  16. else {
  17. ++count1;
  18. printf("%d ", (i+k-2*count1));
  19. }
  20. ++k;
  21. }
  22. count1=count=k=0;
  23. printf("\n");
  24. }
  25. return 0;
  26. }

Inverted full pyramid using *

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int rows, i, j, space;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=rows; i>=1; --i) {
  7. for (space=0; space<rows-i; ++space)
  8. printf(" ");
  9. for (j=i; j<=2*i-1; ++j)
  10. printf("* ");
  11. for (j=0; j<i-1; ++j)
  12. printf("* ");
  13. printf("\n");
  14. }
  15. return 0;
  16. }

Print Pascal's triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1
Source Code
  1. #include<stdio.h>
  2. int main() {
  3. int rows, coef=1, space, i, j;
  4. printf("Enter number of rows: ");
  5. scanf("%d", &rows);
  6. for (i=0; i<rows; i++) {
  7. for (space=1; space <= rows-i; space++)
  8. printf(" ");
  9. for (j=0; j<=i; j++) {
  10. if (j==0 || i==0)
  11. coef = 1;
  12. else
  13. coef=coef*(i-j+1)/j;
  14. printf("%4d", coef);
  15. }
  16. printf("\n");
  17. }
  18. return 0;
  19. }

Print Floyd's Triangle.

1
2 3
4 5 6




Comments

Popular posts from this blog

Generate Documentation for Spring Boot API with swagger and Open API 3

 ðŸ˜‚ How to Generate Documentation for Spring Boot API project Make sure your Project having a swagger or open API 3 Dependency then only you get proper Documentation for your Service. ---------------------------------------------------------------------------- Every method or API above you have to Write Like that then only you get proper Documentation If you do everything in your Application and Run your Application and go to browse and search. . http://localhost:8080/v3/api-docs/ if you want to learn more go with this one.  Open API-3   1. For Return List of Object then you have to use this API operation  @Operation(summary = "This will fetch List of Patient Detailas base on Hospital Name") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Successfully fetch All Patient Details from Database ", content = {  @Content(mediaType = "application/json" ,array = @ArraySchema(schema =@Schema(implementation =PatientDe...

Top 10 website for IT Job | Software developer job apply | Job platfrom to apply Job | Software Developer Job .

                        Top 10 website for IT Job If you want a job in an IT or software company then this blog for you. I will give you the top 10 websites where you can apply the job for the software industry. It is a very good website to apply for a job for a software company.

#FibonacciNumberSeries Write a program of Fibonacci Number Series|| Fib...

Fibonacci Number Series ============================================ 0 1 1 2 3 5 8 13............. a=0; b=1; i=2; printf(a,b); while(i<=n){ c=a+b; printf(c); a=b; b=c; i++; } =========================================================================== import java.util.Scanner; class Fibonacci { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a=0,b=1,c; System.out.print(a+" "+b); for(int i=2;i