So, this will be my first time asking for help with something on this site.

Currently, I am learning c, and I have progressed up to the nested loops.

I think you guys will understand my problem if I present the program, and explain what I want to do.

#include <stdio.h>
void main()
{
int i, n1, n2;
printf("Enter the value of n1 and n2:");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
i=1;
while(i<=10)
{
printf("%2d*%2d=%2d\n",n1,i,i*n1);
i++;
}
n1++;
}
}

Currently, the program above gives the output like this:

If I entered the values like five and seven:

5*1=5
5*2=10

It would go up to 50.

Same thing for the six and seven.

But I want the output in this manner:

Once more, I enter the values five and seven:

Then I want the output in this way:

5*1=5
6*1=6
7*1=7

Until the table of all three number is over. (reaching the 50, 60, and 70.) So how can I accomplish this?

+1

You need to switch the loops and use a temp variable to save the contents of n1.

``````int i = 1, n1, n2, temp;

while (i <= 10)
{
temp = n1;

while (temp <= n2)
{
printf("%2d*%2d=%2d\n",temp,i,i*temp);
temp++;
}
i++;
}``````
I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

Thank you very much, Kyle.

+1

so with the program below, I want to print the list of prime numbers in a given range. (like 1 to 10, 20 to 50, etc.) but apparently, I am missing something.

#include <stdio.h>
void main()
{
int i=2, n1, n2, f=0, n;
printf("Enter the value of n1 and n2:");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
n=n1;
while(i<n)
{
if(n%i==0)
{
f=1;
}
i++;
}
if(f==0)
printf("%d\n",n);
n1++;
}
}

Also, I hope you guys won’t mind if I post some more problems like these, since I am learning c, and I have no other way for asking help or advice other than from my teacher, and he is not available all the time.

Besides, most of the time, I do solve my problems, but if a particular problem is not getting solved by me, then I hope I can post it here.

+1

I am confused.

last time I checked this topic, there was an answer to my question, but before I could test it, it is gone.

ah well, nothing to do then wait for another one I guess...

+1

I had misinterpreted your question, the example did not actually calculate prime numbers. This one should work:

``````#include <stdio.h>
void main()
{
int i=2, n1, n2, f=0;
printf("Enter the value of n1 and n2:\n");
scanf("%d%d",&n1,&n2);
while(n1<=n2)
{
i=2;
f=0;
if(n1>1)
{
while(i<n1)
{
if(n1%i==0)
{
f=1;
break;
}
i++;
}
if(f==0)
{
printf("%d\n",n1);
}
}
n1++;
}
}``````
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

+1

So, I have these four outputs which I need to have:

Pattern 6:
1 2 3 4 5
1 2 3 4 -
1 2 3 - -
1 2 - - -
1 - - - -

Pattern 7:
1 ----
1 2 ---
1 2 3 --
1 2 3 4 -
1 2 3 4 5

Pattern 8:
1 2 3 4 5
- 1 2 3 4
- - 1 2 3
- - - 1 2
- - - - 1

Pattern 9:
1 2 3 4 5 5 4 3 2 1
1 2 3 4 - - 4 3 2 1
1 2 3 - - - - 3 2 1
1 2 - - - - - - 2 1
1 - - - - - - - - 1

I did solved the first one like this, (Pattern 6):

for(i=1;i<=5;i++)
{
for(j=1;j<=n-i+1;j++)
{
printf("%d",j);
}
for(j=1;j<i;j++)
{
printf("-");
}
printf("\n");
}

But how can I solve the remaining three?

Note: the outer loop, while it is stated i<=5, but in actuality, it is supposed to be i<=n. sorry if you get confused by that.

You can rewrite pattern 6 like this:
for(int i=n; i >= 0; i--) {
for(int j = 1; j <= n; j++) {
if(j > i) printf("%d ", j);
else printf("-");
}
printf("\n");
}
Try figuring out the other patterns now.

Roel
golfing in the kitchen

If memory serves, you can also do this too...
for(int i=n, j=1; i >= 0, j<=n; i--, j++) {
if(j > i) {
printf("%d ", j);
} else {
printf("-");
}
}
printf("\n");

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

The problem is, I need the dashes printed as well, that is why another entirely different loop.

otherwise I would have used if else from the beginning.

So I managed to solve the pattern 6, 7, and 8, but pattern 9 is still giving me trouble. some help would be nice about now.

This should work:

``````#include <stdio.h>

void main()
{
for(int i=1;i<=5;i++)
{
//count forward
for(int j=1;j<=5-i+1;j++)
{
printf("%d",j);
}
for(int j=1;j<i;j++)
{
printf("-");
}
//count backward
for(int j=1;j<i;j++)
{
printf("-");
}
for(int j=5;j>=1+(i-1);j--)
{
printf("%d",j-(i-1));
}

printf("\n");

}
}``````
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

+1

Thanks, Magurp.

Now, I hate to do this, because I know people get annoied that I always keep these nubi problems here, but honestly, I don't have any place or a person to ask these questions.

this time, I only have to print the number output, but the problem is, I have to print them in a increasing and decreasing manner in the same program.

Also, I have to print them by using only nested forloops, if that helps to you guys.

you will read what I am trying to say below:

1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1

second program:

1 1
1 2 2 1
1 2 3 3 2 1
1 2 3 4 4 3 2 1
1 2 3 4 5 5 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 3 2 1
1 2 2 1
1 1

As I said early in the post, sorry if you guys get annoied at this.

Meh, its no problem really. Its been awhile since i've worked in C, so getting a bit of practice in as time allows is fine. Hm, it looks like the first program is very similar to pattern 9 in one of your previous posts, and the second one is an extension of that. Try removing the dash loops and adjusting the parameters of the first outer for loop for the first answer. For the second, try copying the loops of the first program and paste a copy of them below it, and rewrite the outer for loop to function like pattern 9.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

@Magurp:

Could you give me  example of loop? I only need loop, nnot entire program.

Sure, its just a slight modification of pattern 9:

``````void main()
{
for(int i=5;i>=1;i--)
{

}
}``````

That would throw everything in reverse, then you just get rid of the loops that add dashes.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thanks for that, I did a dry run and after that, a compiled run of the program, and both times I got the output I want for the first program.

My biggest problem from the start is the second pattern. because not only it starts from,

1 1

It peaks at,

1 2 3 4 5 5 4 3 2 1

And ends at,

1 1

Basically, it counts upwards and downwards in a same program. now, I know that I can put more than one incriment or decrement in one loop, but I don't think it would be useful in this particular case. so, once again, I am lossed to what exactly should I do.

Note: if you are confused what I am trying to say, take another look of the second program which I have already posted the output.

For the second output question, you'll need to combine the original version of pattern 9, and the new version of pattern 9 with all the dashes stripped out, so it will count up on each row, and then down again. Using just the outer loops again as an example:

``````void main()
{
//print top half
for(int i=5;i>=1;i--)
{