NAME: AADITYA BHATNAGAR
ROLL NUMBER: AB22G083
ENROLLMENT NUMBER: 0801IP221002
QUESTION 1:
i)Write a program in C to demonstrate the use of &(address of) and *(value
at address) operator.
#include <stdio.h>
int main() {
int num = 10;
int *ptr;
ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value stored in ptr: %p\n", ptr);
printf("Value at the address stored in ptr: %d\n", *ptr);
// Modifying the value using the pointer
*ptr = 20;
printf("\nAfter modifying the value:\n");
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value stored in ptr: %p\n", ptr);
printf("Value at the address stored in ptr: %d\n", *ptr);
return 0;
}
ii) Write a program in C to compute the sum of all elements in an array using
pointer
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < size; i++) {
sum += *ptr;
ptr++; }
printf("Sum of array elements: %d\n", sum);
return 0; }
Question 2;
Write a program in C to Swap two numbers using Pointers
#include<stdio.h>
void swap(int *a, int *b);
int main(){
int x=10,y=20;
printf(“Before swapping x=%d and y=%d \n”,x,y);
swap(&x,&y);
printf(“After swapping x=%d and y=%d”,x,y);
return 0; }
void swap(int *a, int *b){
int temp=*a;
*a = *b;
*b = temp; }
Question 3;
Write a program in C to calculate the length of a string using a pointer
#include <stdio.h>
int stringLength(const char *str) {
int length = 0;
while (*str != '\0') {
length++;
str++; }
return length; }
int main() {
char str[] = "Hello, World!";
int length;
length = stringLength(str);
printf("Length of the string: %d\n", length);
return 0; }
Question 4: Create a structure named Students to specify data on students
given below:
Roll number, Name, Department, Course, Year of joining. And then display
record of 10 students by using array of structure.
#include <stdio.h>
struct Students {
int rollNumber;
char name[50];
char department[50];
char course[50];
int yearOfJoining;
};
int main() {
struct Students students[10];
// Input the records of 10 students
for (int i = 0; i < 10; i++) {
printf("Enter details of student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Name: ");
scanf("%s", students[i].name);
printf("Department: ");
scanf("%s", students[i].department);
printf("Course: ");
scanf("%s", students[i].course);
printf("Year of Joining: ");
scanf("%d", &students[i].yearOfJoining);
printf("\n");
// Display the records of all students
printf("Student Records:\n");
for (int i = 0; i < 10; i++) {
printf("Student %d:\n", i + 1);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Name: %s\n", students[i].name);
printf("Department: %s\n", students[i].department);
printf("Course: %s\n", students[i].course);
printf("Year of Joining: %d\n", students[i].yearOfJoining);
printf("\n");
return 0; }
Question 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 below Rs. 100.
#include <stdio.h>
#define MAX_CUSTOMERS 20
struct Customer {
int accountNumber;
char name[50];
float balance;
};
void printCustomersWithLowBalance(struct Customer customers[], int size)
{
printf("Customers with Balance Below Rs. 100:\n");
for (int i = 0; i < size; i++) {
if (customers[i].balance < 100) {
printf("Account Number: %d\n", customers[i].accountNumber);
printf("Name: %s\n", customers[i].name);
printf("\n");
}
}
int main() {
struct Customer customers[MAX_CUSTOMERS];
int numCustomers;
printf("Enter the number of customers (up to 20): ");
scanf("%d", &numCustomers);
if (numCustomers > MAX_CUSTOMERS) {
printf("Invalid input. Maximum number of customers allowed is %d.\n",
MAX_CUSTOMERS);
return 0;
printf("\n");
for (int i = 0; i < numCustomers; i++) {
printf("Enter details for Customer %d:\n", i + 1);
printf("Account Number: ");
scanf("%d", &customers[i].accountNumber);
printf("Name: ");
scanf("%s", customers[i].name);
printf("Balance: ");
scanf("%f", &customers[i].balance);
printf("\n"); }
printCustomersWithLowBalance(customers, numCustomers);
return 0;