PLease help of doing a programming for the output is like this
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210
this is a pyramid
using a loop...
please help me???
PLease help of doing a programming for the output is like this
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210
this is a pyramid
using a loop...
please help me???
What have you done so far?
- lmov
Either post some code, or do your own damn homework.
Ramble on...
functions f and f1 return the same values , f is going to be very inefficient compared to f1 .
The logic of the pattern is
at the ith row there are 2*i-1 elements , the starting from i%10 , the values are are incremented by 1 (modulo 10) for the first i elements , therafter they are decremented by 1 (modulo 10 adjusted for negative by adding 10 if required) .
#include <stdio.h>
int f(int i,int j)
{
int ans;
if( (i<=0) || (j<=0) ) return -1;
if(j==1) ans=i%10;
else if(j<=i)
ans= (f(i,j-1)+1)%10;
else
{
ans=(f(i,j-1)-1)%10;
if(ans<0) ans +=10;
}
return ans;
}
int f1(int i,int j)
{
int ans;
if( (i<0) || (j<0) ) return -1;
if(j<=i) ans=(i+j-1)%10;
else
{
ans=(2*i-1-(j-i))%10;
if(ans<0) ans +=10;
}
return ans;
}
int main(void)
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=2*i-1;j++)
printf("%d",f(i,j));
putchar('\n');
}
return 0;
}
I deleted my previous post because I was a jerk in it. Sorry. Hey pinko they way you took was much harder then nessary. I did it using 3 variables, and a total of 3 for loops.
Paul to do this pyramids you have to look at the different elements. In this one I found 3 main parts. 1 is the center number, 2 is the outside numbers, and 3 is the number in between.
First the center number. I called this value X. X is increasing by 2 on every line but displaying only the last digit. X increase to a max of 19. A simple loop for that is here: for(x=1;x<20;x+=2).
The second one is the outside numbers. I called this value Z. Z is increasing by 1 on every line. Now I didn't use a for loop on this. I just increased it by 1 on every X loop.
The third is the numbers between Z and X. I called this value Y. Now Y is used two for loops on. One for the values between Z and X, and the second for the values between X and the second Z.
Now that I've explained how to do it you should be able to read the code and understand how to do them in the future.
Code:int main(void) { int x,y,z=0; for(x=1;x<20;x+=2) { for(y=z+1;y<x;y++) printf("%d",y%10); printf("%d",x%10); for(y=x-1;y>z;y--) printf("%d",y%10); printf("\n"); z++; } }
Oh yeah? Top this (using a loop)!
Quzah.Code:#include <stdio.h> int main( void ) { char *data[10] = { " 1", " 232" , " 34543" , " 4567654" , " 567898765" , " 67890109876" , " 7890123210987" , " 890123454321098" , " 90123456765432109" , "0123456789876543210"}; int x; for(x=0;x<10;x++) puts(data[x]); return 0; }
I dont think whether one can claim that a particular program is more efficient just by counting the number of variables and loops , for example this program which is shorter , uses only two variables and only two loops and is almost as efficient (or inefficient) as my earlier program using the second function. It might save a teeny weeny bit on function call overhead though . For example you just split up what I have done in my second loop into two loops , since the loops are not nested you are not losing anything for the extra loop (I think) compared to the program below .
I think your program will be slightly faster than the one below .
#include <stdio.h>
int main(void)
{
int i,j;
for(i=1;i<=10;i++ {
for(j=1;j<=2*i-1;j++) {
if(j<=i) printf("%d",(i+j-1)%10);
else printf("%d",(3*i-j-1)%10);
}
putchar('\n');
}
return 0;
}
Originally posted by Thantos
I deleted my previous post because I was a jerk in it. Sorry. Hey pinko they way you took was much harder then nessary. I did it using 3 variables, and a total of 3 for loops.
Paul to do this pyramids you have to look at the different elements. In this one I found 3 main parts. 1 is the center number, 2 is the outside numbers, and 3 is the number in between.
First the center number. I called this value X. X is increasing by 2 on every line but displaying only the last digit. X increase to a max of 19. A simple loop for that is here: for(x=1;x<20;x+=2).
The second one is the outside numbers. I called this value Z. Z is increasing by 1 on every line. Now I didn't use a for loop on this. I just increased it by 1 on every X loop.
The third is the numbers between Z and X. I called this value Y. Now Y is used two for loops on. One for the values between Z and X, and the second for the values between X and the second Z.
Now that I've explained how to do it you should be able to read the code and understand how to do them in the future.
Code:int main(void) { int x,y,z=0; for(x=1;x<20;x+=2) { for(y=z+1;y<x;y++) printf("%d",y%10); printf("%d",x%10); for(y=x-1;y>z;y--) printf("%d",y%10); printf("\n"); z++; } }