Alexandria University Computer and Communication
Faculty of Engineering CSE016 Computers &
Specialized Scientific Programs Programming
Lab 2
Objective:
1. Defining variables
2. Knowing the difference between %d, %f, %c
3. Printing the value of a variable using printf
4. Using scanf for reading values from user
5. Simple mathematical operations ( +, - , *, / )
6. Knowing how integer division works in C
1. Write a program that asks the user to enter two integer numbers obtains them from the user
and prints their sum, product, difference, quotient and remainder.
#include <stdio.h>
int main ( )
{
int x, y, sum, product, difference, quotient, remainder;
printf( "Enter first number: ");
scanf( "%d", &x);
printf( "Enter second number: ");
scanf( "%d", &y);
sum = x + y;
product = x * y;
difference = x – y;
quotient = x / y;
remainder = x % y;
printf( "The sum is %d\n", sum);
printf( "The product is %d\n", product );
printf( "The difference is %d\n", difference );
printf( "The quotient is %d\n", quotient );
printf( "The remainder is %d\n", remainder );
return 0;
}
Alexandria University Computer and Communication
Faculty of Engineering CSE016 Computers &
Specialized Scientific Programs Programming
Take home problems
2. John is responsible for planting the street with trees; he can give you the length of the
street in meter, the distance between each two trees in meter, and the cost of planting each
tree in dollar. Write a program that should read this information and then print the number
of trees needed and the total cost.
Alexandria University Computer and Communication
Faculty of Engineering CSE016 Computers &
Specialized Scientific Programs Programming
3. Write a program that calculates the squares and cubes of the numbers from 0 to 10 and
uses tabs to print the following table of values:
Number Square Cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
Alexandria University Computer and Communication
Faculty of Engineering CSE016 Computers &
Specialized Scientific Programs Programming
4. Write a program that reads three integers and prints the average of the integers.
Please read the following list of common errors:
1. If you opened a curly bracket { then don’t forget to close it at the correct place }
2. Not putting ; at the end of every statement you wrote except for int main ()
3. Your program logic goes right after the first { and ends before return 0;
4. Forgetting one or both of the double quotes (“ ”) surrounding the format control string in
a printf or scanf
5. Placing variable definitions among executable statements causes syntax errors
6. A calculation in an assignment statement must be on the right side of the = operator. It is
a compilation error to place a calculation on the left side of an assignment operator
7. Forgetting the % in a conversion specification in the format control string of a printf or
Scanf
8. Placing an escape sequence “format char” such as \n outside the format control string
“” of a printf or scanf
9. Forgetting to include the expressions whose values are to be printed in a printf
containing conversion specifiers
10. Not providing a conversion specifier when one is needed in a printf format control
string to print the value of an expression
Alexandria University Computer and Communication
Faculty of Engineering CSE016 Computers &
Specialized Scientific Programs Programming
11. Placing inside the format control string the comma (,) that is supposed to separate the
format control string from the expressions to be printed
12. Using the incorrect format conversion specifier when reading data with scanf
13. Forgetting to precede a variable in a scanf statement with an ampersand (&) when that
variable should, in fact, be preceded by an ampersand
14. Preceding a variable included in a printf statement with an ampersand (&) when, in
fact, that variable should not be preceded by an ampersand
15. An attempt to divide by zero is normally undefined on computer systems and generally
results in a fatal error, i.e., an error that causes the program to terminate immediately
without having successfully performed its jobs. Nonfatal errors allow programs to run to
completion, often producing incorrect results.
16. If your program won’t run, check the red flag on the right and read the description of the
error in the output panel below
17. Be careful with scanf, you can’t leave a space or putting \n at the end of format control
string. But the following is correct:
scanf("%d%d%d", &x, &y, &z);
And you can enter their values by either leaving space between each int, or by pressing
Enter after each int
Good Luck