Nawaraj

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

UNIGLOBE SECONDARY SCHOOL

Kamaladi Ganesthan, Kathmandu

LAB REPORT 5 : C PROGRAMMING

(COMPUTER SCIENCE)

SUBMITTED BY: SUBMITTED TO:

NAME: Nawaraj Basnet

GRADE: XI J1 Science

DATE : 11th March 2023 (Mr. Sanjog Bhandari)

KATHMANDU, NEPAL
2023
ACKNOWLEDGEMENT

I would like to express my deep appreciation and gratitude to my subject teacher and lecturer
Mr. Sanjog Sir for their coinstantaneous help, advice, information and encouragement in this
project

I feel immense pleasure to present my project after a long work. Besides my effort, the help
and guideline given by many others hasn’t been unnoticed. I express my gratitude to all those
countless people for the support for me in doing this project.

I express my thanks to Uniglobe Secondary School / College for it has been a source of the
creation of this project and the support, valuable information, resources and guidance give to
me to do this project.

I am also grateful and indebt of my beloved friends for their immeasurable help, support and
encouragement from the beginning to the
End of the project without whom this project would not have been a reality.

Last but not the least I would like to thank my parents, family members, friends, this College
and other who help me for their guidance
And support.

Nawaraj Basnet
Objective

Objective is a specific result that a person, a system aims to achieve within a time

frame and with avid able resources. In general, objectives are more specific and

easier to measure than goals. Objectives are basic tools that underline all

planning and strategic activities. They serve as a basic forecasting policy and

evaluating performance.

The main objectives of the project are as follows;

1. To improve skill in C- programing

2. To improve knowledge in C- programing

3. Too familiar with C- programing

4. To improve skill in making project report.


Lab Report 5:
C-Programming
Background Theory

Q1. What is Looping? Write different types of looping statements.


- Looping is the process of executing the same program statement or block
of program statements repeatedly for specified number of times or until
the given condition is satisfied.
There are three types of looping statements in C. They are:

I) ‘for’ loop

II) ‘while’ loop

III)’do while’ loop

Q2. What is ‘for’ loop? Explain with examples.


- ‘for’ loop is the most common type of loop which is used to execute
program statements or block of program statements repeatedly for
specified number of times. It is a definite loop. Mainly it consists of three
expressions: initialization, condition and increment/decrement.
- Its syntax is:
for(initialization; condition; Increment/decrement)
{
statements;
}

Example:
#include<stdio.h>

Void main()

/* Program to calculate and display factorial of 5 */

int i, fact=1;

for(i=1;i<=5;i++)

fact=fact*i;

Printf(“Factorial of 5 is= %d”,fact);

Q3. What is 'while' loop? Explain with examples.


- ‘while’ loop is a looping statement which executes the program
statements repeatedly until the given condition is true. It checks condition
at first; if it is found true then it executes the statements written in its
body part otherwise it just gets out from the loop structure. It is also
known as entry control or pre-test loop.
- Its syntax is:
initialization;
while(condition)
{
statements;
increment/decrement;
}

Example:
#include<stdio.h>

Void main()

/* Program to display number from 1 to 10 */

int i=1; //initialization


while(i<=10) //condition
{
printf(“%d\t”,i);
}
}

Q4. What is 'do while’ loop? Explain with examples.


- ‘do while’ loop is the looping statement which also executes program
statements repeatedly until the given condition is true. It executes the
program statements once at first then only the condition is checked. If
condition is found true then it executes the program statements again,
otherwise it gets out from the loop structure. As it checks condition at last
it is also known as post-test or exit control loop.
- Its syntax is:
initialization;
do
{
statements;
increment/decrement;
}
while(condition);

Example:
#include<stdio.h>
void main()
{
/* program to display multiplication table of 5 */
int i=1; //initialization
int p;
do
{
p=5*i;
printf(“\n6*%d = %d”,I,p);
i++; //increment
} while(i<=10); //condition
}
Q5. What is array? Explain different types of array.
- An array is a collection of similar types of data items treated as a single
unit. It acts to store related data under the same name with an index, also
known as a subscript which helps to access individual array element.
- There are two types of arrays which are as follows:

I. One Dimensional Array:


An array which has one only subscript is named as one-
dimensional array, a subscript is a number of large brackets in which we
put the size of the array. Like any other variable, we must declare array
before they are used. The general form of one dimensional array
declaration is,
Syntax:

data_type array_name [array_size];

II. Multi Dimensional Array

An array which has more than one subscript is named as multi


dimensional array, subscripts define each dimension of the array. Thus, a
two dimensional array will require two square brackets, one for row size
and the other for column size.
In two dimensional array declaration we write,
Syntax:
data_type array_name[row_size][column_size];
Q6. Write differences between one-dimensional array and two
dimensional array.
-
Q7. What is string function? Explain the use of strlwr(), strupr(),
strlen(), strcat(), strrev(), strcmp() and strcpy() function with
appropriate examples.
- The C library supports many string handling functions that can be used to
carry out many of the string manipulation. String is defined as an array of
characters which contains characters, symbols, numbers etc.
The uses of function are:
strlwr()- To convert upper case characters of strings into lower case
characters.
Example:

#include<stdio.h>
#include<string.h>
void main()
{
char str[49];
printf(“Enter a string:”);
gets(str);
printf(“The string in lower-case is: %s”,strlwr(str));

strupr()- To convert lower case characters of strings into upper case characters.
Example:

#include<stdio.h>
#include<string.h>
void main()
{
char str[49];
printf(“Enter a string:”);
gets(str);
printf(“The string in lower-case is: %s”,strupr(str));
strlen()- To return the length of a string which takes the string name as an
argument.

Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str[50];
int l;
printf(“Enter a string:”);
gets(str);
l=strlen(str);
printf(“The length of the string is: %d”, l);
}

strcat()- To join or concatenate one string into other string. It takes two strings
and second string is concatenated over first string.

Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[60];

printf(“Enter first string:”);


gets(str1);
printf(“Enter second string:”);
gets(str2);
strcat(str1,str2);

Printf(“After concatenating the two strings is %s”,str1);


}
strrev()- To reverse the given string

Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str;
printf(“Enter the string:”);
gets(str);
strrev(str);
printf(“The reverse of given string is %s, str);
}

strcmp()- To compare two strings, character by character and stops


comparison when there is a difference in the ASCII value or the end of
any one string and returns ASCII value of the characters that is integer
which is either zero, positive value or negative value. If the value os zero
both strings are same. If the value is positive first string is larger than
second one otherwise first string is smaller than second one.
Example: #include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
int v;
printf(“Enter first string:”);
gets(str1);
printf(“Enter second string:”);
gets(str2);
v=strcmp(str1,str2:
if(v==0)
printf(“Both strings are same.”);
else if(v>0)
printf(“%s comes after %s”,str1,str2);
else
printf(“%s comes before %s”,str1, str2);

}
strcpy()- To copy one string into another string.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char str1[50],str2[50];
printf(“Enter first string:”);
gets(str1);
strcpy(str2,str1);
printf(“After copying the string is %s.”,str2);
}
Q8. Write differences between for loop and while loop

-
Q9. Write differences between while loop and do while loop

You might also like