Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
LAB ASSIGNMENT 4
1.1 write the program in C to demonstrate the use of &( address of )and *(value at address)
operator .
Source code -
#include <stdio.h>
int main()
{
int x = 10;
int *p;
p = &x;
printf("Value of x = %d\n", x);
printf("Address of x = %p\n", &x);
printf("Value of p = %p\n", p);
printf("Address of p = %p\n", &p);
printf("Value at address p = %d\n", *p);
return 0;
}
Output
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
1.2.Write a programme in c to compute the sum of all elements in an array using pointer.
Source code -
#include <stdio.h>
int main()
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int *p;
int sum = 0;
p = arr;
for (int i = 0; i < n; i++)
sum += *p;
p++;
printf("The sum of the array elements is %d\n", sum);
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
return 0;
Output-
2. write a programme in c to swap two no.s using pointer.
Source code-
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
{
int x = 10;
int y = 20;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output-
3.write a programme in c to calculate the length of string using a pointer.
Source code -
#include <stdio.h>
int length(char *s)
{
int count = 0;
while (*s != '\0')
{
count++;
s++;
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
}
return count;
}
int main()
{
char str[] = "Hello world";
printf("The length of the string '%s' is %d\n", str, length(str));
return 0;
}
Output-
4. Create a structure named students to specify data on students given below roll number,
name, department of course, year of joining and then display record of 10 students by using
array of structure.
Source code-
#include <stdio.h>
struct student
{
int roll_no;
char name[20];
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
char dept[10];
char course[10];
int year;
};
int main()
{
struct student s[10];
int i;
printf("Enter the details of 10 students:\n");
for (i = 0; i < 10; i++)
{
printf("Enter roll number: ");
scanf("%d", &s[i].roll_no);
printf("Enter name: ");
scanf("%s", s[i].name);
printf("Enter department: ");
scanf("%s", s[i].dept);
printf("Enter course: ");
scanf("%s", s[i].course);
printf("Enter year of joining: ");
scanf("%d", &s[i].year);
printf("\n");
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
printf("The details of 10 students are:\n");
for (i = 0; i < 10; i++)
{
printf("Roll number: %d\n", s[i].roll_no);
printf("Name: %s\n", s[i].name);
printf("Department: %s\n", s[i].dept);
printf("Course: %s\n", s[i].course);
printf("Year of joining: %d\n", s[i].year);
printf("\n");
}
return 0;
}
Output-
5. 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 low of rupees
100.
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
Source code -
#include <stdio.h>
struct customer
{
int acc_no;
char name[20];
int balance;
};
void print_low_balance(struct customer c[], int n)
{
int i;
int count = 0;
printf("The details of customers with balance below 100 are:\n"); // print the message
for (i = 0; i < n; i++)
{
if (c[i].balance < 100)
{
printf("Account number: %d\n", c[i].acc_no);
printf("Name: %s\n", c[i].name);
printf("Balance: %d\n", c[i].balance);
printf("\n");
count++;
}
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
}
if (count == 0)
{
printf("No customer has balance below 100.\n");
}
}
int main()
{
struct customer c[20];
int n;
int i;
printf("Enter the number of customers: ");
scanf("%d", &n);
printf("Enter the details of %d customers:\n", n);
for (i = 0; i < n; i++)
{
printf("Enter account number: ");
scanf("%d", &c[i].acc_no);
printf("Enter name: ");
scanf("%s", c[i].name);
printf("Enter balance: ");
scanf("%d", &c[i].balance);
printf("\n");
Aaditya Bhatnagar
0801IP221002
Sec - G
Roll no. AB22G083
print_low_balance(c, n);
return 0;
}
Output-
specify data of customers in a bank. The data to be stored is account number, name, balance in