Enter an integer: 1

advertisement
152111011 INTRODUCTION TO PROGRAMMING LAB
LAB WORK 5
Objectives:
 Repetition in programs. The for statement
 Start  Programs  Microsoft Visual Studio 2010
 File  New  Project
 Select Visual C++  Empty Project and write project name Lab05 at bottom.
 In Solution Explorer right click Source Files folder  Add  New Item
 Select C++ File (.cpp) and give name EX1.c at bottom.
 Add a new item to this project such as EX1.c, EX2.c, etc.. for each of the following
programs for today’s lab work. Do not forget exclude previous example.
 Right click file (EX1.c) and select Exclude from project.
1.
The following program (EX1.C) computes the sum of the first 30 terms of the series
1
1 1
1
  ...  + …
3 5
59
Code the flowchart, enter, compile and run.
2.
Modify the program that you wrote for the previous exercise, save it as EX2.C so that it
computes the first 20 terms of the series
1 1 1
1    ...
3 5 7
Hint: You can use an integer variable sign and initialize it to 1 before the loop execution begins.
Each time a new term is computed, update the value of this variable as sign = sign*(-1)
1
3.
The following program (EX3.C) displays the
conversion table from Celsius to Fahrenheit
as follows:
Celsius
10
5
0
-5
Fahrenheit
50.00
41.00
32.00
23.00
4.
Write a program (EX4.C) that finds the
smallest of several integers entered from
keyboard. Your program should read the
number of integers before the data values. Use
the following algorithm:
Enter the code, compile and run it.
/* Conversion of Celsius to Fahrenheit
temperatures */
#include <stdio.h>
#define CBEGIN 10
#define CLIMIT -5
#define STEP 5
int main(void){
int celsius;
double fahrenheit;
printf("
Celsius
Fahrenheit\n");
/* Display the table heading */
/* Display the table */
for(celsius=CBEGIN;celsius>=CLIMIT;celsius-=
STEP)
{
fahrenheit = 1.8 * celsius + 32.0;
printf("%10d%15.2f\n", celsius , fahrenheit);
}
return (0);
}
2
A sample run should be as follows:
How many numbers? 5
Enter an integer: 4
Enter an integer: 5
Enter an integer: 3
Enter an integer: 1
Enter an integer: 8
The smallest value is : 1
Download