08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3 ■ Programs with Sequence Logic

When executed,

• The printf statement will ask for your name.

• gets will wait for you to type your name. When typed, the name will be

stored in the variable name.

• printf will then print a greeting using your name.

Your computer screen will look as follows (assuming Birdie is typed as the name):

Hi, what's your name? Birdie

Delighted to meet you, Birdie

3.5 Examples

We now write programs to solve a few problems. You should try solving the problems before

looking at the solutions. In the sample runs, the underlined items are typed by the user;

everything else is printed by the computer.

3.5.1 Problem 1 - Average

Write a program to request three integers and print their average to one decimal place. The

program should work as follows:

Enter 3 integers: 23 7 10

Their average is 13.3

A solution is shown as Program P3.4.

Program P3.4

//request 3 integers; print their average

#include <stdio.h>

int main() {

int a, b, c;

double average;

printf("Enter 3 integers: ");

scanf("%d %d %d", &a, &b, &c);

average = (a + b + c) / 3.0;

printf("\nTheir average is %3.1f\n", average);

}

55

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!