Exercise 2-1.

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;
}
时间: 2024-10-03 14:00:53

Exercise 2-1.的相关文章

Exercise: Maps (单词统计)

A Tour of Go Exercise: Maps https://tour.golang.org/moretypes/23 WordCount (单词统计) 是一个很经典的小程序了,在很多编程入门教程中都会出现. 这道题比较简单,但也有一些知识点值得一提. 上面这个答案我是参考了网上别人写的.但在参考别人之前我也自己解题了,其中,唯一不同之处是这一句: m[word]++ 我本来写的是: _, ok := m[word] if ok { m[word]++ } else { m[word]

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

【DeepLearning】Exercise:PCA in 2D

Exercise:PCA in 2D 习题的链接:Exercise:PCA in 2D pca_2d.m close all %%================================================================ %% Step 0: Load data % We have provided the code to load data from pcaData.txt into x. % x is a 2 * 45 matrix, where the

kk Exercise 6.1 Convert an integer to words

Exercise 6-1. write a program that will prompt for and read a positive integer less than1,000 from the keyboard, and then create and output a string that is the value of theinteger in words. For example, if 941 is entered, the program will create the

Exercise 3.3 Calculate a discounted price

Exercise 3-3. Write a program that will calculate the price for a quantity entered from the keyboard, given that the unit price is $5 and there is a discount of 10 percent for quantities over 30 and a 15 percent discount for quantities over 50. 1 //

[Usaco2010 Dec]Exercise 奶牛健美操

[Usaco2010 Dec]Exercise 奶牛健美操 题目 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径.简单的说来, 这些点的布局就是一棵树,且每条边等长,都为1. 对于给定的一个奶牛路径集合,精明的奶牛们会计算出任意点对路径的最大值, 我们称之为这个路径集合的直径.如果直径太大,奶牛们就会拒绝锻炼. Farmer John把每个点标记为1..V

Exercise 2.3 Calculating volume price of alternative products

// Exercise 2.3 Calculating volume price of alternative products // The only problem here is to devise a way to determine the price // for the product type. Here I used the product type value to do this. #include <stdio.h> int main(void) { double to

Exercise 2.1 Convert inches to yards, feet, and inches

Exercise 2-1. Write a program that prompts the user to enter a distance in inches andthen outputs that distance in yards, feet, and inches. (For those unfamiliar with imperialunits, there are 12 inches in a foot and 3 feet in a yard.) #include <stdio

List exercise

The slice operator can take a third argument that determines the step size, so t[::2] creates a list that contains every other element from t. If the step size is negative, it goes through the list backward, so t[::-1] creates a list of all the eleme