C Lab Manual 1 To 3

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

LAB MANUAL

COMPUTER SCIENCE AND ENGINEERING


2nd SEMESTER
SUBJECT :- CONCEPTS OF PROGRAMMING USING
C

LIST OF PRACTICALS :-
1. Programming exercises on executing and editing a C program.

2. Programming exercises on defining variables and assigning values to variables.

3. Programming exercises on arithmetic, logical and relational operators.

4. Programming exercises on arithmetic expressions and their evaluation.

5. Programming exercises on formatting input/output using printf and scanf and


their return type values.

6. Programming exercises using if statement.

7. Programming exercises using if – Else.

8. Programming exercises on switch statement.

9. Programming exercises on while and do – while statement.

10. Programming exercises on for – statement.

11. Simple programs using functions and recursive function.

12. Programs on one-dimensional array.

13. Programs on two-dimensional array.

14. (i) Programs for concatenation two strings together.

(ii) Programs for comparing two strings.

15. Simple programs using pointers.

16. Simple programs using structures.

17. Simple programs using union.

18. Simple programs for File Handling


PRACTICAL :-1

Object :- Programming exercises on executing and editing a C program.


Theory :- To perform this practical open the Dev C++ and code as described :
Code :-
#include<stdio.h>
int main ()
{
float kg,g;
printf("Enter the weight in kilogram to convert into grams :\n");
scanf("%f",&kg);
g = kg * 1000;
printf("Equivalent weight in gram = %f",g);
return 0;
}

The output will be :-


Enter the weight in kilogram to convert into grams :

15

Equivalent weight in gram = 15000.000000

Note :-
 <stdio.h> stands of Standard Input Output.
 ‘printf’ is used for output operation.
 ‘scanf’ is used for input operation.
 \n is used for start the new line.
 Here we are taking the value of kilogram by the user, we can also predefine
the value of kilogram.
 We can also use ‘int’ data type, in that case we can’t get or give the floating
values to the system.
 If we use ‘int’ data type in that case we must have replace the ‘%f’ with the
‘%d’ to run the program successfully
PRACTICAL :-2

Object :- Programming exercises on defining variables and assigning values to


variables.

Theory :-
Naming a variable :-
A variable name can be of anything we want to call out variable. Yet there are
specific rules we must follow while naming a variable :

 A variable name can contain alphabets, digits and underscore ( _ ) only.


 The starting letter can’t be a digit.
 White spaces can’t be used.
 The name should not be Reserved Keyword or Special character.

We can declare and assign to a variable in two ways.

1st Way : int a = 18;

2nd Way : int a;

a = 18;

Code :-
#include<stdio.h>
int main ()
{
int a = 12.2221;
printf("Output = %d",a);
return 0;
}

The Output will be :-


Output = 12

Declaration :-

We can’t declare a variable without specifying its data type. The data type of a
variable depends on what we want to store in the variable and how much space we
want it to hold.
PRACTICAL :-3

Object :- Programming exercises on arithmetic, logical and relational operators.


Theory :-
“Special symbols that are used to perform action or operations are known as
operators.”

For example, the symbol plus ( + ) is used to perform addition so it is an operator.

Arithmetic Operators :-
Arithmetic operators are used to perform mathematical operations such as addition,
subtraction, etc.

 Arithmetic operators :- +, -, *, /, %

Code :-
#include<stdio.h>
int main ()
{
int a = 5;
int b = 6;
printf("a + b = %d\n",a+b);
return 0;
}

The output will be :-


a + b = 11

Logical Operators :-
Logical operators are used for performing logical operations. Logical operators
work with Boolean expressions and return a Boolean value. Logical operators are
used with the conditional operators in loops and decision-making statements.

 Logical operators :- &&, ||, !


Code :-
#include<stdio.h>
int main ()
{
int a = 10;
int b = 5;
printf("(a == b) && (a>b) = %d\n",(a == b) && (a>b));
return 0;
}

The output will be :-


(a == b) && (a>b) = 0

Relational Operators :-
Relational operators are used for the comparison between two or more numbers.
Same as Java, C has also six relational operators and their value is in Boolean i.e.
either True or False ( 1 or 0 ).

 Relational operators :- <, <=, >, >=, ==

Code :-
#include<stdio.h>
int main ()
{
int a = 2;
int b = 2;
printf("a == b = %d\n",a==b);
return 0;
}

The output will be :-


a == b = 1

 The output is 1 i.e. True.


 If we change the value of a or b the value will be false or 0.

You might also like