C11A : Introduction to C Programming
Assignment Sheet
How to Submit the Exercises
The programming exercises should be submitted by e-mail directly to your instructor.
The subject line of the e-mail should include the name of the program you are submitting.
For each programming exercise you should attach to your e-mail the file containing the exact C
program you wrote as an attached file to your e-mail. Do not paste the program into the email.
The C Compiler
To run the programs you write on your computer, you need to have the Dev-C++ 5.x compiler.
The compiler can be downloaded free from the following web-site:
http://sourceforge.net/projects/orwelldevcpp/files/Setup Releases/Dev-Cpp 5.4.2 MinGW 4.7.2
Setup.exe/download/
(Note, you may need to copy and paste to your browser.)
The tutorial for working with the Dev-C++ is available on the EPGY website (on your student
page).
http://epgy.stanford.edu/coursepage/course.html?C11A/index.html
Note, the location of where to copy the extended libraries is different from previous versions of
Dev-Cpp. The proper location to copy them to can be found by running Dev-Cpp and selecting
"Tools", then "Compiler Options", then the "Directories" tab, and lastly the "C Includes" tab. The
dialog will then display the path to the include folder you'll need to copy the files to.
The Text Book
The textbook for this course is:
“The Art and Science of C,” by Eric Roberts, Published by Addison-Wesley Publising Company.
ISBN number: 0201543222.
You may be able to find this book at a local college bookshop. This book is also available
through the World Wide Web from amazon.com, an online bookselling agency.
c11A - Programming Exercises
- 1 – Rev 4/2014
C11A
Introduction to C Programming
PROGRAMMING
EXERCISES
For week1 – week12
(15000 – 15890)
c11A - Programming Exercises
- 2 – Rev 4/2014
Week 2 - The compiler
________________________________________________________________________
EPGY Lesson Number – 15210.
2.1. Running the “hello world” program
You should write and run the following program during the lecture (interactively).
There is no need to send this program to your instructor.
/*
* File: hello.c
* -------------* This program prints the message “Hello, World”
* on the screen.
*/
#include <stdio.h>
main()
{
printf ("Hello, World\n");
getchar();
}
EPGY Lesson Number – 15220.
2.2. Running the “hello you” program
You should write and run the following program during the lecture (interactively).
There is no need to send this program to your instructor.
/*
* File: helloYou.c
* --------------------* This program prints a more personal greeting by reading
* in the name of the user.
*/
#include <stdio.h>
int main(void)
{
char string[80];
printf("What is your name?\n");
gets(string);
printf("Hello, %s\n", string);
getchar();
return 0;
}
c11A - Programming Exercises
- 3 – Rev 4/2014
_______________________________________________________________________
EPGY Lesson Number – 15220.
2.3. Running the “prnt20” program
Due: End of week 2. Assignment #1
Type in the following program, save it in your special folder, run it and make sure you
get the requested results.
The text of the program is as follows,
#include <stdio.h>
/* File name: prnt20.c
* This program prints the numbers
* from 1 to 20, on the screen
*/
main()
{
int i;
for (i=1; i<=20; i++)
{
printf ("%d\n", i);
}
getchar();
}
________________________________________________________________________
Please send this file to your instructor.
File name should be: prnt20.c
c11A - Programming Exercises
- 4 – Rev 4/2014
Week 3, 4 - The Fundamentals of C
________________________________________________________________________
EPGY Lesson Number – 15310.
3.1 The Structure of C Programs
Due: Middle of week 3. Assignment #2
Write a program that displays the following message on the output screen:
This is
my first C program
__
(__ location of the cursor)
Please note:
If you need to write a program that only needs to display messages on the screen (by
using printf), without computing or reading values entered by the user, then it’s enough
to include only the standard library (stdio), as done in the hello world example:
File name should be: first.c
________________________________________________________________________
EPGY Lesson Number – 15310.
3.1 The Structure of C Programs
Due: Middle of week 3. Assignment #3
Change the hello world program, so that the message that is displayed on the output
screen will be:
Hello,
World
__
( __ location of the cursor)
The file name should be: hello1.c
c11A - Programming Exercises
- 5 – Rev 4/2014
Remember to save your programs in the exercises folder you created
_______________________________________________________________________
EPGY Lesson Number – 15310.
3.1 The Structure of C Programs
Due: Middle of week 3. Assignment #4
Write a program that prints a birthday greeting.
The output display should be exactly in the following form:
********************
*
*
(20 stars on the wider side of the rectangle,
*
Happy Birthday *
and 5 stars on the narrow side).
*
*
********************
The file name should be : birthday.c
c11A - Programming Exercises
- 6 – Rev 4/2014
EPGY Lesson Number – 15390.
3.6. Simple input and output
Due: Middle of week 4. Assignment #5
Change the addMul.c, so that the output of the program as a whole will be as follows:
(Run your program with 20 (for num1) and 10 (for num2).
The following is a sample run, assuming that the user enters 20 and 10 for num1 and
num2:
The program adds and multiplies two numbers.
Enter first number 20 (+enter)
Enter second number 10 (+enter)
The sum of 20 and 10 is: 30
The product of 20 and 10 is: 200
IMPORTANT !!
Note, that in order to solve this exercise you need to include the extended library
(genlib.h, simpio.h). So, please look for the instructions for including extended libraries
and follow them.
The file name should be addMul.c
_______________________________________________________________________
EPGY Lesson Number – 15400.
3. Summary: Fundamentals of C
Due: End of week 4. Assignment #6
Write a program that calculates the volume of a cube after reading the side’s value from
the user.
The inputs you should run your program with are: 3, 5, 10.
Run your program 3 times with each of the above inputs.
The output should be displayed in the following form:
For example, for the input 3:
The volume of a cube with side = 3 is 27
Exactly in the same form the output display should be designed for 5 and 10.
The file name should be: volume.c.
c11A - Programming Exercises
- 7 – Rev 4/2014
EPGY Lesson Number – 15400.
3. Summary: Fundamentals of C
Due: End of week 4. Assignment #7
Write a program that reads 4 grades (integers) from the user, computes their average, and
then displays on the output screen, the 4 grades as well as the average grade that was
computed.
The inputs you should run your program with are:
n
n
n
70 80 60 90
50 100 90 76
90 95 99 98
You should run your program 3 times, each with one of the above groups of numbers.
The output should be displayed in the following form:
For example for the first group of grades (70,80,60,90) the output display is:
Your grades were: 70 80 60 90
Your average grade is 75
Note that your program may produce the mathematically incorrect result of the
computation of the average grade, because of the use of integer division.
The file name should be: average4.c
c11A - Programming Exercises
- 8 – Rev 4/2014
Week 5, 6, 7, 8 - Control statements
________________________________________________________________________
Epgy Lesson Number – 15510.
5.1.2. If/else statements
Due: Middle of week 5. Assignment #8
Write a program that gets two integers and finds the maximum.
You should run your program 3 times with the following inputs:
A. 2,5
B. -4, 500
C. 70, 70
If the two numbers are equal, the program should print, for example:
The two numbers 70 and 70 are equal
The output display should be as follows. For example, for the first pair, (2,5):
The maximum number between 2 and 5 is 5
File name should be: max.c.
_______________________________________________________________________
Epgy Lesson Number – 15520.
5.1.3. Multiline if statements
Due: Middle of week 5. Assignment #9
Extend the program given in 5.1.1, the program that gets a number and checks if it is a
positive number.
It prints an appropriate message indicating if it’s positive or negative and also prints the
absolute value of the number.
Run your program twice for -7 and 45.
The output display should be as follows,
for -7,
The number -7 is negative and its absolute value is 7.
The file name should be: positive.c
c11A - Programming Exercises
- 9 – Rev 4/2014
Epgy Lesson Number – 15540.
5.3.1. Cascading if statements
Due: Middle of week 5. Assignment #10
Write a program that finds the minimum number among 3 integers.
The program should use minimal number of relational operations.
The output display should be as follows,
For example, if the input numbers are 2,5,9:
The minimum number between 2, 5 and 9 is 2
File name should be: min3.c.
_______________________________________________________________________
Epgy Lesson Number – 15560.
5.4.
The switch statement
Due: week 6. Assignment #11
Use the switch statement to rewrite the following program:
month=GetInteger();
if (month = =6)
printf (“June\n”);
else if (month = = 7)
printf (“July\n”);
else if (month = = 8)
printf (“August\n”);
else if (month >=1) && (month <= 12)
printf (“The month is not in the summer\n”);
else
printf (“Your input number is out of range\n”);
File name should be: summer.c.
c11A - Programming Exercises
- 10 – Rev 4/2014
Epgy Lesson Number – 15560.
5.5.
The switch statement (con't)
Due: week 6. Assignment #12
Write a program (using the switch statement) that gets a number from 0 to 3and translates
it to its binary code according to the following table:
000
101
210
311
The output display for 3, for example should be as follows,
The binary code of 3 is 11.
For numbers outside of the range, the program should display a message saying that it is
outside of the range. For example, if the input is 4, the output should be:
The input number you entered, 4, is not in range.
Run your program on the following inputs:
2, 7
File name should be: binary.c.
_______________________________________________________________________
Epgy Lesson Number – 15560.
5.6.
The switch statement (con't)
Due: week 6. Assignment #13
Write a program that gets a number between 1-7 and prints the corresponding day of the
week. Your program must use the switch statement.
The output display, for 5, for example, should be,
Thursday.
If the input number is not in range the program prints an error message. So, for 8, for
example, the output is,
The number you entered, 8, is not in range.
Run your program 3 times, with the following inputs: 3, 7, 19
File name should be: week.c
c11A - Programming Exercises
- 11 – Rev 4/2014
Epgy Lesson Number – 15580.
5.5.1. While loops – structure and operation
Due: Middle of week 7. Assignment #14
Write a program that executes multiplication of two integers entered by the user, by using
ONLY the addition operation.
So, for example if the inputs are 3 and 4, the program will execute 3 times addition of 4:
4+4+4.
Run your program 3 times with the following inputs:
6, 3
48, 5
3, 7
The output of the program for the first pair, for example, should be,
The result of 6*3 is 18.
Try to solve this problem in the most efficient way you can. (So, if you need to multiply
48 by 5, use the addition operation to add 48 to itself 5 times and not the opposite way
round).
File name should be: mulByAdd.c.
c11A - Programming Exercises
- 12 – Rev 4/2014
Epgy Lesson Number – 15600.
5.5. The while statement
5.5.3. Using sentinel values
Due: End of week 7. Assignment #15
Write a program that gets a list of integers from the user and computes the minimal and
maximal number appearing in the list.
Run your program twice on the following inputs:
A. 570, 6, 94, 76, 2, 0 (0 sentinel value)
B. 78, 50, 4, 9, 0
The output for A, for example should be:
The maximum number in the list is 570.
The minimum number in the list is 2.
File name should be: minMax.c
Due: End of week 7. Assignment #16
Write a program that computes the power of a number by using the multiplication
operation.
The program should ask the user to enter the base and the power and then the program
computes and displays the result.
Run your program 3 times on the following inputs,
base
power
2
4
9
2
10
4
The output for the first pair,
2 to the power 4 is: 16.
File name should be power.c.
c11A - Programming Exercises
- 13 – Rev 4/2014
Epgy Lesson Number – 15630.
5.6.1. The for loops – Structure and operation
Due: Middle of week 8. Assignment #17
Write a program that computes and displays all the squares of the numbers from 1 to 50.
The output should be of the following form,
1
1
2
4
…
50
2500
File name should be: square50.c.
Epgy Lesson Number – 15640.
5.6.2. The for loops – Nested for loops
Due: Middle of week 8. Assignment #18
Write a program that displays a square of stars,
The output should be of the following form,
********
********
********
********
********
********
********
********
File name should be: stars8.c.
c11A - Programming Exercises
- 14 – Rev 4/2014
Epgy Lesson Number – 15670.
Summary - 5. Control statements
Due: End of week 8. Assignment #19
Write a program that displays all the prime numbers between 50 and 100.
A prime number divides only in 1 and in itself.
Note: You can make it more efficient by checking the divisors of N from 2 and upto
square root(N), instead of (N)).
There is a library function called sqrt(n) which is included in the <math.h> header file.
sqrt(n) returns the square root of n.
File name should be: prime.c.
Due: End of week 8. Assignment #20
Write a program that displays a reversed number.
So for example, for the input 175 the output will be, 571.
Run your program on the following inputs:
A. 5743
B. 248
C. 67
File name should be: reverse.c.
c11A - Programming Exercises
- 15 – Rev 4/2014
Week 10, 11, 12
________________________________________________________________________
Some more data types, constants, algorithms and output displays
Epgy Lesson Number – 15800.
8.1 Constants
Due: Middle of week 10 Assignment #21
Write a program, using constants, that computes and displays a table of the factorials of
the numbers in the range of 1-10.
The factorial of n = n! = 1*2*3…. (n-1)*n
So, 3!= 1*2*3=6
File name should be: factorial.c
IMPORTANT
Please use the long data type instead of int with the letters ‘lu’ (%lu) for the format code
of printf, otherwise the results of factorial for the larger numbers (9,10) may produce
wrong results. The format code ‘lu’ stands for unsigned long, meaning that it can hold
values of positive larger integers .
The reason for this is that in many machines the maximum value you can hold in
variables of type int is 32767 where as long enables to hold much larger values.
________________________________________________________________________
Epgy Lesson Number – 15810.
8.2. The floating point data type
Due: End of week 10. Assignment #22
Write a program that displays the table of Fahrenheit - Celsius temperatures.
The formula is: C = (5/9) (F-32)
C - Celsius, F- Fahrenheit
The range should be: 0 - 200, where the step should be set to 20.
The output should be:
Fahrenheit
Celsius
0
-17.8
20
-6.7
40
4.4
……
200…..
Define the floating point variables for the Celsius to be of type float and use the %f
format code.
Note that the sequence %.1f in printf indicates that the floating point should be displayed
with 1 character after decimal point. And %.4f - indicates a display of floating-point
with 4 characters after the decimal point.
File name should be: celsFahr.c
c11A - Programming Exercises
- 16 – Rev 4/2014
Epgy Lesson Number – 15820.
8.3. 1. The quadratic equation problem
Due: Middle of week 11. Assignment #23
For the quadratic equation problem, complete the 4th phase: Write the C program
according to the algorithm specified in the lesson.
Run your program on the following inputs (a, b, and c):
n 3,5,2
n 3,6,3
n 0,3,-6
n 4, 5, 2
File name should be: quad.c
________________________________________________________________________
Epgy Lesson Number – 15830.
8.3.2.1. GCD - The brute-force way
Due: Middle of week 11. Assignment #24
Write a C program to solve the GCD (Greater Common Divisor), according to the bruteforce way. Run your program on the following pairs,
49,35
37,11
180,60
The form of your output should be the following,
For example for the 1st pair,
The GCD of 49 and 35 is 7
File name should be: GCD1.c
_______________________________________________________________________
Epgy Lesson Number – 15840.
8.3.2.2. GCD- The Euclid algorithm
Due: Middle of week 11. Assignment #25
Write the detailed algorithm and the C program to follow this algorithm.
Run your program on the following pairs,
49,35
37,11
180,60
The form of your output should be the following, for example for the 1st pair,
The GCD of 49 and 35 is 7
File name should be, euclid.c.
c11A - Programming Exercises
- 17 – Rev 4/2014
Epgy Lesson Number – 15860.
8.4. 1.The string data type – definition
Due: Middle of week 12. Assignment #26
Write a program that computes the volume of either a cube, cylinder or a box.
The user enters the shape he wants, namely one of the 3 strings: box, cylinder or cube.
Then the program asks the user to enter the appropriate values (such as the side of the
cube or the radius and height of the cylinder, or the 2 sides and the height of the box), and
the program displays the corresponding volume.
So, for example for a cube with the side 4, the output should be,
The volume of a cube with side=4, is: 64.
The formulas for the volumes are:
For a cube with side s, volume = s3
namely, s* s *s
For a box with sides s1 and s2, and height h, volume = s1 * s2 * h
For a cylinder with radius r, and height h, volume = pi * r2 * h (use pi=3.14)
Run your program on the following inputs:
cube, side=5
cylinder, radius=2, height=3
box, width=2 ,length=3, height=4.
Note that when you enter an input string to be read by GetLine there is no need to add
quotation marks!
Hint
In order to compare two strings, such as str1 and str2 you cannot use the == operator. For this purpose you
should use instead the function, StringEqual, that was presented in the lecture.
File name should be: volumes.c
c11A - Programming Exercises
- 18 – Rev 4/2014
Epgy Lesson Number – 15870.
8.4.2. Operations with strings
Due: Middle of week 12. Assignment #27
Write a program that reads a list of strings until the word “end” appears, and returns the
longest string that was included in the input list.
So, for example if the input is:
“The”, “longest”, “string”, “is”, “funny”, “end”
The output will be:
“The longest string is: longest”
If there are several strings of the same length, return the first one.
Run your program on the following input:
“What”, “kind”, “of”, “program”, “is”, “this”, “end”
File name should be: longStr.c.
________________________________________________________________________
Epgy Lesson Number – 15880.
8.5. Controlling the output displays
Due: End of week 12. Assignment #28
Write a program that displays the scores of the basket balls games for 1 season.
The table should include the following information (in separate columns),
name of the basket ball league,
number of games played in the season by the group,
number of winning games,
percent of winning games (with respect to the total games played).
The inputs do not have to be accurate (from real life)!
You have to include at least 4 different rows in the table, namely 4 different leagues .
You can read the inputs from the user or build the table in the program by assigning
values to the variables – as you wish.
However, the percentage needs to be computed within the program!
File name should be: games.c.
c11A - Programming Exercises
- 19 – Rev 4/2014
© Copyright 2026 Paperzz