1

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

2

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

Thumbs up +1

3

Thank you very much, Kyle.

Thumbs up +1

4

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

5

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

6

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