SSN COLLEGE OF ENGINEERING
UIT2603- Internet of Things and C
programming
Assignment 1
Name Lokesh S
Register No. 3122225002065
Date 05-03-2025
1) Consider there are 10 houses in a locality. You are supposed to
find the energy consumption of each home by passing the unit
consumed. You have to calculate the bill amount based on the
energy consumption pattern. When the consumption is less than
100, the bill amount is zero. When the consumption is greater
than 100, reduce 100 units from the consumption and calculate
the bill amount as follows. (a). 0 to 100 units: Free of charge. (b).
0 to 200 units: Free for the first 100 units, for the remaining
balance Rs.1.5 per unit. (c ). For 0 to 500 units: Free for the first
100 units, for the next 100 Rs.2 per unit, for the balance Rs.3 per
unit. (d). Above 500 units: Free for the first 100 units, 101 to 200
units cost Rs.3.5 per unit; next 201 to 500 units cost Rs.4.6 per
unit; and units over 500 cost Rs.6.6 per unit. Write a C program
for the above scenario that uses an array of pointers and an array
of function pointers.
#include <stdio.h>
float calc1(float units) {
return (units - 100) * 1.5; }
float calc2(float units) {
return (100 * 2) + ((units - 200) * 3); }
float calc3(float units) {
return (100 * 3.5) + (300 * 4.6) + ((units - 500) * 6.6); }
int main() {
float consumption[10], bill[10];
float *p_consumption[10];
float (*calc_funcs[])(float) = {calc1, calc2, calc3};
int i;
printf("Enter energy consumption (in units) for 10 houses:\n");
for(i = 0; i < 10; i++) {
scanf("%f", &consumption[i]);
p_consumption[i] = &consumption[i];
for(i = 0; i < 10; i++) {
if(consumption[i] <= 100) {
bill[i] = 0;
} else if(consumption[i] <= 200) {
bill[i] = calc_funcs[0](consumption[i]);
} else if(consumption[i] <= 500) {
bill[i] = calc_funcs[1](consumption[i]);
} else {
bill[i] = calc_funcs[2](consumption[i]);
printf("\nHouse\tConsumption\tBill Amount\n");
for(i = 0; i < 10; i++) {
printf("%d\t%.2f\t\t%.2f\n", i+1, consumption[i], bill[i]);
return 0;
}
OUTPUT:
2) Write a C program using pointers to compute the Sum, Mean
and Standard deviation of all elements stored in an array of “n”
real numbers.
#include <stdio.h>
#include <math.h>
int main() {
int n, i;
float sum = 0, mean, std = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter %d real numbers:\n", n);
for(i = 0; i < n; i++) {
scanf("%f", &arr[i]);
float *p = arr;
for(i = 0; i < n; i++) {
sum += *(p + i);
mean = sum / n;
for(i = 0; i < n; i++) {
std += (*(p + i) - mean) * (*(p + i) - mean);
std = sqrt(std / n);
printf("Sum = %.2f, Mean = %.2f, Standard Deviation = %.2f\n", sum,
mean, std);
return 0;
OUTPUT:
3) Write a C program to calculate a student's result based on two
examinations, one sports event, and three activities conducted.
The weightage of events activities=30%, sports=20% and
examination= 50%.
#include <stdio.h>
int main() {
float exam1, exam2, sports, act1, act2, act3;
float exam_weight, sports_weight, act_weight, finalResult;
printf("Enter marks for Exam 1 and Exam 2: ");
scanf("%f %f", &exam1, &exam2);
printf("Enter marks for Sports event: ");
scanf("%f", &sports);
printf("Enter marks for three Activities: ");
scanf("%f %f %f", &act1, &act2, &act3);
exam_weight = ((exam1 + exam2) / 2.0) * 0.5;
sports_weight = sports * 0.2;
act_weight = ((act1 + act2 + act3) / 3.0) * 0.3;
finalResult = exam_weight + sports_weight + act_weight;
printf("Final result = %.2f\n", finalResult);
return 0; }
OUTPUT:
4) Write a C program to prepare a grocery bill. For that enter the
name of items purchased, quantity in which it is purchased and
its price per unit. Then display the bill in the following format.
#include <stdio.h>
#define MAX_ITEMS 100
struct Item {
char name[50];
int quantity;
float price;
};
int main() {
int n, i;
struct Item items[MAX_ITEMS];
printf("Enter the number of items purchased: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Enter name, quantity, and price for item %d: ", i+1);
scanf("%s %d %f", items[i].name, &items[i].quantity,
&items[i].price); }
float total = 0;
printf("\n----------------- Grocery Bill -----------------\n");
printf("Item\tQuantity\tPrice\tAmount\n");
for(i = 0; i < n; i++) {
float amount = items[i].quantity * items[i].price;
total += amount;
printf("%s\t%d\t\t%.2f\t%.2f\n", items[i].name, items[i].quantity,
items[i].price, amount); }
printf("------------------------------------------------\n");
printf("Total Amount: Rs. %.2f\n", total);
return 0;
OUTPUT:
5) Write a program to calculate tax, given the following
conditions: (a). if income is less than 1,50,000 then no tax (b). if
taxable income is in the range 1,50,001-300,000 then charge 10%
tax (c). if taxable income is in the range 3,00,001-500,000 then
charge 20% tax (d). if taxable income is above 5,0,0,001 then
charge 30% tax.
#include <stdio.h>
int main() {
float income, tax = 0;
printf("Enter your income: ");
scanf("%f", &income);
if(income <= 150000)
tax = 0;
else if(income <= 300000)
tax = (income - 150000) * 0.10;
else if(income <= 500000)
tax = (150000 * 0.10) + ((income - 300000) * 0.20);
else
tax = (150000 * 0.10) + (200000 * 0.20) + ((income - 500000) *
0.30);
printf("Tax to be paid: Rs. %.2f\n", tax);
return 0;
OUTPUT:
6) Write a program to calculate parking charges or a vehicle.
Enter the type of vehicle as a character (like c for car, b for bus,
etc) and number of hours then calculate charges as given below:
● Truck/bus - 20 Rs per hour ● Car-10 Rs per hour ● Scooter/
Cycle/ Motor cycle - 5 Rs per hour. Modify the above program to
calculate the parking charges. Read the hours and minutes when
the vehicle enters the parking lot. When the vehicle is leaving,
enter its leaving time. Calculate the difference between the two
timings to calculate the number of hours and minutes for which
the vehicle was parked. Finally calculate the charges based on
following rules and then display the result on the screen.
#include <stdio.h>
int main() {
char vehicle;
int entryH, entryM, exitH, exitM, entryTotal, exitTotal, totalMinutes,
hours, leftover;
float rate1, rate2, charge = 0;
printf("Enter vehicle type (t/b for truck/bus, c for car, s for
scooter/cycle/motorcycle): ");
scanf(" %c", &vehicle);
printf("Enter entry time (hours minutes): ");
scanf("%d %d", &entryH, &entryM);
printf("Enter exit time (hours minutes): ");
scanf("%d %d", &exitH, &exitM);
entryTotal = entryH * 60 + entryM;
exitTotal = exitH * 60 + exitM;
if (exitTotal < entryTotal) {
printf("Invalid times entered.\n");
return 1;
totalMinutes = exitTotal - entryTotal;
hours = totalMinutes / 60;
leftover = totalMinutes % 60;
if (leftover > 0) hours++;
switch (vehicle) {
case 't': case 'b': rate1 = 20; rate2 = 30; break;
case 'c': rate1 = 10; rate2 = 20; break;
case 's': rate1 = 5; rate2 = 10; break;
default: printf("Invalid vehicle type.\n"); return 1;
if (hours <= 3) charge = hours * rate1;
else charge = 3 * rate1 + (hours - 3) * rate2;
printf("Parking charge: %.2f\n", charge);
return 0; }
OUTPUT:
7) Write a program to build an array of 100 random numbers in
the range 1 to 100. Perform the following operations on the array.
(a) Count the number of elements that are completely divisible by
3. (b) Display the elements of the array by displaying a maximum
of ten elements in one line. (c) Display only the even elements of
the array by displaying a maximum of ten elements in one line.
(d) Count the number of odd elements. (e) Find the smallest
element in the array. (f) Find the position of the largest value in
the array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 100
int main() {
int arr[SIZE];
int i, countDiv3 = 0, countOdd = 0;
int smallest, largest, posLargest;
srand(time(0));
for(i = 0; i < SIZE; i++) {
arr[i] = rand() % 100 + 1;
smallest = arr[0];
largest = arr[0];
posLargest = 0;
for(i = 0; i < SIZE; i++){
if(arr[i] % 3 == 0)
countDiv3++;
if(arr[i] % 2 != 0)
countOdd++;
if(arr[i] < smallest)
smallest = arr[i];
if(arr[i] > largest) {
largest = arr[i];
posLargest = i;
printf("Array elements:\n");
for(i = 0; i < SIZE; i++){
printf("%3d ", arr[i]);
if((i + 1) % 10 == 0)
printf("\n");
printf("\nEven elements:\n");
int count = 0;
for(i = 0; i < SIZE; i++){
if(arr[i] % 2 == 0) {
printf("%3d ", arr[i]);
count++;
if(count % 10 == 0)
printf("\n");
}
printf("\n");
printf("Count of numbers divisible by 3: %d\n", countDiv3);
printf("Count of odd numbers: %d\n", countOdd);
printf("Smallest element: %d\n", smallest);
printf("Position of largest element: %d (0-indexed)\n", posLargest);
return 0;
OUTPUT:
8) (a). Write a short note on pointer arithmetic and pointer
expressions. (b). What do you understand by the term pointer to
a function? (c). Give the advantages of using pointers. (d). Can we
have an array of function pointers? If yes, illustrate with the help
of a suitable example. (e). Differentiate between a function
returning pointer to int and a pointer to function returning int.
(f). Write a program that illustrates passing of character arrays as
an argument to a function (use pointers). (g). Explain with an
example how an array can be passed to a function?
(a) Pointer arithmetic refers to operations such as addition or
subtraction performed on pointer variables, which move the pointer by
multiples of the size of the data type it points to. For example, if p is a
pointer to an int, then p + 1 advances the pointer to the next integer in
memory. Pointer expressions can be used to navigate arrays, manipulate
addresses, and perform offset calculations.
(b) A pointer to a function is a variable that stores the address of a
function. It can be invoked (called) just like a normal function. This is
useful for callback functions, passing functions as parameters, or creating
arrays of functions.
(c) Advantages of using pointers include:
1. Efficient array and string manipulation
2. Dynamic memory allocation
3. Creation of complex data structures (linked lists, trees, etc.)
4. Flexibility in function handling (callbacks, function tables)
(d) Yes, we can have an array of function pointers. For example:
#include <stdio.h>
int add(int a, int b) {
return a + b; }
int subtract(int a, int b) {
return a - b; }
int main() {
int (*funcArr[])(int, int) = {add, subtract};
printf("%d\n", funcArr[0](5, 3));
printf("%d\n", funcArr[1](5, 3));
return 0; }
(e)
Function returning pointer to int: int* func();
This means the function func returns an address of an integer.
Pointer to function returning int: int (*funcPtr)();
This means funcPtr is a pointer variable that holds the address of a
function which returns an integer.
(f) Program illustrating passing a character array (string) to a function
using pointers:
#include <stdio.h>
void printString(char *str) {
printf("%s\n", str); }
int main() {
char arr[] = "Hello";
printString(arr);
return 0; }
(g) Example showing how an integer array can be passed to a function:
#include <stdio.h>
void printArray(int *arr, int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]); }
printf("\n"); }
int main() {
int nums[5] = {1, 2, 3, 4, 5};
printArray(nums, 5);
return 0;
}
9) Write a program to define a structure for a hotel that has
members - name, address, grade, number of rooms, and room
charges. Write a function to print the names of a hotel in a
particular grade. Also write a function to print names of a hotel
that have room charges less than the specified value.
#include <stdio.h>
struct Hotel {
char name[50];
char address[100];
char grade;
int rooms;
float roomCharges;
};
int main() {
int n;
printf("Enter number of hotels: ");
scanf("%d", &n);
struct Hotel hotels[n];
for (int i = 0; i < n; i++) {
printf("\nEnter details for hotel %d:\n", i + 1);
printf("Name: ");
scanf(" %[^\n]", hotels[i].name);
printf("Address: ");
scanf(" %[^\n]", hotels[i].address);
printf("Grade (e.g., A, B, C): ");
scanf(" %c", &hotels[i].grade);
printf("Number of rooms: ");
scanf("%d", &hotels[i].rooms);
printf("Room charges: ");
scanf("%f", &hotels[i].roomCharges);
char searchGrade;
float maxCharge;
printf("\nEnter grade to search: ");
scanf(" %c", &searchGrade);
printf("\nWithin the requested grade (%c) we have:\n", searchGrade);
int countGrade = 0;
for (int i = 0; i < n; i++) {
if (hotels[i].grade == searchGrade) {
countGrade++;
printf("%d) %s - %.2f\n", countGrade, hotels[i].name,
hotels[i].roomCharges);
if (countGrade == 0) {
printf("No hotels found with grade %c.\n", searchGrade);
printf("\nEnter maximum room charge: ");
scanf("%f", &maxCharge);
printf("\nHotels that come within your budget (%.2f):\n", maxCharge);
int countBudget = 0;
for (int i = 0; i < n; i++) {
if (hotels[i].roomCharges < maxCharge) {
countBudget++;
printf("%d) %s (%c) - %.2f\n", countBudget, hotels[i].name,
hotels[i].grade, hotels[i].roomCharges);
if (countBudget == 0) {
printf("No hotels found within this budget.\n"); }
return 0; }
OUTPUT:
10) Write a program to maintain a record of “n” employee detail
using an array of structures with three fields(id, name , salary)
and print the details of employees whose salary is above 5000.
#include <stdio.h>
#define MAX 100
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
int n;
printf("Enter number of employees: ");
scanf("%d", &n);
struct Employee emp[n];
for (int i = 0; i < n; i++) {
emp[i].id = i + 1;
printf("\nEnter details for employee %d:\n", emp[i].id);
printf("Name: ");
scanf(" %[^\n]", emp[i].name);
printf("Salary: ");
scanf("%f", &emp[i].salary);
printf("\nEmployees with salary above Rs. 5000:\n");
for (int i = 0; i < n; i++) {
if (emp[i].salary > 5000)
printf("ID: %d, Name: %s, Salary: %.2f\n", emp[i].id, emp[i].name,
emp[i].salary);
}
return 0;
OUTPUT:
11) Declare a structure fraction that has two fields numerator and
denominator. Create two variables and compare them using a
function. Return 0 if the two fractions are equal, -1 if the first
fraction is less than the second and 1 otherwise. You may convert
a fraction into a floating point number for your convenience.
#include <stdio.h>
struct Fraction {
int numerator;
int denominator;
};
int compareFraction(struct Fraction f1, struct Fraction f2) {
float val1 = (float)f1.numerator / f1.denominator;
float val2 = (float)f2.numerator / f2.denominator;
if (val1 == val2)
return 0;
else if (val1 < val2)
return -1;
else
return 1;
int main() {
struct Fraction f1, f2;
printf("Enter numerator and denominator for the first fraction: ");
scanf("%d %d", &f1.numerator, &f1.denominator);
printf("Enter numerator and denominator for the second fraction: ");
scanf("%d %d", &f2.numerator, &f2.denominator);
int result = compareFraction(f1, f2);
if (result == 0)
printf("The fractions are equal.\n");
else if (result < 0)
printf("The first fraction is less than the second.\n");
else
printf("The first fraction is greater than the second.\n");
return 0;
OUTPUT:
12) Declare a structure POINT. Input the co-ordinates of a point
variable and determine the quadrant in which it lies. The
following table can be used to determine the quadrant. Quadrant
X Y 1 Positive Positive 2 Negative Positive 3 Negative Negative 4
Positive negative.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point pt;
printf("Enter the coordinates (x y): ");
scanf("%d %d", &pt.x, &pt.y);
if(pt.x > 0 && pt.y > 0)
printf("The point lies in Quadrant 1.\n");
else if(pt.x < 0 && pt.y > 0)
printf("The point lies in Quadrant 2.\n");
else if(pt.x < 0 && pt.y < 0)
printf("The point lies in Quadrant 3.\n");
else if(pt.x > 0 && pt.y < 0)
printf("The point lies in Quadrant 4.\n");
else
printf("The point lies on one of the axes.\n");
return 0;
OUTPUT:
13) Define a structure date containing three integers day, month,
and year. Write a program using functions to read data, to
validate the date entered by the user and then print the date on
the screen. For example, if you enter 29,2,2010 then that is an
invalid date as 2010 is not a leap year. Similarly 31,6,2007 is
invalid as June does not has 31 days.
#include <stdio.h>
#include <stdbool.h>
struct Date {
int day;
int month;
int year;
};
bool isLeapYear(int year) {
if(year % 400 == 0)
return true;
if(year % 100 == 0)
return false;
if(year % 4 == 0)
return true;
return false;
int main() {
struct Date d;
printf("Enter date (day month year): ");
scanf("%d %d %d", &d.day, &d.month, &d.year);
int maxDays;
if(d.month < 1 || d.month > 12) {
printf("Invalid month.\n");
return 1;
if(d.month == 2)
maxDays = isLeapYear(d.year) ? 29 : 28;
else if(d.month == 4 || d.month == 6 || d.month == 9 || d.month ==
11)
maxDays = 30;
else
maxDays = 31;
if(d.day < 1 || d.day > maxDays) {
printf("Invalid day for the given month and year.\n");
return 1;
printf("Valid date: %02d/%02d/%04d\n", d.day, d.month, d.year);
return 0;
OUTPUT: