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

Spring Boot With MySQL Database connection with Examples | MySQl Database Configuration with Spring Boot Projects

 ðŸ˜ƒ MySQL Database Configuration with Spring Boot Projects  In this article, we are going to introduce How to connect MySQL Database with the Spring Boot project. pom.xml   org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true mysql mysql-connector-java runtime org.projectlombok lombok true    application. properties   server.port=8025 spring.datasource.url=jdbc:mysql://localhost:3306/princedb spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=create-drop MySqlWithSpringBootApplication.java   package com.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MySqlWithSpringBootApp...

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...

How can we create Auto generated field or ID for mongodb using spring boot

😂 How can we create an Auto-generated field or ID for MongoDB using spring boot? First Create One Application Like Mongodb_sequence-id-generator Pom.XML org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-mongodb de.flapdoodle.embed de.flapdoodle.embed.mongo User.java package com.app; import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Transient; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "users_db") public class User { @Transient public static final String SEQUENCE_NAME = "users_sequence"; @Id private long id; private String firstName; private String lastName; private String email; public User() { } public User(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = la...