Search C Program

Number Pattern - 5

12345
1234
123
12
1

#include <stdio.h>

int main()
{
    int i, j;
    for(i=5;i>=1;i--)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }

    return 0;
}


Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C

15 comments:

  1. what is the error in this code for the above program?
    #include

    main()
    {

    int i,j;
    for( i=1; i>=5;i--)
    {
    for(j=1;j<5-(i-1);j++)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    }

    ReplyDelete
    Replies
    1. How this fow loop condition will get satisfied ??
      for( i=1; i>=5;i--)

      i is eq 1 and your condition is i >=5 which will be false...

      Delete
    2. for condition is false:
      correct condition
      for(i=1;i<=5;i++)

      Delete
    3. This comment has been removed by the author.

      Delete
  2. #include
    using namespace std;
    int main(){
    int n,i,j;
    cout<<"asdf";
    cin>>n;
    for(i=1;i<=n;i++){
    for(j=1;j<n-i+1;j++){
    cout<<j;
    }
    cout<<endl;

    }
    return 0;
    }

    ReplyDelete
  3. write the same program with the help of a while loop

    ReplyDelete
    Replies
    1. /*
      * To represent the parts of a for loop,
      * we'll use the for(x;y;z) representation,
      * where x is the initializer (e.g. i = 1),
      * y is the conditional statement (e.g. i <= j),
      * and z is the increment statement (e.g. ++j).
      *
      * In this code, there are two loops used,
      * so we'll refer to first one as the outer loop,
      * and the second as the inner loop.
      */
      #include

      void main(void)
      {
      int i, j;

      printf("Enter a number (for lines): ");
      scanf("%d", &i); /* If we are to use the value inputted here
      * as the initial value for our outer loop below,
      * then we need not initialize it,
      * i.e., we would leave the x part of the for loop,
      * so it looks like 'for(;y;z)' if we use the for loop
      */

      while (i) // 'while(i)' is basically the same as 'while(i>0)' here
      { // since any nonzero number is considered true. This is the outer y.
      j = 1; // This is the inner x.
      while (j <= i) // The inner y.
      {
      printf("%d ", j);
      ++j; // The inner z.
      }
      printf("\n");
      --i; // The outer z.
      }
      }

      /* You see, the program is really hard to read and maintain
      * if we use while loop for these things, that's why for loops
      * are preferred here.
      */

      Delete
  4. temp=n;
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=temp;j++)
    printf("j");
    temp--;
    printf("\n");
    }

    ReplyDelete
  5. int i,j;
    for(i=1;i<=5;i++) {
    for(j=1;j<=(6-i);j++) {
    printf("%d",j);
    }
    printf("/n");
    }

    ReplyDelete
  6. #include

    int main()
    {
    int r,i,j;

    printf("Enter the number of rows : ");
    scanf("%d",&r);

    for(i=1;i<=r;i++)
    {
    for(j=1;j<=r-i+1;j++)
    {
    printf("%d",j);
    }
    printf("\n");
    }

    return 0;
    }

    ReplyDelete
  7. The soltn is incorrect there is no coding for sopace first apply for loop for space then another for loop to print the pattern

    ReplyDelete
  8. Write a program to input any Sentence and find the longest word and the second longest word.
    eg: India is my Country
    longest word: Country
    Second longest word: India.
    How do i write this program.

    ReplyDelete