Simple Pointers in C.
1. Write a program to read two integers using pointers and perform all
arithmetic operations on them.
#include <stdio.h>
int main() {
int num1, num2, sum, diff, prod, quot, rem;
int *ptr1, *ptr2;
printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);
ptr1 = &num1;
ptr2 = &num2;
sum = *ptr1 + *ptr2;
diff = *ptr1 - *ptr2;
prod = *ptr1 * *ptr2;
quot = *ptr1 / *ptr2;
rem = *ptr1 % *ptr2;
printf("Sum: %d\n", sum);
printf("Difference: %d\n", diff);
printf("Product: %d\n", prod);
printf("Quotient: %d\n", quot);
printf("Remainder: %d\n", rem);
return 0;
}
2. Write a program to accept an integer using pointer and check whether
it is even or odd.
#include <stdio.h>
void checkEvenOrOdd(int *num) {
if (*num % 2 == 0) {
printf("%d is even.\n", *num);
} else {
printf("%d is odd.\n", *num);
}
}
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
checkEvenOrOdd(&num);
return 0;
}
3. Write a program to find maximum from two integers using pointers.
#include <stdio.h>
void findMax(int *num1, int *num2) {
if (*num1 > *num2) {
printf("%d is greater.\n", *num1);
} else {
printf("%d is greater.\n", *num2);
}
}
int main() {
int num1, num2;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
findMax(&num1, &num2);
return 0;
}
4. Write a program to display the elements of an array containing n
integers in the reverse
order using a pointer to the array.
#include <stdio.h>
void displayReverse(int *arr, int n) {
printf("The array in reverse order is: ");
for (int i = n-1; i >= 0; i--) {
printf("%d ", *(arr+i));
}
printf("\n");
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
displayReverse(arr, n);
return 0;
}
Assignment 2:Dynamicmemory allocation in C.
3. Write a program to accept N integers and store them dynamically
display them in
reverse order.
#include <stdio.h>
#include <stdlib.h>
void displayReverse(int *arr, int n) {
printf("The array in reverse order is: ");
for (int i = n-1; i >= 0; i--) {
printf("%d ", *(arr+i));
}
printf("\n");
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *arr = (int *) malloc(n * sizeof(int));
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
displayReverse(arr, n);
free(arr);
return 0;
}
4. Write a program to allocate memory dynamically for n integers such
that the memory is
Initialized to 0. And display it.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int *arr = (int *) calloc(n, sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("The array with %d integers initialized to 0 is: ", n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
Assignment 3: String handling in C and standard library functions for
strings
1. Write a program to read a string and copy it to another sting and
display copied string.
Also the length of copied string. (Use built in functions)
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter a string: ");
scanf("%s", str1);
// Copy the string from str1 to str2
strcpy(str2, str1);
printf("The copied string is: %s\n", str2);
printf("The length of the copied string is: %ld\n", strlen(str2));
return 0;
}
2. Write a program to read two strings. If first string is greater than
second then
concatenate second to first and display concatenated string, If first
string is smaller than
second then concatenate first to second, other-wise display length of
string. (use strcmp)
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], concat[200];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
int cmp = strcmp(str1, str2);
if (cmp > 0) {
strcpy(concat, str1);
strcat(concat, str2);
printf("The concatenated string is: %s\n", concat);
}
else if (cmp < 0) {
strcpy(concat, str2);
strcat(concat, str1);
printf("The concatenated string is: %s\n", concat);
}
else {
printf("The length of the strings is: %ld\n", strlen(str1));
}
return 0;
}
3.Write a program to read a string and one character. Check whether given
character is
present in the given string or not? (Hint: use strchr or strrchr)
#include <stdio.h>
#include <string.h>
int main() {
char str[100], ch, *result;
printf("Enter a string: ");
scanf("%s", str);
printf("Enter a character to search for: ");
scanf(" %c", &ch);
result = strchr(str, ch);
if (result == NULL) {
printf("The character '%c' is not present in the string '%s'.\n", ch,
str);
}
else {
printf("The character '%c' is present in the string '%s'.\n", ch,
str);
}
return 0;
}
Assignment 4: String handling using user defined function and
pointers.
1. Write a program to accept a string and find its length using user
defined function.
(Don’t use pointers)
#include <stdio.h>
int string_length(char str[]);
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int len = string_length(str);
printf("The length of the string '%s' is %d.\n", str, len);
return 0;
}
int string_length(char str[]) {
int len = 0;
while (str[len] != '\0') {
len++;
}
return len;
}
2. Write a function that takes a string as parameterand returns the same
string in upper
case(use pointes). Accept this string in main and display converted
string in main only.
#include <stdio.h>
#include <ctype.h>
void str_to_upper(char *str);
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
str_to_upper(str);
printf("The converted string is: %s\n", str);
return 0;
}
void str_to_upper(char *str) {
while (*str != '\0') {
*str = toupper(*str);
str++;
}
}
3. Write a function to find reverse of the string and use it in main.
#include <stdio.h>
#include <string.h>
void reverse_string(char *str);
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverse_string(str);
printf("The reversed string is: %s\n", str);
return 0;
}
void reverse_string(char *str) {
int i, j;
char temp;
for (i = 0, j = strlen(str) - 1; i < j; i++, j--) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
Assignment 5: Structures and union in C
1. Create a structure employee (id, name, salary). Accept details of n
employees and
display display it in summary format.
#include <stdio.h>
struct employee {
int id;
char name[50];
float salary;
};
int main() {
int n, i;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct employee emp[n];
for (i = 0; i < n; i++) {
printf("\nEnter details of employee %d:\n", i+1);
printf("ID: ");
scanf("%d", &emp[i].id);
printf("Name: ");
scanf("%s", emp[i].name);
printf("Salary: ");
scanf("%f", &emp[i].salary);
}
printf("\nEmployee Information:\n");
printf("ID\tName\tSalary\n");
for (i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\n", emp[i].id, emp[i].name, emp[i].salary);
}
return 0;
}
2. Type sample program 2 and execute it.
Ans Refer sample program 2
Assignment 6: File Handling (Text Files)
1. Write a program to accept a file name and a single character. Display
the count
indicating total number of times character occurs in thefile
#include <stdio.h>
int main() {
char filename[50], ch;
int count = 0;
FILE *fp;
printf("Enter the file name: ");
scanf("%s", filename);
printf("Enter a single character: ");
scanf(" %c", &ch);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
while (fgetc(fp) != EOF) {
if (fgetc(fp) == ch) {
count++;
}
}
fclose(fp);
printf("Total occurrences of %c in file %s: %d\n", ch, filename,
count);
return 0;
}
2. Write a program to accept two filenames. Copy the contents of the
first file to the second
such that the case of all alphabets is reversed.
#include <stdio.h>
#include <ctype.h>
int main() {
char filename1[50], filename2[50];
char ch;
FILE *fp1, *fp2;
printf("Enter the source file name: ");
scanf("%s", filename1);
printf("Enter the destination file name: ");
scanf("%s", filename2);
fp1 = fopen(filename1, "r");
fp2 = fopen(filename2, "w");
if (fp1 == NULL || fp2 == NULL) {
printf("Error opening file\n");
return 1;
}
while ((ch = fgetc(fp1)) != EOF) {
if (isalpha(ch)) {
if (isupper(ch)) {
ch = tolower(ch);
} else {
ch = toupper(ch);
}
}
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
printf("File contents copied and case reversed successfully.\n");
return 0;
}
3. Write a program to accept a filename and count the number of words,
lines and
characters in thefile.
#include <stdio.h>
int main() {
char filename[50], ch;
FILE *fp;
int wordCount = 0, lineCount = 0, charCount = 0;
printf("Enter the file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
charCount++;
if (ch == '\n') {
lineCount++;
}
if (ch == ' ' || ch == '\t' || ch == '\n') {
wordCount++;
}
}
if (charCount > 0) {
wordCount++;
lineCount++;
}
fclose(fp);
printf("Number of words: %d\n", wordCount);
printf("Number of lines: %d\n", lineCount);
printf("Number of characters: %d\n", charCount);
return 0;
}
Assignment 7: Command line arguments and preprocessor
directives.
1. Write a program to define value of pi and use it to find area and
perimeter of a circle
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area, perimeter;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("The area of the circle is: %lf\n", area);
printf("The perimeter of the circle is: %lf\n", perimeter);
return 0;
}
2. Write a program to display all command line arguments passed to main
in the reverse
order.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Command line arguments in reverse order:\n");
for (int i = argc - 1; i >= 0; i--) {
printf("%s\n", argv[i]);
}
return 0;
}
3. Write a program to accept three integers as command line arguments and
find the
minimum, maximum and average of the three. Display error message if
invalid number
of arguments are entered.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int a, b, c, min, max;
float avg;
if (argc != 4) {
printf("Error: Invalid number of arguments.\n");
printf("Usage: %s <integer1> <integer2> <integer3>\n", argv[0]);
return 1;
}
a = atoi(argv[1]);
b = atoi(argv[2]);
c = atoi(argv[3]);
min = a;
max = a;
avg = (a + b + c) / 3.0;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
printf("Average: %.2f\n", avg);
return 0;
}
4.Define a macro MIN which gives the minimum of two numbers. Use this
macro to find
the minimum of nnumbers.
#include <stdio.h>
#define MIN(x, y) ((x) < (y) ? (x) : (y))
int main() {
int n, i;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int min = arr[0];
for (i = 1; i < n; i++) {
min = MIN(min, arr[i]);
}
printf("Minimum: %d\n", min);
return 0;
}