Prints a table with each line giving an integer, its square, and its cube. - C Data Type

C examples for Data Type:int

Introduction

Ask the user to input the lower and upper limits for the table.

Use a for loop.

Demo Code

#include <stdio.h>

int main(void)
{
  long upper = -1, lower=0;
  int reads;//  w  ww . jav a 2  s. co  m

  do{  
    printf("Enter lower and upper integer limits (in that order): ");
    reads = scanf("%ld%ld", &lower, &upper);
    if (reads != 2){
      while (getchar() != '\n') ; // if read fails, clear input buffer
    }
  } while (lower > upper); // if lower is greater than upper, get new input

  printf("\n");

  printf(" Integer       | Square        | Cube          \n");
  printf("---------------|---------------|---------------\n");
  for (long int i = lower; i <= upper; i++)
  {
    printf(" %-14ld| %-14ld| %-14ld\n", i, i * i, i * i * i);    
  }
  printf("\n");

  return 0;
}

Result


Related Tutorials