Home > Back-end >  How Would I go about Printing a Statement after each iteration of the loop? C
How Would I go about Printing a Statement after each iteration of the loop? C

Time:04-23

Im very new to C, The programs primary function is to print multiplication tables up to the users specified integers. But the main thing that Im missing is being able to print the designated times table for which is being printed. For example this is the 1's time table this is the 2's time table etc. Thanks for your Help.

This is my output...

enter image description here

This is the Output i'am Looking to get to....

enter image description here

RAW CODE:

#include<stdio.h>


int main()
{
     int i, j, n, z,  product;
    
    printf("Please Enter an Interger: ");
    scanf("%d", &n);
    
    //next Interger
    
    printf("Please Enter an Interger: ");
    scanf("%d", &z);
    
     /* Generating Multiplication Table */
     for(i=1;i<=n;i  )
     {
          for(j=1;j<=z;j  ) //Nested For Loop to iterate until the second interger is met
          {
               product = i*j;
               printf("%d x %d = %d\t", i, j, product);
          }
          printf("\n");
     }
     return(0);
}

CodePudding user response:

You are looking for this.

#include <stdio.h>

int main()
{
    int n, m, i, j, product;
    
    printf("Enter integer: ");
    scanf("%d", &n);
    
    printf("Enter integer: ");
    scanf("%d", &m);
    
    printf("\n");
    
    for(i=1; i<=n; i  ) {
        printf("* %d Times Table*\n", i); //###
        for(j=1; j<=m; j  ) {
            product = i*j;
            printf("\t%d x %d = %d\n", i, j, product); //###
        }
        printf("****************"); //###
        printf("\n\n"); //###
    }
    

    return 0;
}

The logic was fine, but you had to fix a bit your printf usage.

CodePudding user response:

A little Bit Changes are Needed

#include<stdio.h>

int main()
{
 int i, j, n, z,  product;

printf("Please Enter an Interger: ");
scanf("%d", &n);

//next Interger

printf("Please Enter an Interger: ");
scanf("%d", &z);

 for(i=1;i<=n;i  )
 {
      printf("\n*%d Times Table*",i); //statement to print before table printing
      for(j=1;j<=z;j  ) //Nested For Loop to iterate until the second interger is met
      {
           product = i*j;
           printf("\n %d x %d = %d\t", i, j, product); //to print on new line
      }
      printf("\n");
      printf("\n***********"); //* pattern as output demand     
 }
 return(0);

}

  • Related