![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2005
Posts: 1
Rep Power: 0
![]() |
i am single 43 years old man... extreme photoshpper
just decided to get myself into programming.. so i just arrived the 'for' function in my 317 pages book i just cant figure out how do this : ( its part of a questino not a full one ) The users enters a number which will spicify the number of rows and the value of the middle number like lets say the user entered the number '7' the shape will be like this 7 \\ 12 spaces from the left 6_7_6 \\ 10 spaces from the left 5_6_7_6_5 \\ 8 spaces from the left 4_5_6_7_6_5_4 \\ 6 spaces from the left 3_4_5_6_7_6_5_4_3 \\ 4 spaces from the left 2_3_4_5_6_7_6_5_4_3_2 \\ 2 spaces from the left 1_2_3_4_5_6_7_6_5_4_3_2_1 \\ 0 spaces from the left we must use 'for' functions three or two ones anyone knows ? please share so far here is what i have done THIS : #include<stdio.h>
int main()
{
int k,h,i,j,number;
printf("Please enter a number: ");
scanf("%d",&number);
for(i=number; i > 0; i--) {
/* print leading spaces */
for(j=0; j < (i-1) * 2; j++) {
printf(" ");
}
for(k=1; k < number; k++){
printf("%d",k);}
for(h=number;h>=number;h--)
{
if(h!=1)
printf(" %d,",h);
else
printf("%d",h);
}
/* need a loop or 2 loops or something here */
printf("%d \n", i);
}
return 0;
}OR THIS #include<stdio.h>
void main ()
{
int i,j,t,n;
printf("Please enter a number\n");
scanf("%d",&n);
x=(n-i)*2
for(i=1;i<=n;i++)
{
for(j=1;j<=x;j++)
printf(" ");
for(t=1;t<=n;t++)
printf("%d",t);
for(k=n;k>=1;k--)
{
if(k!=1)
printf("SPACE HERE THEN %d",n);
else
printf("%d",n)
{
printf("\n");
}
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
I think this would be the easiest way:
int width = 7;
int i, j;
for(i = width; i > 0; i--) {
for(j = i; j < width; j++)
printf("%i ", j);
for(j = width; j >= i; j--)
printf("%i ", j);
putchar('\n');
}You can set width to any width you want. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|