0% found this document useful (0 votes)
3 views

C Programming Lab Student Manual

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Programming Lab Student Manual

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

U24CS101 Programming in C STUDENT LAB MANUAL

EX NO:1-a) IMPLEMENTATION OF AREA OF SHAPES

AIM:
To write a C program to calculate the areas of different shapes, namely circle,
rectangle and triangle using Input and Output Statements.

OBJECTIVES:
● To understand the concept of input output statements such as formatted and
unformatted.
DESCRIPTION(Mapping to Theory):
The C programming language is equipped with built-in functions,
specifically printf() and scanf() which are used for performing formatted output
and input operations respectively. These functions are termed as 'formatted'
because they allow data to be inputted or outputted in a specific, predetermined
format.
Formatted I/O functions
The formatted functions accept various format specification strings along
with a list of variables in the form of parameters. This format specification string
refers to a character string used for specifying the data type of every variable
present as the output or input along with the width and size of the I/O.The C
program performs input and output operations using different input and output
functions.
printf():The printf function is used to print formatted output to the console.

Syntax: printf(“control string”,variable list);

Control String: A format string that specifies how subsequent arguments (if any)
are converted for output.Additional arguments to be formatted and printed.

scanf():The scanf function is used to read formatted input from the console.

Syntax:scanf(“control string”,arg 1,arg 2,arg 3,.....argn);


Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Unformatted I/O Functions:

These functions are used exclusively for character data types or character
arrays/strings. They are designed for reading single inputs from the user at the
console and for displaying values on the console.They are referred to as
“unformatted” I/O functions because they do not support format specifiers. Unlike
formatted I/O functions like printf() and scanf(), you cannot use format specifiers
to control the formatting of the data. They display or read data as-is without
formatting options.

The following are unformatted I/O functions

1. getch()
2. getchar()
3. putchar()
4. gets()
5. puts()
6. putch()

getch(): In C, getch() reads a single character from the keyboard without


displaying it on the console screen. It immediately returns without requiring the
user to press the Enter key. This function is declared in the conio.h header file and
is often used for controlling screen display.

Syntax: getch(); (or) variable-name = getch()

getchar():In C, getchar() reads a single character from the keyboard and waits
until the Enter key is pressed. It processes one character at a time. This function is
declared in the stdio.h header file

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Syntax: Variable-name = getchar();

putchar():In C, putchar() is used to display a single character at a time, either by


passing the character directly or by using a variable that stores the character. This
function is declared in the stdio.h header file.

Syntax: putchar(variable_name);

gets(): In C, gets() reads a group of characters or strings from the keyboard, and
these characters are stored in a character array. It allows you to input space-
separated texts or strings. This function is declared in the stdio.h header file.
However, please note that gets() is considered unsafe due to the risk of buffer
overflow and is generally discouraged in favor of safer alternatives like fgets().

Syntax: char str[length of string in number];

Example:gets(str);

puts():In C programming, puts() is used to display a group of characters or strings


that are already stored in a character array. This function is declared in the stdio.h
header file.

Syntax: puts(identifier_name );

putch(): In C, putch() is used to display a single character provided by the user,


and it prints the character at the current cursor location. This function is declared in
the conio.h header file

Syntax: putch(variable_name);

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

SOFTWARE REQUIRED:

TurboC or any Equivalent C compiler

ALGORITHM:

PSEUDOCODE:

BEGIN
INPUT length, breadth,radius,base, height
SET a1 = length * breadth
SET a2 = 0.5 * base * height
SET a3 = 3.14 * radius * radius
OUTPUT a1
OUTPUT a2

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

OUTPUT a3
END
FLOWCHART:

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

REAL TIME APPLICATIONS:

1. Real-Time Data Processing

Application: Real-time data processing involves analyzing and reacting to data as it


arrives. This is crucial in applications where timely responses are necessary.

2. Graphics and Games:

Application:C language has been used in the development of a variety of graphics


and gaming applications, such as chess, bouncing ball, archery etc.

VIVA QUESTIONS
1. How do you calculate the area of different shapes?

2.Give the syntax for printf() and scanf()?

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

3.How to take input and output of basic types in C?

4.Which are the types of formatted input and output?

RESULT:
Hence, the C-program to calculate the areas of different shapes was successfully
executed.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

EX NO:1-b) GREATEST OF THREE NUMBERS

AIM:
To write a C program of Greatest of three numbers using Operators and
Conditional statements.

OBJECTIVES:

To understand the concept of different types of binary as well as unary


operators and conditional statements.

DESCRIPTION(Mapping to Theory):

OPERATORS:The operators are types of symbols that inform a compiler


for performing some specific logical or mathematical functions. The operators
serve as the foundations of the programming languages. Thus, the overall
functionalities of the C programming language remain incomplete if we do not use
operators.
Broadly, there are eight types of operators in C and C++. They are:
● Increment and decrement operators.
● Bitwise operators.
● Assignment operators.
● Logical operators.
● Relational operators.
● Special operators.
● Conditional operators.
● Arithmetic Operators.
Arithmetic Operators

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example

+ Addition Adds together two x+y


values

- Subtraction Subtracts one value x-y


from another

* Multiplication Multiplies two values x*y

/ Division Divides one value by x/y


another

% Modulus Returns the division x%y


remainder

++ Increment Increases the value of ++x


a variable by 1

-- Decrement Decreases the value of --x


a variable by 1

CONDITIONAL STATEMENTS:

Conditional statements in C comprise if, if-else, nested, ladder, and switch-


case. They are decision-making constructs that help determine the flow of a
program based on whether a set of predefined conditions are met or not.C has the
following conditional statements:

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

● Use if to specify a block of code to be executed, if a specified condition is


true
● Use else to specify a block of code to be executed, if the same condition is
false
● Use else if to specify a new condition to test, if the first condition is false
● Use switch to specify many alternative blocks of code to be executed

The if Statement,Use the if statement to specify a block of code to be executed if a


condition is true.
Syntax:
if(condition)
{
// true statement(s)
}
The else Statement,Use the else statement to specify a block of code to be
executed if the condition is false.
Syntax:
if(condition)
{
// true statement(s)
}
else
{
//False Statement
}
The else if Statement,Use the else if statement to specify a new condition if the
first condition is false.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Syntax:
if(condition)
{
// true statement(s)
}
else if (Condition)
{
// elseif True Statements
}
else
{
//False Statement
}
If...Else (Ternary Operator),There is also a short-hand if else, which is known as
the ternary operator because it consists of three operands. It can be used to replace
multiple lines of code with a single line. It is often used to replace simple if else
statements:
Syntax: variable = (condition) ? expressionTrue : expressionFalse;

ALGORITHM:
Step1:Start
Step2:Declare appropriate variables, a, b, c for the respective
three numbers as integers.
Step3:Obtain inputs of the three numbers.
Step4:Using conditional if and logical AND, check
if a>b && a>c then go to Step 4.1, else go to step 5.
Step 4.1 Print a is the largest number.
Step5:Using conditional if and logical AND,
Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

check if b>a && b>c then go to Step 5.1, else go to step 6.


Step 5.1 Print b is the largest number.
Step6:Using conditional if and logical AND,
check if c>a && c>b then go to Step 6.1, else go to step 7.
Step 6.1 Print c is the largest number.
Step7:Using conditional if and logical AND,
check if a==b && a==c, go to step 7.1, else go to step 5.
Step 7.1:Print “All numbers are equal"
Step8:Stop

PSEUDOCODE:

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

FLOWCHART:

REAL TIME APPLICATION:

1. Sensor Data Processing:


Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Use if-else statements to handle different ranges of sensor data and trigger
appropriate actions.
2. Engine Control:
Use if statements to adjust engine parameters based on conditions like speed and
temperature.
3. Transaction Processing
Use if-else statements to validate and process transactions based on different
criteria.

VIVA QUESTIONS:
1. What is the purpose of the if statement in C?

2. How does the if-else construct differ from the if-else if-else construct?

3. Explain the use of the switch statement with an example.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

4. What is the role of the break statement in a switch statement?

5. Can a switch statement have an else block?

RESULT:Hence the C-program to determine the largest number from an input of


3Numbers was successfully executed.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

EX NO:1-c) PRINT FIRST 10 NATURAL NUMBERS USING FOR LOOP


AIM:
To write a C program of the first 10 natural numbers using a for loop.

OBJECTIVES:
To understand the concept of using a for loop.

DESCRIPTION(Mapping to Theory):
The for loop is a control flow statement for specifying iteration. Specifically,
a for-loop functions by running a section of code repeatedly until a certain
condition has been satisfied. In the for loop, the variable i is initialized to 1, and the
loop will continue as long as i is less than or equal to 10. In each iteration of the
loop, the printf function will print the value of i to the console, followed by a space
character. Finally, the loop will increment the value of i by 1, and the process will
repeat until the condition i<=10 is no longer true.So, when this code runs, it will
output the numbers 1 through 10 separated by spaces.
SYNTAX:

for (expression 1; expression 2; expression 3) {


// code block to be executed
}
The working of for loop :
1. Initialization is the basic step of for loop, this step occurs only once
during the start of the loop. During Initialization, variables are declared, or
already existing variables are assigned some value.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

2. During the Second Step condition statements are checked and only if the
condition is the satisfied loop we can further process otherwise the loop is
broken.

3: All the statements inside the loop are executed.

4: Updating the values of variables has been done as defined in the loop.
Continue to Step 2 till the loop breaks.

Algorithm:

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

Pseudo Code:

FLOWCHART:

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

REAL TIME APPLICATIONS:


1. ATM machine:
Software of the ATM machine is in a loop to process transaction after
transaction until you acknowledge that you have no more to do.
2. Mobile device user:
Software in a mobile device allows the user to unlock the mobile with 5
password attempts. After that it resets the mobile device.
3.Counting Money:
A stack of 20 bills and you need to count them, you might say, “For each bill
in the stack, I will pick it up and count it.” You would repeat this action 20 times,
similar to how a for loop iterates through a collection.
VIVA QUESTIONS:
1. What is syntax and the purpose of using a for loop ?

2. How do you introduce natural numbers?

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024
U24CS101 Programming in C STUDENT LAB MANUAL

3.What is the sum of n natural numbers?

RESULT: Hence the C-program to the first 10 natural numbers was successfully
executed and the Output was verified.

Department:CSE/IT/AIDS/EEE/ECE MSEC
R2024

You might also like