Programming Lab1
Programming Lab1
PROGRAMMING FUNDAMENTAL
NAME OF GROUP
MEMBERS & 2.
MATRIX NUMBER
3.
COURSE BETC1313
DATE 26/2/2018
TOTAL MARKS
1
1.0 OBJECTIVES
2.0 EQUIPMENT
This Lab is concerned with the basic elements used to construct C elements. These elements
includes the C character set, identifiers, keywords, data types, constants, variables, expressions
statements and escape sequences.
Comments:
Comments statements will not to be compiled. Comments are simply the statements to improve
program readability and to document program properly. Comments begins with /* and end with */,
text is placed in between them.
/* Lab Session 1 */
printf() Function:
This function is used to output combination of numerical values, single character and strings
Syntax:-
printf ( fomat specifier , variable or constant);
printf( text );
Examples:-
printf( Area of circle is %f sqmm ,3.756);
scanf() Function:
The purpose of scanf() function is to accept data from keyboard, and place that data to a
memory location specified in its argument.
Syntax:-
scanf( format specifiers , address of variable);
Examples:-
scanf( %d , &r);
2
Escape Sequences:
These are non-printing characters. They are special character set, each with specific meaning. An
escape sequence always begins with a back slash and is followed by one or more special
characters.
Variables:
A variable name is a location in memory where a value can be stored for use by a program. All
variables must be defined with a name and a data type in the code before they can be used in a
program.
There are certain reserved words called Keywords that have standard, predefined meanings in C.
These keywords can be used only for their intended purpose; they can t be used as programmer
defined identifier.
Data Types:
C supports several different types of data, each of which may be represented differently within the
computer s memory. The basic data types are listed below.
3
Format Specifiers:
Format specifier specifies that which type of data has to be print or read into. Following is a list of
different format specifiers.
Specifiers Meaning
%c Character
%d Integer
%f Float value
%e Float value in exponential form
%u Unsigned Integer
%x Hexadecimal integer (unsigned)
%o Octal value
%s String
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r; /* Declares a variable of type integer */
float area; /* Declares a variable of type float */
float Pi=3.14; /*Initializing a variable of type float */
printf( "\t\t\tEnter the radius of circle: "); /*Output the string on the
screen*/
scanf( "%d" , &r); /*Stores the value on the address of variable r*/
area=Pi*r*r; /*Evaluating area and assigning the value to variable area*/
printf( "\n\n\t\t\tArea of Circle is :%0.3f ",area);
}
Even though this program is simple, it illustrates several important features of C language.
Output:
4
4.0 PROCEDURES.
a) scanf( d ,value)
scanf(“%d”,&value);
b) Read an integer from the keyboard and store the value entered in the variable a.
Your program should also display the valued stored in variable a.
5
c) Read two integers simultaneously from the keyboard and store the value entered in the
variable a & b.
Your program should also display the valued stored in variable a and b.
3. Write a complete C statements below to demonstrate what do actually these codes print?
Save your codes and print screen your output console as a result.
a) printf(“\n*\n**\n***\n****\n*****”);
6
b) printf(“This is\base“);
c) printf(“\n\t\t\t1\n\t\t2\n\t3\n4\n\t5\n\t\t6\n\t\t\t7“ );
4. You are asked to build a simple program that can find the average of 3 integers. Those 3
integers are input by user. Your average value should display the floating point, rounded to 2
decimal places.
Do provide the flowchart, showing the sequence reflecting your program flow.
Save your code and print screen your output console as a result.
7
5.0 PROBLEM SOLVING
Write a C program to prompt the user to input 3 integer values and print these values in forward
and reversed order, as shown below.
8
6.0 RESULTS
7.0 DISCUSSION
Kindly discuss on the lab’s result and finding in section 4.0 and 5.0.
Scanf() function is to accept data from keyboard, and place that data to a memory location
specified in its argument. Output will show error if false format specifiers and address of
variable is inserted and program cant be excuted properly.Printf() need a correct format
specifier and constant so that the program can be excuted properly.Every function also
need to end with semicolon.We can set the our program to be displayed in our desired
variable by changing the scanf() adress of variable.Besides that , \n is used to make a new
line at the output of the program.\t is needed to use when whe want to make the output in
horizontal tab. We need to use double or float data type when want the output digits to be
more precie.Finally,we can arrange the variables so that the output will be arranged as our
wish.
8.0 CONCLUSION
Conclude what you have learned in this lab session.
From this experiment , I had familiarise with C Integrated Development Environment. Besides
that, I also have learned to study basic building blocks of C language such as input – output
format specifier. I also learn to manipulate the different data type of variables and constants.
9
10