Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: c interview questions on pattern printing

  1. #1
    Senior Member Bandhavi's Avatar
    Join Date
    May 2012
    Posts
    169,199

    c interview questions on pattern printing

    This is also one of the interview question

    Program to print the below triangle??

    Code:
    A
    B B
    C C C
    D D D D
    E E E E E
    Last edited by Bandhavi; 07-25-2012 at 09:06 AM.

  2. #2
    Senior Member Bandhavi's Avatar
    Join Date
    May 2012
    Posts
    169,199

    c program to print Pascal triangle

    Code:
       1
      1 1
     1 2 1
    1 3 3 1
    How to print the pascal triangle in c??
    This is also one of the interview question...

    Can anyone in the group please share your logic's to implement these patterns.

  3. #3
    Senior Member Bandhavi's Avatar
    Join Date
    May 2012
    Posts
    169,199
    Last edited by Bandhavi; 07-25-2012 at 12:32 PM.

  4. #4
    Senior Member Tarah's Avatar
    Join Date
    Jun 2012
    Posts
    300
    Quote Originally Posted by Bandhavi View Post
    This is also one of the interview question

    Program to print the below triangle??

    Code:
    A
    B B
    C C C
    D D D D
    E E E E E
    This code will help you

    Code:
    int main () 
    {
    
    int Ind, Num, Ind1;
    char ch = 'A';
    
    
    printf("Enter number of rows\n");
    scanf("%d",&Num);
    
    for ( Ind = 1 ; Ind <= Num ; Ind++ )
    {
        for ( Ind1 = 1 ; Ind1 <= Ind ; Ind1++)
            printf("%c ", ch);
    
        printf("\n");
        ch++;
    }
    return 0;
    }
    the output is

    Code:
    Enter number of rows
    12
    A
    B B
    C C C
    D D D D
    E E E E E
    F F F F F F
    G G G G G G G
    H H H H H H H H
    I I I I I I I I I
    J J J J J J J J J J
    K K K K K K K K K K K
    L L L L L L L L L L L L

  5. #5
    Senior Member Tarah's Avatar
    Join Date
    Jun 2012
    Posts
    300
    Quote Originally Posted by Bandhavi View Post
    Code:
       1
      1 1
     1 2 1
    1 3 3 1
    How to print the pascal triangle in c??
    This is also one of the interview question...

    Can anyone in the group please share your logic's to implement these patterns.
    Try the below code
    Code:
    #include <stdio.h>
    
    long factorial(int);
    int main ()
    {
    
       int i, n, c;
    
       printf("Enter the number of rows you wish to see in pascal triangle\n");
       scanf("%d",&n);
    
       for ( i = 0 ; i < n ; i++ )
       {
          for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
             printf(" ");
    
          for( c = 0 ; c <= i ; c++ )
             printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
    
          printf("\n");
       }
    return 0;
    }
    Factorial function

    Code:
    long factorial(int n)
    {
       int c;
       long result = 1;
    
       for( c = 1 ; c <= n ; c++ )
             result = result*c;
    
       return ( result );
    }


    this is will be the output

    Code:
    Enter the number of rows you wish to see in pascal triangle
    6
         1
        1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1
    1 5 10 10 5 1

  6. #6
    Senior Member Bandhavi's Avatar
    Join Date
    May 2012
    Posts
    169,199

    reverse peramid pattern with alphabets

    Code:
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
     A B C D E F G H I J K L M N O P Q R S T U V W X Y
      A B C D E F G H I J K L M N O P Q R S T U V W X
       A B C D E F G H I J K L M N O P Q R S T U V W
        A B C D E F G H I J K L M N O P Q R S T U V
         A B C D E F G H I J K L M N O P Q R S T U
          A B C D E F G H I J K L M N O P Q R S T
           A B C D E F G H I J K L M N O P Q R S
            A B C D E F G H I J K L M N O P Q R
             A B C D E F G H I J K L M N O P Q
              A B C D E F G H I J K L M N O P
               A B C D E F G H I J K L M N O
                A B C D E F G H I J K L M N
                 A B C D E F G H I J K L M
                  A B C D E F G H I J K L
                   A B C D E F G H I J K
                    A B C D E F G H I J
                     A B C D E F G H I
                      A B C D E F G H
                       A B C D E F G
                        A B C D E F
                         A B C D E
                          A B C D
                           A B C
                            A B
                             A







    I want this pattern can anyone help?????

  7. #7
    Senior Member Tarah's Avatar
    Join Date
    Jun 2012
    Posts
    300
    Quote Originally Posted by Bandhavi View Post
    Code:
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
     A B C D E F G H I J K L M N O P Q R S T U V W X Y
      A B C D E F G H I J K L M N O P Q R S T U V W X
       A B C D E F G H I J K L M N O P Q R S T U V W
        A B C D E F G H I J K L M N O P Q R S T U V
         A B C D E F G H I J K L M N O P Q R S T U
          A B C D E F G H I J K L M N O P Q R S T
           A B C D E F G H I J K L M N O P Q R S
            A B C D E F G H I J K L M N O P Q R
             A B C D E F G H I J K L M N O P Q
              A B C D E F G H I J K L M N O P
               A B C D E F G H I J K L M N O
                A B C D E F G H I J K L M N
                 A B C D E F G H I J K L M
                  A B C D E F G H I J K L
                   A B C D E F G H I J K
                    A B C D E F G H I J
                     A B C D E F G H I
                      A B C D E F G H
                       A B C D E F G
                        A B C D E F
                         A B C D E
                          A B C D
                           A B C
                            A B
                             A







    I want this pattern can anyone help?????

    Code:
          char ch1 = 'A';
          int n1, c1, k1, space1 = 0;
    
          scanf("%d", &n1);
    
          for ( k1 = n1 ; k1 >= 1 ; k1-- )
          {
              for ( c1 = 1 ; c1 <= space1 ; c1++)
                  printf (" ");
    
              space1++;
    
              for ( c1 = 1 ; c1 <= k1 ; c1++ )
              {
                 printf("%c ", ch1);
                 ch1++;
              }
    
              printf("\n");
              ch1 = 'A';
          }
    This porgram will give the output like

    Code:
    26
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
     A B C D E F G H I J K L M N O P Q R S T U V W X Y
      A B C D E F G H I J K L M N O P Q R S T U V W X
       A B C D E F G H I J K L M N O P Q R S T U V W
        A B C D E F G H I J K L M N O P Q R S T U V
         A B C D E F G H I J K L M N O P Q R S T U
          A B C D E F G H I J K L M N O P Q R S T
           A B C D E F G H I J K L M N O P Q R S
            A B C D E F G H I J K L M N O P Q R
             A B C D E F G H I J K L M N O P Q
              A B C D E F G H I J K L M N O P
               A B C D E F G H I J K L M N O
                A B C D E F G H I J K L M N
                 A B C D E F G H I J K L M
                  A B C D E F G H I J K L
                   A B C D E F G H I J K
                    A B C D E F G H I J
                     A B C D E F G H I
                      A B C D E F G H
                       A B C D E F G
                        A B C D E F
                         A B C D E
                          A B C D
                           A B C
                            A B
                             A

  8. #8
    Senior Member suve's Avatar
    Join Date
    Jun 2012
    Posts
    441
    Code:
       *
      *A*
     *A*A*
    *A*A*A*

    How to get this pattern

  9. #9
    Senior Member Bandhavi's Avatar
    Join Date
    May 2012
    Posts
    169,199
    Quote Originally Posted by suve View Post
    Code:
       *
      *A*
     *A*A*
    *A*A*A*

    How to get this pattern
    Code:
    #include<stdio.h>
     
    main()
    {
        int n, c, k, space, count = 1;
     
        printf("Enter number of rows\n");
        scanf("%d",&n);
     
        space = n;
     
        for ( c = 1 ; c <= n ; c++)
        {
            for( k = 1 ; k < space ; k++)
               printf(" ");
     
            for ( k = 1 ; k <= c ; k++)
            {
                printf("*");
     
                if ( c > 1 && count < c)
                {
                     printf("A");    
                     count++; 
                }      
            }    
     
            printf("\n");
            space--;
            count = 1;
        }
        return 0;
    }
    Try this code, I have not tested..


  10. #10
    Senior Member
    Join Date
    Oct 2012
    Posts
    295
    Blog Entries
    5
    Can anyone please add more patterns :))
    Very much needful post)

Page 1 of 2 12 LastLast

Similar Threads

  1. Frequently Asked C++/OOPS Interview Questions With Answers
    By Bandhavi in forum Programming languages
    Replies: 2
    Last Post: 08-22-2014, 11:29 AM
  2. Unix Interview Questions
    By vijay in forum Unix
    Replies: 1
    Last Post: 02-27-2013, 07:25 AM
  3. Load Testing Interview Questions
    By kdp747 in forum Testing
    Replies: 10
    Last Post: 08-14-2012, 12:34 PM
  4. Replies: 3
    Last Post: 07-20-2012, 09:58 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •