Number pattern in C language

Number pattern in C language

Question: Write a C program to print the below number pattern

Explanation for number pattern:

From the above image we can see number decreases if the column value is greater than or equal to row value.

Logic:

  • Get the input from the user which is n.
  • Both the row and column size is n.
  • Store the n value into the temp variable to print the pattern
  • Decrement the temp value only if column value(j) is greater than or equal to row value(i)
  • Repeat the above conditions in a loop to get the pattern

Brief explanation:

  • Let us consider input n=5 and then go to the loop to print the pattern
  • The outer loop executes for 5 times which is the row size
  • And inner loop also do the same but for column size which is 5
  • For the first row temp value is 5 and it is not going to decrement because column value (i = 5) is not greater than or equal to column value (j = 4,3,2,1,0)
  • In the next row is print the first column value (temp = 5) and then check the condition it is true then decrement the value of (temp = 4) then print remaining column values
  • Same thing will happen for remaining rows, at last will get the number pattern

Program:


#include <stdio.h>

int main()
{
    int n;
    int i,j;
    int temp;
    printf("Enter the Input:");
    scanf("%d", &n);

    for (i = n; i > 0; i--)
    {
        temp = n; //Storing the number of rows in a temp variable
        for(j = n - 1; j >= 0; j--)
        {
             printf("%d ",temp); //Print the temp variable start with value (n)
             if(j >= i) //Decrement the temp value only if column value is greater than or equal to row value
                 temp--;
       }
       printf("\n"); //new line to print values in next row
    }

    return 0;
}

You Might also like:

Print The Snake Pattern Using Two Loop

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x