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

C Lab

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

C Lab

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

SYLLABUS

1. Draw flowchart using Raptor tool

2. Program using Operators

3. Program using Iterative Statements

4. Program using Arrays

5. Program using modular programming using function.

6. Program using various String Operations

7. Program using Pointers

8. Program using user-defined data types

9. Program using files

10. Program using Dynamic memory allocation functions


INDEX

Ex. PAGE
DATE NAME OF THE EXPERIMENTS MARKS SIGN
No. No.
Flowchart using Raptor tool
 Simple interest calculation
1.  Greatest among three numbers
 Find the sum of digits of
a number
Operators
 Arithmetic operator
2.  Logical operator
 Relational operator
 Ternary operator
Iterative statements
 For loop
 While loop
 Do-while loop
3. Conditional Statements
 if
 if-else
 Nested if-else
 if-else-if ladder
Arrays
 One-dimensional numeric
4. array
 Two-dimensional numeric
array

5. Modular programming using functions


String operations
6.  With built-in function
 Without built-in function
7. Add two numbers using Pointers

8. User defined data types

9. Read and Write in a file

10. Dynamic memory allocation

AVERAGE MARKS AWARDED

Content Beyond the Syllabus


FLOWCHART USING RAPTOR TOOL

EX. NO:1

DATE:

AIM:
To draw a flowchart for adding two numbers using RAPTOR tool

SYMBOLS DEFINITION

The assignment symbol is used to change the value


Assignment of a variable. The right hand side of the assignment
is evaluated, and the resulting value is placed in the
variable on the left hand side. For example,
consider the case where the value of x is currently
5, and the assignment "x <- x + 1" is executed.
First "x+1" is evaluated, yielding the result 6. Then
the value of x is changed to be 6. Note that
assignment is very different from mathematical
equality. The statement should be Read "Set x to
x+1" instead of "x equals x+1". The assignment
symbol can also be used to assign a string
expression to a string variable

Call The call symbol is used to invoke procedures


such as graphics routines and other instructor-
provided procedures. The call symbol is also used
to run subcharts included in a Raptor program.

Input The input symbol is used to ask the user for a


number or string while the flowchart is executing.
When an input symbol is executed, the user will
be prompted with a dialog to enter a value that can
be interpreted as either a number or string,
depending on what the user types. The user can
also override the source for input by specifying a
text file to be
used in place of the keyboard.
Output The output symbol is used to either write a number
or text to the Master Console window. The user can
also override the destination for output by
specifying a text file to be used instead of the
Master Console.
Selection

Yes No The selection structure is used for decision making.


The programmer enters in the diamond an
expression that evaluates to Yes (True) or No
(False). Such expressions are formally referred to
as Boolean expressions. Based on the result of the
expression in the diamond, control of the program
will branch either left (Yes, or True) or right (No,
or False).

Loop Control The loop structure is used to repeat a sequence of


symbols until a certain condition is met. When
LOOP execution reaches the bottom of the loop, it starts
over again at the top. The loop is exited when the
diamond symbol is executed and the Boolean
expression in the diamond evaluates to Yes
(True)

Yes

No
SIMPLE INTEREST CALCULATION

SAMPLE INPUT AND OUTPUT:

P= 2

R= 3

N=4

OUTPUT:

I=24
GREATEST AMONG THREE NUMBERS

SAMPLE INPUT AND OUTPUT:

A= 7

B= 9

C=4

OUTPUT:

B=9
FIND THE SUM OF DIGITS OF A NUMBER

SAMPLE INPUT AND OUTPUT:

Number=453

OUTPUT:

Sum=12

MARKS ALLOCATION
Preparation& conduct of (50)
experiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:
Thus the flow chart was drawn successfully using Raptor Tool.
OPERATORS
EX NO:2

DATE:
ARITHMETIC OPERATORS

AIM:

To write c program to perform arithmetic operators.

ALGORITHM:

Step 1: Start the program.


Step2: Read 'a' and 'b' value.
Step 3: perform addition, subtraction, multiplication, division, modulo operator.
Step 4: store result in c.
Step 5: stop the program.

SOURCE CODE:

#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
SAMPLE OUTPUT:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
PROBLEMSTATEMENT:

Write a program that converts a temperature from Fahrenheit to Celsius. The formula to convert
Fahrenheit to Celsius is:

FORMULA:

Celsius = 5/9 ×( Fahrenheit– 32 )

The user should input the temperature in Fahrenheit, and the program should then calculate and display
the temperature in Celsius.

PROGRAM:

#include <stdio.h>
int main()
{
float Fahrenheit, Celsius;
printf(“Enter temperature in Fahrenheit: “);
scanf(“%f”, &Fahrenheit);
Celsius = (5.0 / 9.0) * (Fahrenheit – 32);
printf(“Temperature in Celsius is %.2f\n”, Celsius);
return 0;
}

INPUT:

Enter temperature in Fahrenheit: 98.6

OUTPUT:

Temperature in Celsius is 37.00


LOGICAL OPERATORS

AIM:
To write c program to perform logical operators

ALGORITHM:

Step 1: Start the program.


Step2: Reada,b and cvalue.
Step 3: perform and, or, not.
Step 4: Displayresullt
Step 5: stop the program.

SOURCE CODE:

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c >
b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
SAMPLE OUTPUT:

(a == b) && (c > b) is
1 (a == b) || (c < b) is 1
!(a == b) is 0
PROBLEM STATEMENT:

Write a program that determines if a student has passed or failed an exam based on their scores in two
subjects. A student passes if they score at least 40 in both subjects. The user should input the scores for
the two subjects, and the program should then determine if the student has passed or failed using only
logical and relational operators.

PROGRAM:

#include <stdio.h>
int main()
{
int score1, score2;
int passed;
printf(“Enter the score for subject 1:
“); scanf(“%d”, &score1);
printf(“Enter the score for subject 2:
“); scanf(“%d”, &score2);
Passed = (score1 >= 40) && (score2 >= 40);
printf(“You have %s the exam.\n”, Passed ? “Passed” : “Failed”);
return 0;
}

INPUT:

Enter the score for subject 1: 50


Enter the score for subject 2: 45

OUTPUT:

You have Passed the exam.


RELATIONAL OPERATORS

AIM:
To write c program to perform relational operators

ALGORITHM:

Step 1: Start the program.


Step2: Reada,b and
cvalue.
Step 3: perform relational operator.
Step 4: Displayresullt
Step 5: stop the program.

SOURCE CODE:

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
return 0;
}
SAMPLE OUTPUT:

5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
PROBLEMSTATEMENT:

Write a program that takes two numbers as input and checks which one is greater, using only relational
and arithmetic operators (no conditional statements or loops). The program should then display which
number is greater or if both numbers are equal.

PROGRAM:

#include <stdio.h>
int main()
{
int num1, num2;
intisGreater, isEqual;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
isGreater = (num1 > num2);
isEqual = (num1 == num2);
printf(“The first number is Greater.\n” * isGreater + “The second number is Greater.\n” *
(!isGreater&& !isEqual) + “Both numbers are equal.\n” * isEqual);
return 0;
}

INPUT:

Enter the first number: 30


Enter the second number: 20

OUTPUT:

The first number is Greater.


TERNARY OPERATORS
AIM:

To write c program to perform ternary operators

ALGORITHM:

Step 1: Start the program.


Step2: Reada,b and
cvalue.
Step 3: perform ternary operator.
Step 4: Displayresullt
Step 5: stop the program.

SOURCE CODE:

#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
(age >= 18) ? printf("You can vote") : printf("You cannot
vote"); return 0;
}

SAMPLE OUTPUT:

Enter your age: 12


You cannot vote
PROBLEM STATEMENT:

Write a program that determines the greatest of two numbers using the ternary operator. The user
should input two integers, and the program should use the ternary operator to find and display the
greatest number.

PROGRAM:

#include <stdio.h>
int main()
{
int num1, num2, Greatest;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
Greatest = (num1 > num2) ? num1 : num2;
printf(“The Greatest number is %d\n”, Greatest);
return 0;
}

INPUT:

Enter the first number: 25


Enter the second number: 30

OUTPUT:

The Greatest number is 30

.
VIVA QUESTIONS:

1. Explain about increment and decrement operator?


2. What is unary operator?
3. Define assignment operator.

ADDITIONAL PROGRAM:

1. Write a C program for increment and decrement operator.


2. Write a C program to swap to integers with using temp variable.

MARKS ALLOCATION
Preparation& conduct of (50)
experiments
Observation & result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


ITERATIVE STATEMENTS
EX.NO:3

DATE:
FOR LOOP

AIM:

To write a c program to print even numbers using for loop.

ALGORITHM:

Step 1: Start.
Step 2: initialize i=2
Step 3: check condition
Step 4: i=i+2
Step 5: print i
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
inti;
clrscr();
for(i=2;i<=10;i+=2)
{
printf("%d\t",i);
}
getch();
}

SAMPLE OUTPUT:

2 4 6 8 10
PROBLEM STATEMENT

Write a program that prompts the user to input a positive integer. It should then print the multiplication
table of that number.

PROGRAM:

#include <stdio.h>
int main() {
intnum ;
printf ("Enter a positive integer: ");
scanf("%d", &num);
if (num<= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1; }
printf("Multiplication table of %d:\n", num);
for (inti = 1; i<= 5; i++) {
printf("%d * %d = %d\n", num, i, num * i);
} return 0;
}
OUTPUT
Enter a positive integer: 15
Multiplication table of 15:
15 * 1 = 15
15 * 2 = 30
15 * 3 = 45
15 * 4 = 60
15 * 5 = 75
WHILE LOOP

AIM:
To write a c program to print even numbers using while loop.

ALGORITHM:

Step 1: Start.
Step 2: initialize i=2
Step 3: check condition
Step 4: i=i+2
Step 5: print i
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
Inti=2;
clrscr();
while(i<=10)
{
printf("%d\t",i);
i=i+2;
}
getch();
}

SAMPLE OUTPUT:

2 4 6 8 10
PROBLEM STATEMENT

Write a program to compute sinx for given x. The user should supply x and a positive integer n. We
compute the sine of x using the series and the computation should use all terms in the series up through
the term involving xn
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ......

PROGRAM

#include <stdio.h>

#include <math.h>

int main() {

double x, result = 0;

int n, i = 1;

printf("Enter the value of x in radians: ");

scanf("%lf", &x);

printf("Enter the value of n (a positive integer):

"); scanf("%d", &n); double term = x; int sign =

1;

while (i<= n) {

result += sign *

term; sign *= -1;

term *= (x * x) / ((2 * i) * (2 * i + 1)); i++;

} printf("sin(%.2lf) = %.10lf\n", x, result); return 0;

OUTPUT

Enter the value of x in radians: 24

Enter the value of n (a positive integer): 3

sin(24.00) = 64075.2000000000
DO WHILE LOOP

AIM:
To write a c program to print even numbers using do while loop.

ALGORITHM:

Step 1: Start.
Step 2: initialize i=2
Step 3: i=i+2
Step 4: print i
Step 5: check condition and do repeat
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
inti=2;
clrscr();
do
{
printf("%d\t",i);
i=i+2;

} while(i<10);
getch();
}

SAMPLE OUTPUT:

2 4 6 8 10
PROBLEM STATEMENT

Write a program to enter the numbers till the user wants and at the end the program should display the
largest and smallest numbers entered

PROGRAM

#include <stdio.h>

int main() {

int number; int smallest, largest;char choice; smallest = largest = 0;

do {

printf("Enter a number: ");scanf("%d", &number);

if (smallest == 0 && largest == 0) { smallest = largest = number;

} else {

if (number < smallest) {

smallest = number;} if (number > largest) {

largest = number;}}

printf("Do you want to enter another number? (y/n):

"); scanf(" %c", &choice);

} while (choice == 'y' || choice == 'Y')

printf("Smallest number: %d\n", smallest);

printf("Largest number: %d\n", largest); return 0;

OUTPUT

Enter a number: 41

Do you want to enter another number?: 0

Smallest number: 41

Largest number: 41
MARKS ALLOCATION
Preparation& conduct of (50)
experiments

Observation & result (30)


Record (10)

Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


CONDITINAL STATEMENTS

IF CONDITION

AIM:
To write a c program to print even numbers using if condition.

ALGORITHM:

Step 1: Start.
Step 2: initialize a and b
Step 3: a>b
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
inta=10,b=12;
clrscr();
if(a>b)
{
printf(“%d=a”);

}
getch();
}

SAMPLE OUTPUT:

10
PROBLEM STATEMENT

Write a basic C program using conditional statement to check number is valid or not. If number is
divisible by 2 then valid print on screen otherwise nothing will print.

PROGRAM

#include <stdio.h>

int main()

int n;

printf(“Enter the value for n”);

scanf(“%d”,&n);

if (n/2==0)

printf(“valid number”);

return 0;

Output

Enter the value for n26

valid number

RESULT

Thus,if number is divisible by 2 then valid print on screen otherwise nothing will print.
PROBLEM STATEMENT

Imagine you're writing a program to determine the grade of a student based on their score. Write a C
program that takes the score as input and displays the corresponding grade according to the following
criteria:

 If the score is between 90 and 100 (inclusive), display "Grade: A".


 If the score is between 80 and 89 (inclusive), display "Grade: B".
 If the score is between 70 and 79 (inclusive), display "Grade: C".
 If the score is between 60 and 69 (inclusive), display "Grade: D".
 If the score is below 60, display "Grade: F".

Implement the program using only if statements, without using else statements.

PROGRAM

#include <stdio.h>
int main() {
int score;
printf("Enter the student's score: ");
scanf("%d", &score);
if (score >= 90 && score <= 100)
{ printf("Grade: A\n");
}
if (score >= 80 && score <= 89)
{ printf("Grade: B\n");
}
if (score >= 70 && score <= 79)
{ printf("Grade: C\n");
}
if (score >= 60 && score <= 69)
{ printf("Grade: D\n");
}
if (score < 60) {
printf("Grade: F\n");
}
return 0;
}

OUTPUT

Enter the student's score: 55


Grade: F
IF-ELSE CONDITION

AIM:
To write a c program to print even numbers using if-else condition.

ALGORITHM:

Step 1: Start.
Step 2: initialize a and b
Step 3: a>b
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
inta=10,b=12;
clrscr();
if(a>b)
{
printf(“%d=a”);

}
else
{
printf(“%d=b”);
getch();
}

SAMPLE OUTPUT:

10
PROBLEM STATEMENT

Write a basic C program using if else statement to check if a number is even or odd.

PROGRAM

#include <stdio.h>

int main()

int n;

printf(“Enter the value for n”);

scanf(“%d”,&n);

if (n%2==0)

printf(“the given number is even number”);

else

printf(“the given number is odd number”);

return 0;

Output

Enter the value for n68

the given number is even number


PROBLEM STATEMENT

Imagine you're writing a program to determine the discount rate for customers based on their total
purchase amount. Write a C program that takes the total purchase amount as input and calculates the
discount rate according to the following criteria:

 If the total purchase amount is less than $100, no discount is given.


 If the total purchase amount is between $100 and $500 (inclusive), a 5% discount is given.
 If the total purchase amount is between $501 and $1000 (inclusive), a 10% discount is given.
 If the total purchase amount is greater than $1000, a 15% discount is given.

Implement the program using only if-else statements without nesting or using a ladder structure.

PROGRAM

#include <stdio.h>
int main() {
float totalAmount, discountRate = 0;
printf("Enter the total purchase amount: $");
scanf("%f", &totalAmount);
if (totalAmount > 1000) {
discountRate = 0.15;
}
if (totalAmount >= 501 && totalAmount <= 1000)
{ discountRate = 0.10;
}
if (totalAmount >= 100 && totalAmount <= 500)
{ discountRate = 0.05;
}
if (discountRate == 0) {
printf("No discount is given.\n");
} else {
printf("A %.0f%% discount is given.\n", discountRate * 100);
}
return 0;
}

OUTPUT

Enter the total purchase amount: $1200


A 15% discount is given.
NESTED IF ELSE CONDITION

AIM:
To write a c program to print even numbers using nested if-else condition.

ALGORITHM:

Step 1: Start.
Step 2: initialize a and b,c
Step 3: a>b>c
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>

#include<conio.h>

void main(){

inta,b,c;clrscr();

printf("\nEnter 3 number");

scanf("%d%d%d",&a,&b,&c);

if(a>b){

if(a>c)

printf(“%d is great",a);else

printf(“%d is grear”,c);}

else{if(b>c)

printf(“%d is great”,b);else

printf(“%d is great”,c);}

getch();}

SAMPLE OUTPUT:

Enter 3 number: 6 2 1

6 is great
PROBLEM STATEMENT

You are a developer tasked with creating a program for a small weather station that records
temperatures from three different sensors placed at different locations in a city. The program needs to
determine which sensor recorded the highest temperature on a given day.

Program

#include <stdio.h>

int main() {

float temp1, temp2, temp3;

printf("Enter temperature from Sensor 1: ");

scanf("%f", &temp1);

printf("Enter temperature from Sensor 2: ");

scanf("%f", &temp2);

printf("Enter temperature from Sensor 3: ");

scanf("%f", &temp3);

if (temp1 > temp2) {

if (temp1 > temp3) {

printf("Sensor 1 recorded the highest temperature: %.2f\n", temp1);

} else {

printf("Sensor 3 recorded the highest temperature: %.2f\n", temp3);

} else {

if (temp2 > temp3) {

printf("Sensor 2 recorded the highest temperature: %.2f\n", temp2);

} else {

printf("Sensor 3 recorded the highest temperature: %.2f\n", temp3);

return 0;
}

Input

Enter temperature from Sensor 1: 25

Enter temperature from Sensor 2: 30

Enter temperature from Sensor 3: 28

Output

Sensor 2 recorded the highest temperature: 30


IF ELSE IF LADDER CONDITION

AIM:
To write a c program to print even numbers using if-else-if ladder condition.

ALGORITHM:

Step 1: Start.
Step 2: initialize a and b,c
Step 3: a>b>c
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>

#include<conio.h>

void main(){

intmark;clrscr();

printf("\nEnter the mark:");

scanf("%d",&mark);

if(mark>90)printf("S Grade");

else if(mark>80)printf("A Grade");

else if(mark>70)printf("B Grade");

else if(mark>60)printf("C Grade");

else if(mark>56)printf("D Grade");

else if(mark>=50)printf("E Grade");

elseprintf("Fail Mark");

getch();

SAMPLE OUTPUT:

Enter the mark: 85

A Grade
PROBLEM STATEMENT

To determine the bill amount based on the units consumed.Scenario:The first 100 units are charged at
$1.50 per unit.The next 100 units are charged at $2.00 per unit.The next 300 units are charged at
$2.50 per unit.Any units above 500 are charged at $3.00 per unit.

PROGRAM

#include <stdio.h>

int main() {

float units, bill;

printf("Enter the number of units consumed: ");

scanf("%f", &units);

if (units <= 100) {

bill = units *

1.50;

} else if (units <= 200) {

bill = (100 * 1.50) + ((units - 100) * 2.00);

} else if (units <= 500) {

bill = (100 * 1.50) + (100 * 2.00) + ((units - 200) * 2.50);

} else {

bill = (100 * 1.50) + (100 * 2.00) + (300 * 2.50) + ((units - 500) * 3.00);

printf("The total energy bill is: $%.2f\n", bill); return 0;

OUTPUT

Enter the number of units consumed: 250

The total energy bill is: $475.00.


VIVA QUESTIONS:

1. Differentiate between while and do-while loop.


2. Define looping statement.

ADDITIONAL PROGRAM:

1. Write a C program to print odd numbers.


2. Write a C Program to print student result using goto statement.

MARKS ALLOCATION
Preparation & conduct of (50)
experiments

Observation&result (30)

Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


ARRAYS
EX.NO: 4

DATE:
ONE DIMENSIONAL ARRAY

AIM:
To write a c program to print find average on ‘n’ marks

ALGORITHM:

Step 1: Start.
Step 2: Declare one dimensional array
Step 3: Get input values
Step 4: calculate sum and average
Step 5: Display it
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
intmark[10],n,i,sum=0;
float avg;
clrscr();
printf("\nEnter number of marks:");
scanf("%d",&n);
printf("Enter %d marks one by one:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&mark[i]);
sum=sum+mark[i];
}
printf("\nSum=%d",sum);
avg=sum/n; printf("\nAverage=
%f",avg); getch();
}

SAMPLE OUTPUT:

Enter number of mark: 5


Enter 5 marks one by one:
90 80 90 80 70
Sum=410
Average=82.00
PROBLEM STATEMENT:

You are developing an inventory management system for a small store. The system needs a program to
help sort a list of product prices to determine the cheapest and most expensive items easily. Product
prices are integer values. You need to implement a C program that will take the prices as input, sort
them in ascending order, and display the sorted list.

PROGRAM:
include <stdio.h>
int main() {
int prices[100];
int numProducts;

printf("Enter the number of products: ");


scanf("%d", &numProducts);

printf("Enter the prices of the products:\n");


for (int i = 0; i < numProducts; i++) {
printf("Price of product %d: ", i + 1);
scanf("%d", &prices[i]);
}

for (int i = 0; i < numProducts - 1; i++) {


for (int j = 0; j < numProducts - i - 1; j++)
{ if (prices[j] > prices[j + 1]) {
// Swap prices[j] and
prices[j+1] int temp = prices[j];
prices[j] = prices[j + 1];
prices[j + 1] = temp;
}
}
}
printf("Sorted list of product prices:\n");
for (int i = 0; i < numProducts; i++) {
printf("%d ", prices[i]);
}
printf("\n");
return 0;
}

OUTPUT:
Enter the number of products: 5
Enter the prices of the products:
Price of product 1: 25
Price of product 2: 10
Price of product 3: 50
Price of product 4: 30
Price of product 5: 15
Sorted list of product prices:
10 15 25 30 50
TWO DIMENSIONAL ARRAY

AIM:
To write a c program to add two matrix.

ALGORITHM:

Step 1: Start.
Step 2: Enter rows and columns using two dimensional array
Step 3: Get A and B matrix integers
Step 4: Add A and B matrix and store in C matrix
Step 5: Display it
Step 6: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
inta[10][10],b[10][10],c[10][10],row,col,i,j;
clrscr();
printf("Enter the no.of rows:");
scanf("%d",&row);
printf("Etner the no.of columns:");
scanf("%d",&col);
printf("Enter the matrix A element:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the matrix B element:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition matrix is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

SAMPLE OUTPUT:

Enter the no.of rows:3


Etner the no.of columns:3
Enter the matrix A element:
1 2 3
4 5 6
7 8 9
Enter the matrix B element:
1 2 3
4 5 6
7 8 9
Addition matrix is:
2 4 6
8 10 12
14 16 18
PROBLEM STATEMENT:

You are tasked with developing a C program to assist a small data analysis firm in processing two-
dimensional arrays (matrices) of numerical data. The firm needs a tool that can add corresponding
elements from two given 2D arrays and produce a resultant .The matrices to be added will always have
the same dimensions.

PROGRAM
include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix1[ROWS]
[COLS]; int
matrix2[ROWS][COLS];
int result[ROWS][COLS];
printf("Enter elements of the first matrix (%d x %d):\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("Enter element [%d][%d]: ", i,
j);
scanf("%d", &matrix1[i][j]);
}
}
printf("\nEnter elements of the second matrix (%d x %d):\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("Enter element [%d][%d]: ", i,
j);
scanf("%d", &matrix2[i][j]);
}
}
printf("\nResultant matrix after addition:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}

OUTPUT:

Enter elements of the first matrix (3 x


3): Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 4
Enter element [1][1]: 5
Enter element [1][2]: 6
Enter element [2][0]: 7
Enter element [2][1]: 8
Enter element [2][2]: 9

Enter elements of the second matrix (3 x


3): Enter element [0][0]: 9
Enter element [0][1]: 8
Enter element [0][2]: 7
Enter element [1][0]: 6
Enter element [1][1]: 5
Enter element [1][2]: 4
Enter element [2][0]: 3
Enter element [2][1]: 2
Enter element [2][2]: 1

Resultant matrix after addition:


10 10 10
10 10 10
10 10 10

VIVA QUESTIONS:

1. Define Arrays.
2. List some operations performed by array.

ADDITIONAL PROGRAM:

1. Write a C program to reverse array elements.


2. Write a C Program to perform insertion sort.

MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


MODULAR PROGRAMMING USING FUNCTIONS
EX.NO: 5

DATE:
AIM:
To write a c program to execute module programming using function.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare a function.
Step 3: Declare actual arguments a,b.
Step 4: Read values for a,b.
Step5: Define the function.
Step 6: Call the function.
Step 7:Perform function prototypes and recursion
function. Step8: Print the result.
Step 9: Return.
Step 10: Stop the program.

SOURCE CODE:

1 Function with no argument and no return value.


#include<stdio.h>
#include<conio.h>
void sum(void);
void main()
{
sum();
}
void sum()
{
inta,b,c;
clrscr();
printf("\nEnter 2 numbers:");
scanf("%d%d",&a,&b);
c=a+b;
printf("Sum=%d",c);
getch();
}

Output
Enter 2 numbers: 2 3
Sum = 5
2. Function with argument and no return value
#include<stdio.h>
#include<conio.h>
void sum(int,int);
void main()
{
inta,b;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
void sum(inta,int b)
{
int c;
c=a+b;
printf("Sum=%d",c);
}

Output

Enter 2 numbers: 4 3
Sum = 7

3 Function with no argument and with return value.


#include<stdio.h>
#include<conio.h>
int sum(void);
void main()
{
int c;
clrscr();
c=sum(); printf("\
nSum=%d",c); getch();
}
int sum()
{
inta,b,c;
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
}
Output

Enter 2 numbers: 4 7
Sum = 11
4 Function with argument and with return value.
#include<stdio.h>
#include<conio.h>
int sum(int,int);
void main()
{
inta,b,c;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
c=sum(a,b); printf("\nSum=
%d",c); getch();
}
int sum(inta,int b)
{
int c;
c=a+b;
return(c);
}

Output
Enter 2 numbers: 4 7
Sum = 11

5. Find the factorial of given number using recursion

#include<stdio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
printf("Factorial of %d = %d",n,fact(n));
getch();
}
int fact(int n)
{
if(n!=1)
n=n*fact(n-1);
return n;
}

Output
Enter number: 5
Factorial of 5 is 120
PROBLEM STATEMENT:

1. You are working as a software developer for a company that designs digital displays for household
appliances. Your current project involves programming a thermostat that displays temperature
readings in both Celsius and Fahrenheit. You need to write a function in C that reads a temperature in
Celsius from the user, converts it to Fahrenheit, and displays both values. The function should not take
any arguments and should not return any value. It will simply perform the input, conversion, and
output

PROGRAM:

#include <stdio.h>
void displayTemperature()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius:
"); scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Celsius: %.2f\n", celsius);
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
}
int main() {
displayTemperature();
return 0;
}

OUTPUT:

Enter temperature in Celsius: 25


Temperature in Celsius: 25.00
Temperature in Fahrenheit: 77.00
PROBLEM STATEMENT

2. Imagine you're developing a program for a library management system. You need to create a function
that calculates the late fee for returning a book after the due date. Write a C program that includes a
function calculateLateFee which takes two parameters: the due date of the book and the actual
return date. The function should calculate and return the late fee based on the following criteria:

 If the book is returned on or before the due date, the late fee is $0.
 If the book is returned after the due date, the late fee is calculated as $0.25 for each day
overdue.

Your program should prompt the user to enter the due date and the actual return date of the book, call
the calculateLateFee function to determine the late fee, and then display the late fee to the user.

PROGRAM

#include <stdio.h>
float calculateLateFee(int dueDate, int returnDate) {
if (returnDate <= dueDate) {
return 0.0;
}
else {
return 0.25 * (returnDate - dueDate);
}
}

int main() {
int dueDate, returnDate;
float lateFee;
printf("Enter the due date (in days from today):
"); scanf("%d", &dueDate);
printf("Enter the actual return date (in days from today):
"); scanf("%d", &returnDate);
lateFee = calculateLateFee(dueDate, returnDate);
printf("The late fee for returning the book is: $%.2f\n",
lateFee); return 0;
}

OUTPUT:

Enter the due date (in days from today): 10


Enter the actual return date (in days from today):
12 The late fee for returning the book is: $0.50
VIVA QUESTIONS:

1. What is user defined function?


2. Define return statement?

ADDITIONAL PROGRAM:

1. Write a C program to returning multiple values using an array.


2. Write a C Program Swapping of two numbers and changing the value of a variable
using pass by reference.

MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


STRING OPERATION
EX.NO: 6

DATE:

STRING CONCATENATION WITH OUT USING BUILT IN LIBRARY FUNCTIONS

AIM:
To write a c program to concatenation of two strings.

ALGORITHM:

Step 1: Start.
Step 2: Get two strings using char datatype
Step 3: use while loop to concat two
strings Step 4: Display it
Step 5: Stop.

SOURCE CODE:

#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
inti=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
SAMPLE OUTPUT:

Enter First String: Hello


Enter Second String: World
Concatenated String is HelloWorld
STRING CONCATENATION WITH USING BUILT IN LIBRARY FUNCTIONS

AIM:
To write a c program to concatenation of two strings.

ALGORITHM:

Step 1: Start.
Step 2: Get two strings using char
datatype Step 3: use strcat to concat two
strings Step 4: Display it
Step 5: Stop.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the string1:");
gets(s1);
printf("\nEnter the string2:");
gets(s2);
strcat(s1,s2);
printf("\nConcatenated String:%s",s1);
getch();
}

SAMPLE OUTPUT:

Enter string1: Hello


Enter string2: World
Concatenated String: HelloWorld
PROBLEM STATEMENT

You are working on a text processing application in C that requires frequent manipulation of strings.
One of the core features needed is concatenating two strings.
Your task is to implement this feature in two ways:

1. Without using any built-in library functions.


2. Using the built-in library function strcat.

PROGRAM

Without using any built-in library functions

#include <stdio.h>
#define MAX_LENGTH 101
int main() {
char str1[MAX_LENGTH], str2[MAX_LENGTH];
int I, j;
printf(“Enter the first string: “);
fgets(str1, MAX_LENGTH, stdin);
printf(“Enter the second string: “);
fgets(str2, MAX_LENGTH, stdin);
str1[strcspn(str1, “\n”)] = 0;
str2[strcspn(str2, “\n”)] = 0;
for (I = 0; str1[i] != „\0‟; i++);
for (j = 0; str2[j] != „\0‟; j++, i++)
{ str1[i] = str2[j];
}
str1[i] = „\0‟;
printf(“Concatenated string: %s\n”, str1);
return 0;
}

Using the built-in library function strca


#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 101
int main() {
char str1[MAX_LENGTH], str2[MAX_LENGTH];
printf(“Enter the first string: “);
fgets(str1, MAX_LENGTH, stdin);
printf(“Enter the second string: “);
fgets(str2, MAX_LENGTH, stdin);
str1[strcspn(str1, “\n”)] = 0;
str2[strcspn(str2, “\n”)] = 0;
strcat(str1, str2);
printf(“Concatenated string: %s\n”, str1);
return 0;
}
INPUT

Enter the first string: Hello


Enter the second string: World

OUTPUT
Concatenated string: HelloWorld

VIVA QUESTIONS:

1. How to initialize string?


2. How to convert upper case letter to lower case letters?

ADDITIONAL PROGRAM:

1. Write a C program to reverse a string.


2. Write a C Program to compare two strings.

MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


ADD TWO NUMBERS USING POINTERS

EX.NO.7

DATE:

AIM:

To write a C program for adding two numbers using pointers.

ALGORITHM:

Step 1: Start the Program


Step 2: Read the values for a, b
Step 3: Initialize the pointer p, q with the address of a, b
Step 4: Add the values of the address hold by p, q by
sum=*p+*q Step 5: Print the value of sum.
Step 6: Stop the program.

SOURCE CODE:

#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &a, &b);
p = &a;
q = &b;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}

SAMPLE OUTPUT:
Enter two integers to add
4
5
Sum of entered numbers=9
PROBLEM STATEMENT:

Write a program that adds two numbers using pointers. The user should input two integers, and the
program should use pointers to store these integers and compute their sum. The program should then
display the sum.

PROGRAM:

#include <stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2, *ptrSum;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
ptr1 = &num1;
ptr2 = &num2;
ptrSum = &sum;
*ptrSum = *ptr1 + *ptr2;
printf(“The sum of %d and %d is %d\n”, *ptr1, *ptr2,
*ptrSum); return 0;
}

INPUT:

Enter the first number: 10


Enter the second number: 20

OUTPUT:

The sum of 10 and 20 is 30


VIVA QUESTIONS:

1. What is Pointer?
2. How to use Pointers?
3. What is NULL Pointer in C?

ADDITIONAL PROGRAM:

1. Write a C program to swap two numbers using pointers.

2. Write a C program to perform arithmetic operations of two numbers using pointer


and functions.

MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


STUDENT DETAILS USING STRUCTURE
EX NO :8
DATE:

AIM:
To print student details using structure.

ALGORITHM:

Step 1: Start the program.


Step 2: Defining the structure student with
members. Step 3: Declare name, r no, m1, m2, m3,
m4.
Step 4: Read the student details.
Step 5: Total the marks and calculate the
average. Step 6: Print the mark list
Step 7:Stop the program.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
intrno;
int m1,m2,m3, m4;
int tot;
float avg;
}s;
void main()
{
int n;
clrscr();
printf("\nENTER NAME,RNO AND MARKS \n\n");
scanf("%s%d",.s.name, &s.rno);
scanf("%d",& s.m1);
scanf("%d",&s.m2);
scanf("%d",&s.m3);
scanf("%d",&s.m4);
printf("\n\n\n");
printf("\t\t******************STUDENT DETAILS***************\n");
printf("\t \n"); printf("\
tNAME\tRNO\tM1\tM2\tM3\tM4\tTOTAL\tAVERAGE\n"); printf("\t \
n"); s.tot=s.m1+s.m2+s.m3+s.m4;
s.avg=(s.tot)/4; printf("\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\
n",s.name,s.rno,s.m1,s.m2,s.m3,s.m4,s.tot,s.avg); printf("\t \n");
getch();
}

SAMPLE OUTPUT:
ENTER NAME, RNO AND MARKS
Sugir 23
95
85
85
95
***********************STUDENT DETAILS******************
NAME RNO M1 M2 M3 M4 TOTAL AVERAGE

Sugir 23 95 85 85 95 360 90.00

PROBLEM STATEMENT

Imagine you are developing a program to manage student information for a school. Each student record
contains the following information: student ID, name, age, and grade. Define a structure to represent a
student, and write a C program that allows users to add new students and display their information.

You can also extend the program to include additional functionalities such as searching for a student by
their ID, updating student information, and removing a student from the system.
PROGRAM

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#define MAX_STUDENTS 100


#define MAX_NAME_LENGTH 50

// Structure to represent a student


struct Student {
int studentID;
char name[MAX_NAME_LENGTH];
int age;
int grade;
};

// Global array to store student records


struct Student students[MAX_STUDENTS];

// Variable to keep track of the number of students


int numStudents = 0;

int main() {
int choice = 0;
while (choice != 3) {
printf("\nStudent Information Management System\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 1) {
// Add a new student
if (numStudents == MAX_STUDENTS) {
printf("Error: Maximum capacity reached. Cannot add more
students.\n");
continue;
}

struct Student newStudent;

printf("Enter student ID: ");


scanf("%d", &newStudent.studentID);

printf("Enter student name: ");


scanf("%s", newStudent.name);

printf("Enter student age: ");


scanf("%d", &newStudent.age);

printf("Enter student grade: ");


scanf("%d", &newStudent.grade);

students[numStudents++] = newStudent;

printf("Student added successfully.\n");


} else if (choice == 2) {
// Display all student records
if (numStudents == 0) {
printf("No student records found.\n");
continue;
}

printf("Student Records:\n");
printf("ID\tName\tAge\tGrade\n");
for (int i = 0; i < numStudents; i++)
{
printf("%d\t%s\t%d\t%d\n", students[i].studentID,
students[i].name, students[i].age, students[i].grade);
}
} else if (choice == 3) {
// Exit the program printf("Exiting...\
n");
} else {
printf("Invalid choice. Please enter a number between 1 and 3.\n");
}
}
return 0;
}

OUTPUT

Student Information Management System


1. Add Student
2. Display Students
3. Exit
Enter your choice: 1
Enter student ID: 101
Enter student name: John
Enter student age: 20
Enter student grade: 85
Student added successfully.

Student Information Management System


1. Add Student
2. Display Students
3. Exit
Enter your choice:
2 Student Records:
ID Name Age Grade
101 John 20 85

Student Information Management System


1. Add Student
2. Display Students
3. Exit
Enter your choice: 3
Exiting...
VIVA QUESTIONS:

1. Define structure.
2. Difference between array and structure.
3. Difference between union and structure.

ADDITIONAL PROGRAM:

1. Write a C program to print student details using union


MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


READ AND WRITE IN A FILE

EX.NO.9

DATE:

AIM:

To write a C program for Read and write the information’s in a file.

ALGORITHM:

Step 1:Start the Program


Step 2:Open the File
Step 3:Use the file commands
fopen,fclose,fwrite. Step 4:Give the inputs
rollno,name,percent.
Step 5:Read the file.
Step 6:Print the rollno,name,percent
Step 7:Stop the program.

SOURCE CODE:

include<stdio.h>
#include<conio.h>
struct stud
{
int roll;
char name[12];
int percent;
}
s = {10,"SMJC",80};
void main()
{
FILE *fp;
struct stud s1;
clrscr();
fp = fopen("ip.txt","w");
/* write struct s to file */
fwrite(&s, sizeof(s), 1,fp);
fclose(fp);
fp = fopen("ip.txt","r");
/* Readstruct s to file */
fRead(&s1, sizeof(s1), 1,fp);
fclose(fp);
printf("nRoll : %d",s1.roll);
printf("nName : %s",s1.name);
printf("nPercent : %d",s1.percent);
}

SAMPLE OUTPUT
Roll : 10
Name : SMJC
Percent : 80

PROBLEM STATEMENT :

Program to write characters a to z into a file and read the file and print the characters in lowercase.

PROGRAM:
#include<stdio.h>
Voidmain()
{
file *f1;
char c;
clrscr();
printf("Writing characters to file... \n");
f1 = fopen("alpha.txt","w");
for(ch=65;ch<=90;ch++)
fputc(ch,f1);
fclose(f1);
printf("\nRead data from file: \n");
f1 = fopen("alpha.txt","r");
/reads character by character in a
file/ while((c=getc(f1))!=eof)
printf("%c",c+32); /prints characters in lower
case/ fclose(f1);
getch();
}

OUTPUT:
Writing characters to file….
Read data from file: abcdefghijklmnopqrstuvwxyz
VIVA QUESTIONS:

1. What are the file operations in C?


2. What are the modes available?
3. What is w+, r+, a+?

ADDITIONAL PROGRAM:

1. Write a C program to merge two files

MARKS ALLOCATION
Preparation & conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


DYNAMIC MEMORY ALLOCATION
EX.NO.10

DATE:

AIM:

To write a C program for create memory for int, char and float variable at run time using
dynamic memory allocation.

ALGORITHM:

Step 1:Start the Program


Step 2:Open the File
Step 3:Use the memory allocation function allocate memory.
Step 4:Give the inputs.
Step 5:Print the values
Step 6:Stop the
program.

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *iVar;
char *cVar;
float *fVar;

iVar=(int*)malloc(1*sizeof(int));
cVar=(char*)malloc(1*sizeof(char));
fVar=(float*)malloc(1*sizeof(float));

printf("Enter integer value: ");


scanf("%d",iVar);

printf("Enter character value: ");


scanf(" %c",cVar);

printf("Enter float value: ");


scanf("%f",fVar);

printf("Inputted value are: %d, %c, %.2f\n",*iVar,*cVar,*fVar);


free(iVar);
free(cVar);
free(fVar);

return 0;
}

SAMPLE OUTPUT

Enter integer value: 100


Enter character value: x
Enter float value: 123.45

Inputted value are: 100, x, 123.45


PROBLEM STATEMENT:

WRITE A C PROGRAM TO CALCULATE AN ELECTRIC BILL BASED ON THE FOLLOWING


THREE CONDITIONS USING DYNAMIC MEMORY ALLOCATION

1 FOR THE FIRST 100 UNITS, THE RATE IS $1.50 PER UNITS
2 FOR THE NEXT 200 UNITS, THE RATE IS $2.00 PER UNIT
3 FOR ANY UNITS ABOVE 300, THE RATE IS $2.50 PER UNIT

PROGRAM:

#include <stdio.h>

#include <stdlib.h>

int main()

int units;

float *rates, bill = 0;

printf("Enter the total units consumed: ");

scanf("%d", &units);

// Dynamically allocate memory for the rates array

rates = (float*)malloc(units * sizeof(float));

// Check if memory allocation is

successful if (rates == NULL) {

printf("Memory allocation failed.");

return 1;

// Assign rates based on the three conditions

for (inti = 0; i< units; i++) {

if (i< 100) {

rates[i] = 1.50;

} else if (i< 300) {


rates[i] = 2.00;

} else {

rates[i] = 2.50;

// Calculate the total bill amount

for (inti = 0; i< units; i++) {

bill += rates[i];

// Print the total bill amount

printf("Total bill amount: $%.2f\n", bill);

// Free dynamically allocated memory

free(rates);

return 0;

OUTPUT:

Enter the total units consumed: 102


Total bill amount: $154.00
VIVA QUESTIONS:

1. What are the functions in dynamic memory allocation?


2. What is the general format for malloc()?
3. What is the general format for calloc()?

ADDITIONAL PROGRAM:

1. Write a C program to perform sequential access files.

MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments

Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)

RESULT:

Thus the C program was compiled and executed successfully.


CONTENT BEYOND THE SYLLABUS

1. Write a C program to develop simple arithmetic calculator using Switch Case statement.

You might also like