Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
Q1)(i)Write a program in C to demonstrate the use of &(address of) and
*(value at address) operation .
Code:-
#include<stdio.h>
Int main()
{
Int num = 10; //Declaring an integer variable and initializing it with value 10
Printf(“Value of num: %d\n”, num); //Printing the value of num
Printf(“Address of num: %p\n”, &num); //Printing the address of num
Int *ptr; //Declaring a pointer of integer type
Ptr = # //Initializing the pointer with the address of num
Printf(“Value at address %p: %d\n”, ptr, *ptr); //Printing the value at the address ptr is pointing to
Return 0;
}
Compiled Output:
Value of num: 10
Address of num: 0x7fffd9008a2c
Value at address 0x7fffd9008a2c: 10
[Process completed – press Enter]
ii) Write a program in C to compute the sum of all elements in an array using
pointer.
Code:-
#include<stdio.h>
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
#define MAX_SIZE 100 //Maximum array size
Int main()
{
Int arr[MAX_SIZE];
Int I, n, sum = 0;
Int *ptr; //Pointer to array element
Printf(“Enter size of the array: “);
Scanf(“%d”, &n);
Printf(“Enter elements in array:\n”);
For(i=0; i<n; i++)
{
Scanf(“%d”, &arr[i]);
}
/* ptr = arr is equivalent to ptr = &arr[0] */
Ptr = arr;
For(i=0; i<n; i++)
{
Sum = sum + *(ptr + i);
}
Printf(“Sum of array elements: %d”, sum);
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
Return 0;
}
Compiled Output:-
Enter size of the array: 5
Enter elements in array:
2
4
6
8
10
Sum of array elements: 30
Q2 Write a program in C to Swap two numbers using Pointers.
Code:-
#include <stdio.h>
Void swap(int *a, int *b);
Int main() {
Int num1, num2;
Printf(“Enter first number: “);
Scanf(“%d”, &num1);
Printf(“Enter second number: “);
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
Scanf(“%d”, &num2);
// call the function to swap two numbers
Swap(&num1, &num2);
Printf(“After swapping, the first number is %d and the second number is %d”, num1, num2);
Return 0;
}
Void swap(int *a, int *b) {
Int temp;
Temp = *a;
*a = *b;
*b = temp;
}
Complied Output:-
Enter first number: 62
Enter second number: 73
After swapping, the first number is 73 and the second number is 62
[Process completed – press Enter]
Q3.)Write a program in C to calculate the length of a string using a pointer.
Code:-
#include <stdio.h>
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
struct Students {
int rollNo;
char name[50];
char department[50];
char course[50];
int yoj;
};
int main() {
struct Students student[10]; // array of 10 Student structures
// Taking input of records of 10 students
for (int i = 0; i < 10; i++) {
printf("\nEnter details of student %d:\n", i+1);
printf("Roll No: ");
scanf("%d", &student[i].rollNo);
printf("Name: ");
scanf("%s", &student[i].name);
printf("Department: ");
scanf("%s", &student[i].department);
printf("Course: ");
scanf("%s", &student[i].course);
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
printf("Year of Joining: ");
scanf("%d", &student[i].yoj);
}
// Displaying the recorded information of students
printf("\nDisplaying Record of 10 students:\n");
for (int i = 0; i < 10; i++) {
printf("\nRecord of student %d:\n", i+1);
printf("Roll No.: %d\n", student[i].rollNo);
printf("Name: %s\n", student[i].name);
printf("Department: %s\n", student[i].department);
printf("Course: %s\n", student[i].course);
printf("Year of Joining: %d\n", student[i].yoj);
}
return 0;
}
Complied Output:-
Enter details of student 1:
Roll No: 69
Name: Adarsh
Department: IT
Course: Btech
Year of Joining: 2022
Enter details of student 2:
Roll No: 23
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
Name: Arpan
Department: Civil
Course: Btech
Year of Joining: 2023
Enter details of student 3:
Roll No: 39
Name: Siddhart
Department: CS
Course: BTech
Year of Joining: 2022
Continue …
Q5)Create a structure to specify data of customers in a bank. The data to be
stored is:
Account number,
Name,
Balance in account.
Assume maximum of 20 customers in the bank.
Write a function to print the Account number and name of each customer
with balance below Rs. 100.
Code:-
#include <stdio.h>
struct BankCustomer {
int accountNo;
char name[50];
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
float balance;
};
void printLowBalCustomers(struct BankCustomer customers[], int n) {
for (int i = 0; i < n; i++) {
if (customers[i].balance < 100) {
printf("Account No.: %d\nName: %s\n", customers[i].accountNo, customers[i].name);
}
}
}
int main() {
struct BankCustomer customers[20]; // Array of customers
int n;
printf("Enter number of bank customers: ");
scanf("%d", &n);
// Taking input of data of each customer
for (int i = 0; i < n; i++) {
printf("\nEnter details of customer %d:\n", i+1);
printf("Account number: ");
scanf("%d", &customers[i].accountNo);
printf("Name: ");
scanf("%s", &customers[i].name);
printf("Balance: ");
scanf("%f", &customers[i].balance);
}
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010
// Printing the details of customers with balance less than Rs. 100
printf("\nCustomers with balance less than Rs. 100:\n");
printLowBalCustomers(customers, n);
return 0;
}
Compiled Output:-
Enter number of bank customers: 2
Enter details of customer 1:
Account number: 0801221010
Name: Adarsh
Balance: 10000
Enter details of customer 2:
Account number: 0801221037
Name: Arpan
Balance: 89
Customers with balance less than Rs. 100:
Account No.: 801221037
Name: Arpan
[Process completed – press Enter]
Lab Assignment 06
CO10507
Name:- Adarsh Patel
Roll no.:-AB22G069
Enroll no. :-0801IT221010