-
Loop
Hi
I want the program to take in a number n, and then print n,n-1,n-2 till 1
the next line, it should start from n-1,n-2 till 1
then n-2,n-3 till 1
eg n=5, then
54321
4321
321
21
1
I tried but it doesnt work as I though.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0;
clrscr();
printf("Enter value of N\n");
scanf("%d",n);
while(n!=0)
{
for(i=n;i>=1;i--,n--)
{
printf("%d",i);
printf("\n");
}
}
getch();
}
any help?
-
You probably only want n to go down once per line, not one time for every digit printed.
-
fixed it
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n, row,col;
clrscr();
printf("enter n\n");
scanf("%d",&n);
for(row=n; row>=1; row--)
{
for(col=row; col>=1; col--)
{
printf("%d ",col);
}
printf("\n");
}
for(row=1; row<=n; row++)
{
for(col=1; col<=row; col++)
{
printf("%d ",col);
}
printf("\n");
}
getch();
}