C Primer Plus Version 6 Chapter 5 Programming Exercises

Keywords: Programming

1. Convert minutes to hours and minutes, and use the while loop to let the user repeat the input until the input 0 exits.

#include<stdio.h>
int main(void)
{
	const int AHoursOfMinutes = 60;
	int minutes;
	int m_hour;
	int m_minute;
	do {
		printf("Please enter the time (minutes)");
		scanf_s("%d", &minutes);
		m_hour = minutes / AHoursOfMinutes;
		m_minute = minutes % AHoursOfMinutes;
		printf("%d hour %d Minute\n", m_hour, m_minute);
	} while (minutes != 0);

	return 0;


2. The user enters an integer and the program prints it and the 10 digits behind it.
 

#include<stdio.h>
int main(void)
{
	int n;
	printf("Please enter an integer");
	scanf_s("%d", &n);
	for (int i = n; i <= n + 10; i++)
		printf("%d ", i);

	getchar(); getchar()a;
	return 0;

3. Enter days, convert them into weeks and days, and exit when users enter non-positive integers. This question is about the same as Question 1.

#include<stdio.h>
int main(void)
{

	int days;
	int weeks;
	int day;
	do {
		printf("Input days");
		scanf_s("%d", &days);
		weeks = days / 7;
		day = days % 7;
		printf("%d week,%d day\n", weeks, day);
	} while (days > 0);


	getchar(); getchar();
	return 0;
}

4. Input height (cm) into inches.

#include<stdio.h>
		int main(void)
	{
		float height = 1;
		float inchs;
		int feet;
		while (height > 0)
		{
			printf("Input height (cm)");
			scanf_s("%f", &height);
			if (height <= 0)
				break;
			inchs = height / 2.54;
			feet = inchs / 12;
			inchs -= feet * 12;
			printf("%f cm = %d feet, %f inches\n", height, feet, inchs);

		}
		printf("Bye");
		getchar(); getchar();
		return 0;
	} 

5.6 slightly

7. Computational cube

#include<stdio.h>
	double cube(double dd)
	{
		return (dd * dd * dd);
	}
	int main(void)
	{
		double dd;
		printf("Enter a floating point number");
		scanf_s("%lf", &dd);
		printf("%lf", cube(dd));
		getchar(); getchar();
		return 0;
	}

8. Write a program for calculating the model

#include<stdio.h>

		int main(void)
	{
		int first;
		int second;
		printf("Seeking module:\n");
		printf("Enter an integer as%The value on the right");
		scanf_s("%d", &second);
		printf("Input the first number");
		scanf_s("%d", &first);
		printf("%d %% %d is %d\n", first, second, first%second);
		while (first > 0)
		{
			printf("Enter the next number (<=0 Exit)");

			scanf_s("%d", &first);
			if (first <= 0)
				break;
			printf("%d %% %d is %d\n", first, second, first%second);
		}
		printf("Done");
		return 0;
	}

 

9. Input Chinese temperature, convert to Celsius and Open temperature

#include<stdio.h>
	void Temperatures(double f)
	{
		double CT = 5.0 / 9.0*(f - 32.0);
		double KT = CT + 273.16;
		printf("%.2lf °F , %.2lf °C ,%.2lf K\n", f, CT, KT);
	}
	int main(void)
	{
		double  FT;
		printf("Input a Chinatown temperature F (q Sign out):");
		while (scanf_s("%lf", &FT) == 1)
		{
			Temperatures(FT);
			printf("Input a Chinatown temperature F (q Sign out):");
		}
		return 0;
	}

 

Posted by Tucker1337 on Fri, 25 Jan 2019 10:45:13 -0800