[英]Number pattern printing in c
I'm newbie to programming.我是编程新手。 So as an exercise, I'm trying to print a number pattern like below.因此,作为练习,我正在尝试打印如下所示的数字模式。
0
10
210
3210
43210
I tried the code below.我尝试了下面的代码。
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<=n;i++)
{
for(j=0;j>=i;j--)
{
printf("%d",i);
}
printf("\n");
}
}
The output am getting after running the code above is:-运行上面的代码后得到的 output 是:-
10
10
10
10
10
Am just stuck.我只是卡住了。 Not able to solve this question.无法解决这个问题。 Can anyone help me please?任何人都可以帮助我吗?
There are two loops.有两个循环。 The first loop control the line number with range as [0,N).第一个循环控制范围为 [0,N) 的行号。 The second loop control the character sequences per line.第二个循环控制每行的字符序列。 Each line print out [Line_Number + 1] digital numbers.每行打印出 [Line_Number + 1] 个数字。 The sequence has a pattern as [Line_Number - Column_Number].该序列的模式为 [Line_Number - Column_Number]。
The example code:示例代码:
#include <stdio.h>
void main() {
int i, j, n = 5;
for (i = 0; i < n; i++) {
for (j = 0; j < (i + 1); j++) {
printf("%d", (i - j));
}
printf("\n");
}
}
Build and run:构建并运行:
gcc test.c
./a.out
The output: output:
0
10
210
3210
43210
You have done a simple mistake for the inner loop-j
.您对inner loop-j
犯了一个简单的错误。 Make sure that your outer loop i-loop
refers to the number of lines and your printing as printf("%d",i);
确保您的外部循环i-loop
指的是行数,并且您的打印为printf("%d",i);
defines how many lines you want.定义你想要多少行。
#include<stdio.h>
void main()
{
int i ,j,n=5;
for(i=0;i<n;i++)
{
for(j=i;j>=0;j--)
{
printf("%d",j);
}
printf("\n");
}
}
Then the output will be:那么 output 将是:
0
10
210
3210
43210
#include<stdio.h>
void main()
{
int i ,j,n=5,t[n];
for(i=0;i<n;i++)
{
t[i]=i;
for(j=i;j>=0;j--)
{
printf("%d",t[j]);
}
printf("\n");
}
try this...尝试这个...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.