Documente Academic
Documente Profesional
Documente Cultură
OBJECTIVES:
INTRODUCTION:
Printf (and a few related functions) use the first string as a template in which specifiers
are replaced by formatted values taken from the other parameters.
Specifiers
Specifier Meaning
%c print a character
%d print an integer in decimal format
%e print a float in exponential format
%f print a float in fixed point format
%g print like %f or %e, whichever uses fewer characters
%o print an integer in octal format
%s print a character string
%u print an unsigned number
%x print in hexadecimal format
1. Write, compile and execute a program that will display the sum of digits from 1 to 100
using for loop statement.
2. Record the output of your program.
3. Display the value of the output variable in the for loop statement per step.
4. Encode the program shown below.
#include <stdio.h>
int main(void)
{
int i, j, k;
i = 0;
j = i++;
k = ++i;
printf("i = %d\tj = %d\tk = %d\n", i, j, k);
getch();
return(0);
}
5. Explain the output of the program.
6. Differentiate the function of statements 3 and 4.
CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 4
CHARACTER INPUT/OUTPUT
OBJECTIVES:
return (0);
}
But it will not accept the character or display it until the Return or Enter key is pressed.
PROCEDURE:
CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 5
PROCEDURE:
2. Compile the program and execute it. What is the output of the program?
3. How does the if condition work in the program?
4. What is the role of the while statement?
5. Create a new blank file in C compiler and include the following commands.
#include <stdio.h>
#define YES 1
#define NO 0
int main(void)
{
int c, nl, nw, nc, inword;
inword = NO;
nl = nw = nc= 0;
CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 6
ARRAYS
OBJECTIVES:
PROCEDURE:
1. What is the function of the program shown below?
#include <stdio.h>
#define SIZE 20
int main(void)
{
int i, n, x[SIZE];
float sum, average;
printf("Enter the number of values\t?");
scanf("%d", &n);
sum = 0;
for (i = 0; i < n; i++) {
printf("Enter value #%d\t?", i);
scanf("%d", &x[i]);
sum += x[i];
}
average = sum / n;
printf("The average is %f\n", average);
for (i = 0; i < n; i++)
printf("x[%d] = %d\n", i, x[i]);
return(0);
}
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 7
FUNCTIONS
OBJECTIVES:
INTRODUCTION:
PROCEDURE:
2. Observe the output of the program and describe how the function was used in the
program.
3. Describe the significance of using a function.
4. Create a program that computes the area of a circle, triangle, rectangle and square using
four functions.
CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 8
PASSING PARAMETERS
OBJECTIVES:
INTRODUCTION:
C uses call by value. The function gets a copy of the original values.
Any changes made in the function to the parameters' values are never seen by the
function that did the calling.
PROCEDURE:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.
Experiment 9
STRINGS
OBJECTIVES:
PROCEDURE:
1. What is the total number of memory cells required to store n-characters of data?
2. What is the syntax for string declaration capable of holding 100 characters?
3. Compile and write the output of the following program.
#include <stdio.h>
#define MAXLINE 1000;
int getline(char s[], int lim);
void copy(char s1[], char s2[]);
int main(void)
{
int len;
int max;
char line[MAXLINE];
char save[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy (line, save);
}
if (max > 0)
printf("%s", save);
return(0);
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1&& (c = getchar()) != EOF && c != '\n'; )
s[i++] = c;
if (c == '\n')
s[i++] =c;
s[i] = '\0';
return(i);
}
void copy(char s1[], char s2[])
{
int i = 0;
while ((s2[i] = s1[i]) != '\0')
i++;
}
CONCLUSION:
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
__________________________________________________________.