打印一个表,每行给出一个整数,平和立方值

#include stdio.hint main(void){long upper = -1, lower=0;int reads;do{printf(Enter lower and upper integer limits (in that order): );

编程学习网为您整理以下代码实例,主要实现:打印一个表,每行给出一个整数,平和立方值,希望可以帮到各位朋友。

#include <stdio.h>

int main(voID)
{
  long upper = -1, lower=0;
  int reads;

  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;
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐