for (y = 1; y < num1 + 1; y++)
    {
        for(x = 1; x < num2 + 1; x++)
        { 
          if (y >= 1 && y <= 4 && x >= 1 )
          {  
             printf ("%d x %d = %d", y, x, y*x);
             printf ("   ||   ");
          }
          else if (y >= 5 && y <= 9)
          {
             printf ("%d x %d = %d", y, x, y*x);
             printf ("   ||  "); 
          }          
          else if (y == 10)
          {
             printf ("%d x %d = %d", y, x, y*x);
             printf ("  ||  "); 
          }              
        }
          if (y >= 1 && y <= 8)
          {
             printf ("\n");
             printf("       ");
          }
          else if (y == 9)
          {
             printf ("\n");
             printf("      ");  
          }
    }    

I tried changing the for loop to do while + while but doesn't seem to work.

do
    {
        while( x <= num2)
        { 
          if (y >= 1 && y <= 4)
          {  
             printf ("%d x %d = %d", y, x, y*x);
             printf ("   ||   ");
          }
          else if (y >= 5 && y <= 9)
          {
             printf ("%d x %d = %d", y, x, y*x);
             printf ("   ||  "); 
          }          
          else if (y == 10)
          {
             printf ("%d x %d = %d", y, x, y*x);
             printf (" || "); 
          }
          x++;              
        } 
        if (y >= 1 && y <= 9)
        {
             printf ("\n");
             printf ("       ");
        }
        else if ( y == 10)
        {
             printf ("\n");
             printf ("     ");
        }
        y++;          
    }
    while (y != num1 + 1);

Recommended Answers

All 2 Replies

Did you set the value of y to 1 before entering the do/while loop?
Also you have:

if (y >= 1 && y <= 4)

as far as I can see y would always be greater than or equal to 1, so there's no need to test that condition.

In for loop program,
the x and y values are initialised on their own as x=1 or y=1; whereas in in the second one you'l have to initialise it before loop starts.. and after y++ re initialize x to 1; else the condition will fail...
and this is not the best way to do do this program.. youhave made it very complex!
instead of that, try this:

#include<conio.h>
#include<stdio.h>
int i=1,j=1,num,num1;
int main()
{

 clrscr();
 printf("Enter The Limits= ");
 scanf("%d%d",&num,&num1);
 do
  {
    while(i<=num1)
     {
      printf("%d*%d=%d\n",j,i,j*i);
      i++;
     }
    j++;
    i=1;
    printf("\n");
  }
  while(j<=num);
  getch();
  return 0;
}
NOTE:

Re- initialize as i've done after j++ . This is very important when you are using while or do while loop!!

and in for loop program, use

for (y = 1; y <=num1; y++)
     {
          for(x = 1; x <=num2; x++)
          {

i.e.

y<=num1

instead of

y<num1+1

it reduces complexity in program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.