2018-07-25 14:46:45

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?

Thumbs up +1

2018-07-25 15:58:57

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

2018-07-25 18:29:46

Thank you very much, Kyle.

Thumbs up +1

2018-08-06 20:05:57

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.

Thumbs up +1

2018-08-07 10:47:27

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...

Thumbs up +1

2018-08-07 11:30:24

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

Thumbs up +1

2018-10-29 12:17:07 (edited by Dark Eagle 2018-10-29 12:20:00)

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.

Thumbs up

2018-10-30 00:57:45

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

Thumbs up

2018-10-30 03:10:33

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.

Thumbs up

2018-10-30 06:06:01

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.

Thumbs up

2018-11-02 16:13:15

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.

Thumbs up

2018-11-07 11:55:08

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

Thumbs up +1

2018-11-07 19:32:55

Thanks, Magurp.

Thumbs up

2018-11-15 17:53:51 (edited by Dark Eagle 2018-11-15 17:55:30)

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.

Thumbs up

Yesterday 00:19:44

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

Thumbs up

Yesterday 13:28:56

@Magurp:

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

Thumbs up

Today 01:50:28 (edited by magurp244 Today 01:54:34)

Sure, its just a slight modification of pattern 9:

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

//your code here

    }
}

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

Thumbs up