Hello,
I wish to create the following patterns with numbers.
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Hello,
I wish to create the following patterns with numbers.
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Sounds good, so go ahead and do it
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hello I just can't code it
can anyone please help me to write its code?
I have just written
but this is not workingCode:#include<stdio.h> #include<conio.h> void main() { clrscr(); int i=0,base=0,j=0,p=0,diff=0; printf("Enter the value of base: "); scanf("%d",&base); for(i=1;i<=base;i++) { printf("%d\t",i); //getch(); for(j=1;j<=i-1;j++) { do { diff=base-j; p=p+diff+i; printf("%d\t",p); j++; }while (j<=i-1); printf("%d\t",p); //getch(); } printf("\n"); } getch(); }
You should look at the triangle you wish to get again. The method of getting from column x to column x+1 is way way way way easier than you seem to think it is, based on the code. (EDIT: Or maybe you are just trying to use p in a couple different and incompatible ways. It's not clear, because hey, you could also comment your code to say what you're trying to do.) Also you have a do-while loop inside a for loop, which is not wrong in and of itself, but in this case is just silly.
Hey please can you write the code for me?
I just can't get it.
Yet I have developed the code but want to make some changes that it have a lot of variables, can you please cut down?
Code:#include<stdio.h> #include<conio.h> void main() { int i,j,c,b,p=0; clrscr(); printf("enter the name"); scanf("%d",&b); for(i=1;i<=b;i++) { printf(" %d",i); c=b-1; p=c+i; for(j=2;j<=i;j++) { printf(" %d",p); c=c-1; p=p+c; } p=0; printf("\n"); } getch(); }
Can, but won't.
You can probably get away with one less variable in your code, but there's no reason for you to do so, as each variable that you have has a nice meaning -- of course it's a little bit of work to figure out the meanings with names like "c" and "p" and the like.
That is... weird. You say that you "just can't get it", yet you have written a program that appears to do what you wanted to do.Originally Posted by bharat_kaushik
My suggestions are:
- Get rid of the unnecessary inclusion of <conio.h> and use of the non-standard clrscr() and getch().
- Change the return type of main to int.
- Instead of asking for a name, ask for something more sensible.
- Indent your code.
- Declare variables at the start of the first block in which they are used.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
okey ya now I got the solution for my problem, just wish if anyone have any other solution using arrays or something else then please write for me.
Personally, I feel that c really should be removed, with b renamed to num_rows and p renamed to sum.Originally Posted by tabstop
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
being just a newbie here. you are really good man. We haven't undergone looping yet in our programming. I may not be able to help you. I just want to express my amazement on your thread.
Code:/* This type of printing can be done using simple mathematical induction r&c c = | 0 1 2 3 4 ______________________________ r 1 | 1 | 2 | 2 (2+4) | 3 | 3 (3+4) (7+3) | 4 | 4 (4+4) (8+3) (11+2) | 5 | 5 (5+4) (9+3) (12+2) (14+1) for(r = 1 to 4) c = 0; p = r; while(c<r) // for upper triangle c++ p = p + (5-c) */ #include<stdio.h> main() { int r,c,p; for(r=1;r<5;r++) { for(c=0,p=r; c<r ; c++,p=p+(5-c)) printf("%d\t",p); printf("\n"); } }