码迷,mamicode.com
首页 > 其他好文 > 详细

Exercise 2-1.

时间:2015-04-29 08:34:44      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

 

Exercise 2-1. Write a program that prompts the user to enter a distance in inches and
then outputs that distance in yards, feet, and inches. (For those unfamiliar with imperial
units, there are 12 inches in a foot and 3 feet in a yard.)

//ME
#include<stdio.h>
int main()
{
    int inches;
    int feet;
    int yards;
    
    printf("please enter the distance in inches:");
    scanf ("%d", &inches);
    feet = inches / 12;
    yards= feet / 3;
    feet = feet % 3;
    inches = inches % 12;
    
    printf(" the dis tance is %d yards, %d feet, %d inches", yards, feet, inches); 
    
    return 0;
}

 

// Exercise 2.1 Convert inches to yards, feet, and inches
#include <stdio.h>

int main(void)
{
  int inches = 0;
  int yards = 0;
  int feet = 0;
  const int inches_per_foot = 12;  // There are 12 inches in 1 foot.
  const int feet_per_yard = 3;     // Rhere are 3 feet in 1 yard.

  printf("Enter a distance in inches: ");
  scanf("%d", &inches);

  feet = inches/inches_per_foot;   // Get whole feet.
  yards = feet/feet_per_yard;      // Get whole yards in value of feet.
  feet %= feet_per_yard;           // Get residual feet.
  inches %= inches_per_foot;       // Get residual inches.

  printf("That is equivalent to %d yards %d feet and %d inches.\n", yards, feet, inches);
  return 0;
}

 

Exercise 2-1.

标签:

原文地址:http://www.cnblogs.com/huimotuo/p/4464751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!