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

How to Send OTP in Mobile Number | login with OTP mobile Number | How to send OTP in mobile no with Spring Boot APP

   ðŸ˜‚               Login with Mobile Number OTP ---------------------------------------------------------------------------- If you want to develop a project to log in with OTP mobile Number in Spring Boot Applications then this post for you. In this post, I am going to use some other service to send the OTP in mobile number. we have to use it in this project spring boot. we are going to use Twilio to send SMS. we are going to use a web socket to send the data from the browser to the SMS gateway. Oracle Database for store the user details. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>com.twilio.sdk</grou

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   <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</gro

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 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>de.flapdoodle.embed</groupId> <artifactId>de.flapdoodle.embed.mongo</artifactId> </dependency> 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 = &