Badr University in Assiut
å
School of Artificial Intelligence & Data Management
Computer2 – Practice Sheet No. 2
Objectives & Outcomes
Using print statement.
Using the Escape Sequence to format the Displayed Text.
Pre-knowledge Write Comments a Single line and Multiple lines.
Declaring & initializing variables
Format Specifiers and Data Types
Apply the know C print statement till now.
Learn the Escape Sequence (\n,\t,\\,\’,\”).
Objectives
Apply Comments for Single line and Multiple lines.
Apply Variables
Outcomes Write simple C programs.
Illustrative Examples
Example Displaying Messages on Screen
Prompt: Write a program that uses printf to display "Hello World!" on the
screen.
Write a single C statement to accomplish: Prompt the user to enter an
integer. End your prompting message with a colon (:) followed by a space
2
and leave the cursor positioned after the space.
ANS: printf( "Enter an integer: ");
3
Example on basic escape sequences in C , such as newline, tab, and quotes
\n
nn
nn
n
Badr University in Assiut
å
School of Artificial Intelligence & Data Management
: Moves the cursor to the next line.
\t : Adds a horizontal tab space.
nn
nn
\\ : Prints a backslash.
nnn
nn
\” : Prints double quotes.
n
nn
nn
n
Example both single-line and multi-line comments in C.
// to Comment a Single line
//This prints a greeting message
/*
Mulitple lines
*/ to Comment multiple lines
/*
This is a multi-line comment.
It can span across multiple lines.
4
The following line prints another message.
*/
Badr University in Assiut
å
School of Artificial Intelligence & Data Management
Examples Variables
Format Specifiers and Data Types
Assignments
Fill in the blanks in each of the following:
a- Every C program begins execution at the function………..
1
b- b- C statements should ends with……………
State whether each of the following is true or false. If false, explain why:
2 a- Comments cause the computer to print the text after the // on the screen when
the program is executed ( )
Badr University in Assiut
å
School of Artificial Intelligence & Data Management
b- b- The escape sequence \n, when output with printf and the stream insertion
operator,causes the cursor to position to the beginning of the next line on the
screen ()
c- c- All variables must be declared before they’re used.()
Prompt: Write a C program that prints your name, age, and country in separate lines
Name: [Your Name]
Age: [Your Age]
Country: [Your Country]
3
Expected Output:
Name: Alice Smith
Age: 30
Country: Canada
Write a single C statement to accomplish each of the following:
a- Prompt the user to enter an integer. End your prompting message with a colon
4 (:) followed by space and move the cursor positioned to the next line.
b- Print the message "This is a C program" on two lines. End the first line with C.
c- Assign the product of variables b and c to variable a.
Write a program that generates the following table:
1990 135
1991 7290
5
1992 11300
1993 16200
Use a single print statement for all output
Write a C program to print the following shape.
*
***
6
*****
*******
Prompt: Define variables for your name, age, and country. Use printf to display them.
Expected Output:
7 Name: Alice Smith
Age: 30
Country: Canada
Badr University in Assiut
å
School of Artificial Intelligence & Data Management
Write a C program that demonstrates variable usage and performs basic arithmetic
8
operations (addition, subtraction, multiplication, and division) without using user input
كود الطالب اسم الطالب
Comments Grades