# Input-Output Exercises

 Site: Saylor Academy Course: CS107: C++ Programming Book: Input-Output Exercises
 Printed by: Guest user Date: Thursday, March 30, 2023, 5:03 PM

## Description

Complete these exercises to check your understanding of input and output.

## C Basic Input Output statement Exercises

1. Write a program that converts Centigrade to Fahrenheit.
Expected Output:
Input a temperature (in Centigrade): 45113.000000 degrees Fahrenheit.

2. Write a C program that calculates the volume of a sphere.
Expected Output:
Input the radius of the sphere : 2.56The volume of sphere is 70.276237.

3. Write a C program that prints the perimeter of a rectangle to take its height and width as input.
Expected Output :
Input the height of the Rectangle : 5Input the width of the Rectangle : 7Perimeter of the Rectangle is : 24.000000

4. Write a C program that converts kilometers per hour to miles per hour.
Expected Output:
Input kilometers per hour: 159.320568 miles per hour

5. Write a C program that takes hours and minutes as input, and calculates the total number of minutes.
Expected Output:
Input hours: 5Input minutes: 37Total: 337 minutes.

6. Write a program in C that takes minutes as input, and display the total number of hours and minutes.
Expected Output:
Input minutes: 5469 Hours, 6 Minutes

7. Write a program in C that reads a forename, surname and year of birth and display the names and the year one after another sequentially.
Expected Output:
Input your firstname: TomInput your lastname: DavisInput your year of birth: 1982Tom Davis 1982

8. Write a program in C to calculate the sum of three numbers with getting input in one line separated by a comma.
Expected Output:
Input three numbers separated by comma: 5,10,15The sum of three numbers: 30

9. Write a C program to perform addition, subtraction, multiplication and  division of two numbers.
Expected Output:
Input any two numbers separated by comma: 10,5The sum of the given numbers: 15The difference of the given numbers: 5The product of the given numbers: 50The quotient of the given numbers: 2.000000

10. Write a C program to find the third angle of a triangle if two angles are given.
Expected Output :
Input two angles of triangle separated by comma : 50,70Third angle of the triangle : 60

Source: W3resource, https://www.w3resource.com/c-programming-exercises/input-output/index.php
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

## Solutions

##### Exercise 1

Write a C program that convert a temperature from Centigrade to Fahrenheit.

The centigrade scale, which is also called the Celsius scale, was developed by Swedish astronomer Andres Celsius. In the centigrade scale, water freezes at 0 degrees and boils at 100 degrees. The centigrade to Fahrenheit conversion formula is:

C = (5/9) * (F - 32)

where F is the Fahrenheit temperature.

Sample Solution

C Code:

#include<stdio.h>float temp_f; /* degrees fahrenheit */float temp_c; /* degrees centigrade */char line_text[50]; /* a line of input
*/int main(){ printf("Input a temperature (in Centigrade): "); fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%f",&temp_c); temp_f
=((9.0/5.0)* temp_c)+32.0; printf("%f degrees Fahrenheit.\n", temp_f); return(0);}


Sample Output:

Input a temperature (in Centigrade): 45 113.000000 degrees Fahrenheit.

Flowchart

##### Exercise 2

Write a C program that calculates the volume of a sphere.

A sphere is a perfectly round geometrical object in three-dimensional space that is the surface of a completely round ball.

In three dimensions, the volume inside a sphere is derived to be V = 4/3*π*r3 where r is the radius of the sphere

Sample Solution

C Code:

#include<stdio.h>float myradius; /* radius of the sphere */float myvolume; /* volume of the sphere (to be computed)
*/char line_text[50]; /* a line from the keyboard *//* the value of pi to 50 places, from wikipedia */constfloat PI =3.14159265358979323846264338327950288419716939937510;int main(){
printf("Input the radius of the sphere : "); fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%f",&myradius); myvolume =(4.0/3.0)* PI *(myradius
* myradius * myradius);/* volumn=(4/3) * pi * r^3*/ printf("The volume of sphere is %f.\n", myvolume); return(0);}

Sample Output:

Input the radius of the sphere: 2.56 The volume of sphere is 70.276237.

Flowchart

##### Exercise 3

Write a C program that prints the perimeter of a rectangle to take its height and width as input.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int main(){float rec_width; /* width of the rectangle */float rec_height; /* height of the rectangle
*/float rec_perimeter; /* perimeter (to be computed) */ printf("Input the height of the Rectangle : "); sscanf(line_text,"%f",&rec_height); printf("Input
the width of the Rectangle : "); sscanf(line_text,"%f",&rec_width); rec_perimeter =2.0*(rec_height + rec_width);/* perimeter = 2 * ( width + height )*/ printf("Perimeter
of the Rectangle is : %f\n", rec_perimeter); return0;}

Sample Output:

Input the height of the Rectangle: 5 Input the width of the Rectangle: 7 Perimeter of the Rectangle is: 24.000000

Flowchart

##### Exercise 4

Write a C program that converts kilometers per hour to miles per hour.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>float kmph; /* kilometers per hour */float miph;
/* miles per hour (to be computed) */char line_text[50]; /* a line from the keyboard */int main(){ printf("Input kilometers per hour: "); fgets(line_text,
sizeof(line_text), stdin); sscanf(line_text,"%f",&kmph); miph =(kmph *0.6213712); printf("%f miles per hour\n", miph); return(0);}

Sample Output:

Input kilometers per hour: 15 9.320568 miles per hour

Flowchart

##### Exercise 5

Write a C program that takes hours and minutes as input, and calculates the total number of minutes.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int hrs; /* given number of hours */int mins; /* given number of minutes */int tot_mins;
/* total number of minutes (to be computed) */constintMINaHOUR=60; /* number of minutes in an hour */char line_text[50]; /* line of input from keyboard */int
main(){ printf("Input hours: "); fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%d",&hrs); printf("Input minutes: ");
fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%d",&mins); tot_mins = mins +(hrs *MINaHOUR); printf("Total: %d minutes.\n", tot_mins);
return(0);}

Sample Output:

Input hours: 5 Input minutes: 37 Total: 337 minutes.

Flowchart

##### Exercise 6

Write a program in C that takes minutes as input, and display the total number of hours and minutes.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int tot_mins;/* given number of minutes */int hrs; /* number of hours (to be computed) */int mins;
/* number of minutes (to be computed) */constintMINaHOUR=60; /* number of minutes in an hour */char line_text[50]; /* line of input from keyboard */int
main(){ printf("Input minutes: "); fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%d",&tot_mins); hrs =(tot_mins /MINaHOUR);
mins =(tot_mins %MINaHOUR); printf("%d Hours, %d Minutes.\n", hrs, mins); return(0);}

Sample Output:

Input minutes: 546 9 Hours, 6 Minutes.

Flowchart

##### Exercise 7

Write a program in C that reads a firstname, lastname and year of birth and display the names and the year one after another sequentially.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int main(){ char firstname[20], lastname[20]; int bir_year; printf("Input your firstname: "); scanf("%s", firstname);
printf("Input your lastname: "); scanf("%s", lastname); printf("Input your year of birth: "); scanf("%d",&bir_year); printf("%s %s %d\n", firstname,
lastname, bir_year); return0;}

Sample Output:

Input your firstname: Tom Input your lastname: Davis Input your year of birth: 1982 Tom Davis 1982

Flowchart

##### Exercise 8

Write a program in C to calculate the sum of three numbers with getting input in one line separated by a comma.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int num1,num2,num3; /* declaration of three variables */int sum;char line_text[50]; /* line of input from keyboard
*/int main(){ printf("Input three numbers separated by comma: "); fgets(line_text,sizeof(line_text), stdin); sscanf(line_text,"%d, %d, %d",&num1,
&num2,&num3); sum = num1+num2+num3; printf("The sum of three numbers: %d\n", sum); return(0);}

Sample Output:

Input three numbers separated by comma: 5,10,15 The sum of three numbers: 30

Flowchart

##### Exercise 9

Write a C program to perform addition, subtraction, multiplication and division of two numbers.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int main(){ int num1, num2; int sum,sub, mult, mod; float div;
/*  * Read two numbers from user separated by comma  */ printf("Input any two numbers separated by comma: "); scanf("%d,%d",
&num1,&num2); /*  * Performs all arithmetic operations  */ sum = num1 + num2;
sub= num1 - num2; mult = num1 * num2; div =(float)num1 / num2; mod = num1 % num2; /*
* Prints the result of all arithmetic operations  */ printf("The sum of the given numbers: %d\n", sum); printf("The difference of the given numbers: %d\n",
sub); printf("The product of the given numbers: %d\n", mult); printf("The quotient of the given numbers: %f\n", div); printf("MODULUS = %d\n", mod);
return0;}

Sample Output:

Input any two numbers separated by comma: 10,5 The sum of the given numbers: 15 The difference of the given numbers: 5 The product of the given numbers: 50 The quotient of the given numbers: 2.000000MODULUS = 0

Flowchart

##### Exercise 10

Write a C program to find the third angle of a triangle if two angles are given.

Pictorial Presentation:

Sample Solution

C Code:

#include<stdio.h>int main(){ int ang1, ang2, ang3;/*are three angles of a triangle */ /* Read two angles of the triangle from user separated
by comma*/ printf("Input two angles of triangle separated by comma : "); scanf("%d, %d",&ang1,&ang2); ang3 =180-(ang1 + ang2);
/* Calculates the third angle */ printf("Third angle of the triangle : %d\n", ang3); return0;}

Sample Output:

Input two angles of triangle separated by comma : 50,70 Third angle of the triangle : 60

Flowchart