0% found this document useful (0 votes)
20 views

Advance c Practical Important Questions

The document contains a series of C programming practical questions and solutions covering various topics such as command line arguments, array manipulation, string handling, structures, and memory management. Each section includes code snippets and expected outputs for tasks like reversing an array, finding maximum salary among employees, and calculating average marks. Additionally, it addresses operations with strings, matrices, and user-defined functions.

Uploaded by

sajal4singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Advance c Practical Important Questions

The document contains a series of C programming practical questions and solutions covering various topics such as command line arguments, array manipulation, string handling, structures, and memory management. Each section includes code snippets and expected outputs for tasks like reversing an array, finding maximum salary among employees, and calculating average marks. Additionally, it addresses operations with strings, matrices, and user-defined functions.

Uploaded by

sajal4singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

ADVANCE C PROGRAMMING PRACTICAL IMPORTANT QUESTIONS

S2 -1 Write C program to display arguments and count of arguments passed through command line.

#include <stdio.h>
int main(int argc, char *argv[]) {
int i;
printf("Number of arguments: %d\n", argc);
for (i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
Output :-

gcc -o args args.c


./args hello world

This will produce the following output:


Number of arguments: 3
Argument 0: ./args
Argument 1: hello
Argument 2: world

S2-2 Write C program to accept n numbers in an array and display it in reverse order using pointers.

1) #include <stdio.h>
2)
3) int main() {
4) int n, i, temp;
5) printf("Enter the size of the array: ");
6) scanf("%d", &n);
7) int arr[n];
8) printf("Enter the elements of the array: ");
9) for (i = 0; i < n; i++) {
10) scanf("%d", &arr[i]);
11) }
12) int *ptr1 = arr; // pointer to the first element of the array
13) int *ptr2 = arr + n - 1; // pointer to the last element of the array
14) while (ptr1 < ptr2) {
15) temp = *ptr1;
16) *ptr1 = *ptr2;
17) *ptr2 = temp;
18) ptr1++;
19) ptr2--;
20) }
21) printf("The reversed array is: ");
22) for (i = 0; i < n; i++) {
23) printf("%d ", arr[i]);
24) }
25) return 0;
26) }
27)

S2-3 Write C Program to accept n records of employee information (eno,ename,salary) and display record of employees having maximum
salary.

#include <stdio.h>

struct employee {
int eno;
char ename[20];
float salary;
};
int main()
{
int n, i, max_salary = 0, max_index = 0;
struct employee emp[100];

printf("Enter the number of employees: ");


scanf("%d", &n);

for (i = 0; i < n; i++)


{
printf("Enter the employee number: ");
scanf("%d", &emp[i].eno);

printf("Enter the employee name: ");


scanf("%s", emp[i].ename);

printf("Enter the employee salary: ");


scanf("%f", &emp[i].salary);

if (emp[i].salary > max_salary)


{
max_salary = emp[i].salary;
max_index = i;
}
}
printf("The employee with the maximum salary is:\n");
printf("Employee number: %d\n", emp[max_index].eno);
printf("Employee name: %s\n", emp[max_index].ename);
printf("Employee salary: %f\n", emp[max_index].salary);

return 0;
}

S3-1 Write C program to find string length and compare two strings using predefined string functions

#include <stdio.h>
#include <string.h>

int main() {
char str1[] = "Hello, world!";
char str2[] = "Goodbye, world!";

// Find the length of str1


int len1 = strlen(str1);
printf("The length of str1 is %d\n", len1);

// Find the length of str2


int len2 = strlen(str2);
printf("The length of str2 is %d\n", len2);

// Compare str1 and str2


int cmp = strcmp(str1, str2);
if (cmp == 0) {
printf("str1 and str2 are equal\n");
} else if (cmp < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}
return 0;
}

S3-2 Write C program to calculate average marks of “n” number of students using pointers and array

#include <stdio.h>

int main()
{
int n, i, sum = 0;
float average;
int *marks;
printf("Enter the number of students: ");
scanf("%d", &n);
marks = (int *)malloc(n * sizeof(int));
printf("Enter the marks of the students: ");
for (i = 0; i < n; i++)
{
scanf("%d", &marks[i]);
}
for (i = 0; i < n; i++)
{
sum += marks[i];
}
average = (float)sum / n;
printf("The average marks of the students is: %f", average);
free(marks);
return 0;
}
S3-3 Write a program to Generate Fibonacci Series of N Numbers using Command-Line
Argument.

#include <stdio.h>

int main(int argc, char *argv[]) {


int n = atoi(argv[1]);
int a = 0, b = 1;
printf("%d %d", a, b);
for (int i = 2; i < n; i++) {
int nextTerm = a + b;
a = b;
b = nextTerm;
printf(" %d", nextTerm);
}
printf("\n");
return 0;
}

S4-1 Write C program to interchange values of two numbers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

S4-2 Write a program to compare two strings. If they are not equal display their length
and if equal concatenate them.

#include <stdio.h>
#include <string.h>

int main() {
char str1[20], str2[20];
int len1, len2;

printf("Enter the first string: ");


scanf("%s", str1);

printf("Enter the second string: ");


scanf("%s", str2);

len1 = strlen(str1);
len2 = strlen(str2);

if (strcmp(str1, str2) != 0) {
printf("The strings are not equal.\n");
printf("The length of the first string is %d.\n", len1);
printf("The length of the second string is %d.\n", len2);
} else {
printf("The strings are equal.\n");
strcat(str1, str2);
printf("The concatenated string is %s.\n", str1);
}

return 0;
}

S4-3 Write C Program to accept book information (book_id, book_name, book_author).


Read the details of n number of books. Display book details of book.

#include <stdio.h>

struct book
{
int book_id;
char book_name[50];
char book_author[50];
};

int main()
{
int n;
struct book b[100];

printf("Enter the number of books: ");


scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("\nEnter details for book %d:\n", i + 1);

printf("Enter book id: ");


scanf("%d", &b[i].book_id);

printf("Enter book name: ");


scanf("%s", b[i].book_name);

printf("Enter book author: ");


scanf("%s", b[i].book_author);
}

printf("\n\nBook details:\n");

for (int i = 0; i < n; i++)


{
printf("\nBook %d\n", i + 1);
printf("Book id: %d\n", b[i].book_id);
printf("Book name: %s\n", b[i].book_name);
printf("Book author: %s\n", b[i].book_author);
}

return 0;
}

S5-1 Write C program to check given number is even or odd using macros.
#include <stdio.h>

#define EVEN(x) (x % 2 == 0)
#define ODD(x) (x % 2 != 0)

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (EVEN(num)) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}

return 0;
}

S5-2 Write C program to find string length and reverse string using pointers and user defined
function.

#include <stdio.h>
#include <string.h>

int main()
{
char str[100];
int length;

printf("Enter a string: ");


scanf("%s", str);

length = strlen(str);

printf("The length of the string is: %d\n", length);

reverse_string(str);

printf("The reversed string is: %s\n", str);

return 0;
}

void reverse_string(char *str)


{
char *begin, *end, temp;

begin = str;
end = str + strlen(str) - 1;

while (begin < end)


{
temp = *begin;
*begin = *end;
*end = temp;

begin++;
end--;
}
}

S5-3 Write C Program to accept n Author information (authorid, author_name, address) and
store it in structure and display the same.

#include <stdio.h>

struct author {
int authorid;
char authorname[50];
char address[100];
};

int main() {
struct author author;

printf("Enter author ID: ");


scanf("%d", &author.authorid);

printf("Enter author name: ");


scanf("%s", author.authorname);

printf("Enter author address: ");


scanf("%s", author.address);

printf("\nAuthor Information:\n");
printf("Author ID: %d\n", author.authorid);
printf("Author Name: %s\n", author.authorname);
printf("Author Address: %s\n", author.address);

return 0;
}

S6-1 Write C program to accept student information (rollno, name, marks) and display same
information using structure.
#include <stdio.h>

struct student {
int rollno;
char name[50];
float marks;
};

int main() {
struct student s;

printf("Enter student information:\n");

// Accept roll number


printf("Roll number: ");
scanf("%d", &s.rollno);

// Accept name
printf("Name: ");
scanf("%s", s.name);

// Accept marks
printf("Marks: ");
scanf("%f", &s.marks);

// Display student information


printf("\nStudent information:\n");
printf("Roll number: %d\n", s.rollno);
printf("Name: %s\n", s.name);
printf("Marks: %.2f\n", s.marks);
return 0;
}

S6-2 Write C program to count number of vowels, consonants, and words in given sentence.

#include <stdio.h>

int main() {
char sentence[100];
int vowels = 0;
int consonants = 0;
int words = 1;

printf("Enter a sentence: ");


gets(sentence);

for (int i = 0; sentence[i] != '\0'; i++) {


if (sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] == 'i' || sentence[i] == 'o' || sentence[i]
== 'u') {
vowels++;
} else if (sentence[i] >= 'a' && sentence[i] <= 'z') {
consonants++;
} else if (sentence[i] == ' ') {
words++;
}
}

printf("Number of vowels: %d\n", vowels);


printf("Number of consonants: %d\n", consonants);
printf("Number of words: %d\n", words);

return 0;
}

S6-3 Write C program to accept three integers as command line arguments and find the
minimum, maximum and average of the three numbers. Display error message if the
number of arguments entered are invalid.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {


if (argc != 4) {
printf("Error: Invalid number of arguments\n");
printf("Usage: %s <num1> <num2> <num3>\n", argv[0]);
return 1;
}

int num1 = atoi(argv[1]);


int num2 = atoi(argv[2]);
int num3 = atoi(argv[3]);

int min = num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);
int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
double avg = (num1 + num2 + num3) / 3.0;

printf("Minimum: %d\n", min);


printf("Maximum: %d\n", max);
printf("Average: %.2f\n", avg);

return 0;
}
S7-2 Write C program that accepts names of n cities and write functions for the following:
a) Search for a city
b) Display the longest names

#include <stdio.h>
#include <string.h>

#define MAX_CITIES 100


#define MAX_CITY_LENGTH 50

void searchCity(char cities[][MAX_CITY_LENGTH], int n, char *city) {


for (int i = 0; i < n; i++) {
if (strcmp(cities[i], city) == 0) {
printf("City found at position %d.\n", i + 1);
return;
}
}
printf("City not found.\n");
}

void displayLongestNames(char cities[][MAX_CITY_LENGTH], int n) {


int maxLength = 0;
for (int i = 0; i < n; i++) {
int len = strlen(cities[i]);
if (len > maxLength)
maxLength = len;
}
printf("Longest city names:\n");
for (int i = 0; i < n; i++) {
if (strlen(cities[i]) == maxLength)
printf("%s\n", cities[i]);
}
}

int main() {
char cities[MAX_CITIES][MAX_CITY_LENGTH];
int n;
printf("Enter the number of cities: ");
scanf("%d", &n);

printf("Enter names of cities:\n");


for (int i = 0; i < n; i++) {
printf("City %d: ", i + 1);
scanf("%s", cities[i]);
}

char searchCityName[MAX_CITY_LENGTH];
printf("\nEnter the name of the city to search: ");
scanf("%s", searchCityName);
searchCity(cities, n, searchCityName);

displayLongestNames(cities, n);

return 0;
}

S7-3 Write C program to accept a matrix of size 3x3 and print the same using pointer.

#include <stdio.h>

int main() {
int matrix[3][3];
int *ptr = &matrix[0][0];
// Accept the matrix from the user
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", ptr);
ptr++;
}
}

// Print the matrix using pointer


printf("The matrix is:\n");
ptr = &matrix[0][0];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", *ptr);
ptr++;
}
printf("\n");
}

return 0;
}

S8-1 Write C Program to check whether a number is positive, negative or zero using macros.

#include <stdio.h>

#define IS_POSITIVE(x) ((x) > 0)


#define IS_NEGATIVE(x) ((x) < 0)
#define IS_ZERO(x) ((x) == 0)

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (IS_POSITIVE(num)) {
printf("%d is positive.\n", num);
} else if (IS_NEGATIVE(num)) {
printf("%d is negative.\n", num);
} else {
printf("%d is zero.\n", num);
}

return 0;
}

S8-3 Write C program to store n student information(rollno, name, percentage) structure and
display the same in ascending order of name of student.

#include <stdio.h>

struct student {
int rollno;
char name[50];
float percentage;
};

void main() {
int n, i, j;
struct student s[100];

printf("Enter the number of students: ");


scanf("%d", &n);

for (i = 0; i < n; i++) {


printf("Enter the roll number, name and percentage of student %d: ", i + 1);
scanf("%d %s %f", &s[i].rollno, s[i].name, &s[i].percentage);
}

// Sort the students in ascending order of name


for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (strcmp(s[j].name, s[j + 1].name) > 0) {
struct student temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}

// Display the students in ascending order of name


printf("The students in ascending order of name are:\n");
for (i = 0; i < n; i++) {
printf("%d %s %.2f\n", s[i].rollno, s[i].name, s[i].percentage);
}
}

S9-1 Write C Program to find minimum of three numbers using nested macros.

#include <stdio.h>

#define min(a, b) ((a) < (b) ? (a) : (b))


#define minthree(a, b, c) min(min(a, b), c)
int main() {
int x, y, z;

printf("Enter three numbers: ");


scanf("%d %d %d", &x, &y, &z);

int min_value = minthree(x, y, z);

printf("The minimum of three numbers is %d\n", min_value);

return 0;
}

S9-2 Write C program to create a student structure having fields roll_no, stud_name and
address. Accept the details of ‘n’ students into the structure, rearrange the data in
alphabetical order of stud_name and display the result.

#include <stdio.h>
#include <stdlib.h>

struct student {
int roll_no;
char stud_name[50];
char address[100];
};

int main() {
int n, i, j;

printf("Enter the number of students: ");


scanf("%d", &n);
struct student *students = (struct student *)malloc(n * sizeof(struct student));

for (i = 0; i < n; i++) {


printf("Enter the details of student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].roll_no);
printf("Student name: ");
scanf("%s", students[i].stud_name);
printf("Address: ");
scanf("%s", students[i].address);
}

// Rearrange the data in alphabetical order of stud_name


for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (strcmp(students[i].stud_name, students[j].stud_name) > 0) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}

// Display the result


printf("\nThe students in alphabetical order of stud_name are:\n");
for (i = 0; i < n; i++) {
printf("%d. %s\n", students[i].roll_no, students[i].stud_name);
}

free(students);

return 0;
}

S9-3 Write C program which accepts a sentence from the user and replaces all lower case letters by uppercase
letters.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
char str[100];
int i;

printf("Enter a sentence: ");


fgets(str, 100, stdin);

for (i = 0; str[i] != '\0'; i++) {


if (islower(str[i])) {
str[i] = toupper(str[i]);
}
}

printf("The sentence in uppercase is: %s", str);

return 0;
}

S10-1 Write C program to find square of number using macro.

#include <stdio.h>
// Define macro to find square and cube
#define SQUARE(x) (x * x)
#define CUBE(x) (x * x * x)

int main()
{
int num;

// Input a number from user


printf("Enter any number to find square and cube: ");
scanf("%d", &num);

// Calculate and print square


printf("SQUARE(%d) = %d\n", num, SQUARE(num));

// Calculate and print cube


printf("CUBE(%d) = %d\n", num, CUBE(num));

return 0;
}

S10-2 Write C program to to store and access “ name, subject and percentage” for two
student.(using union)

#include <stdio.h>
#include <string.h>

union student {
char name[40];
int subject;
float percentage;
};
int main() {
union student s1, s2;

// Store data for student 1


strcpy(s1.name, "John Doe");
s1.subject = 101;
s1.percentage = 85.5;

// Store data for student 2


strcpy(s2.name, "Jane Doe");
s2.subject = 102;
s2.percentage = 90.0;

// Access and print data for student 1


printf("Student 1:\n");
printf("Name: %s\n", s1.name);
printf("Subject: %d\n", s1.subject);
printf("Percentage: %.2f\n", s1.percentage);

// Access and print data for student 2


printf("\nStudent 2:\n");
printf("Name: %s\n", s2.name);
printf("Subject: %d\n", s2.subject);
printf("Percentage: %.2f\n", s2.percentage);

return 0;
}
S10-3 Write a program to remove all other characters in a string except alphabets.

#include <stdio.h>
#include <string.h>

int main() {
char str[150];
int i, j;

printf("Enter a string: ");


gets(str);

for (i = 0; str[i] != '\0'; ++i) {


while (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))) {
for (j = i; str[j] != '\0'; ++j) {
str[j] = str[j + 1];
}
str[j] = '\0';
}
}

printf("Resultant string: %s", str);

return 0;
}
S11-1 Write C program to multiply two numbers using function pointer.

#include <stdio.h>

int multiply(int a, int b) {


return a * b;
}

int main() {
int a = 10;
int b = 20;

// Declare a function pointer


int (*fptr)(int, int);

// Assign the address of the multiply function to the function pointer


fptr = &multiply;

// Call the function using the function pointer


int product = fptr(a, b);

// Print the product


printf("Product: %d\n", product);

return 0;
}
S11-2 Write C program to declare a structure "employee"(name,age,salary) which contains
another structure "address"(house number,street) as member variable.Accept the details of one employee and
display it.(using pointer variable)

#include <stdio.h>

struct address
{
int house_number;
char street[100];
};

struct employee
{
char name[50];
int age;
float salary;
struct address address;
};

int main()
{
struct employee emp;
struct employee *ptr;

ptr = &emp;

printf("Enter the name of the employee: ");


scanf("%s", ptr->name);

printf("Enter the age of the employee: ");


scanf("%d", &ptr->age);

printf("Enter the salary of the employee: ");


scanf("%f", &ptr->salary);

printf("Enter the house number of the employee: ");


scanf("%d", &ptr->address.house_number);

printf("Enter the street of the employee: ");


scanf("%s", ptr->address.street);

printf("\nEmployee Details\n");
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Salary: %.2f\n", ptr->salary);
printf("House Number: %d\n", ptr->address.house_number);
printf("Street: %s\n", ptr->address.street);

return 0;
}

S11-3 Write C program to find sum of n elements entered by user. To perform this, allocate memory dynamically
using malloc() function

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, sum = 0;
int *ptr;

printf("Enter the number of elements: ");


scanf("%d", &n);

ptr = (int *)malloc(n * sizeof(int));


if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

for (i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);
scanf("%d", &ptr[i]);
}

for (i = 0; i < n; i++) {


sum += ptr[i];
}

printf("The sum of the elements is: %d\n", sum);


free(ptr);
return 0;
}

S12-2 Write a program to accept three integers as command line arguments and find the
minimum, maximum and average of the three numbers. Display error message if the number of arguments entered are
invalid.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Error: Invalid number of arguments. Please provide three integers.\n");
return 1;
}

int num1, num2, num3;


num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
num3 = atoi(argv[3]);

int min, max;


float average;

// Finding minimum
min = num1 < num2 ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);

// Finding maximum
max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);

// Finding average
average = (num1 + num2 + num3) / 3.0;

printf("Minimum: %d\n", min);


printf("Maximum: %d\n", max);
printf("Average: %.2f\n", average);

return 0;
}

gcc program.c - o program


./program 10 20 30
Minimum: 10
Maximum: 30
Average: 20.00

./program 10 20
Error: Invalid number of arguments. Please provide three integers.

S12-3 Write C program to create Structure employee having fields emp_id, emp_name,
designation. Pass this entire structure to function and display the structure elements using
pointers.

#include <stdio.h>

struct employee {
int emp_id;
char emp_name[50];
char designation[50];
};

void display_employee(struct employee *emp) {


printf("Employee ID: %d\n", emp->emp_id);
printf("Employee Name: %s\n", emp->emp_name);
printf("Designation: %s\n", emp->designation);
}

int main() {
struct employee emp1;

// Initialize the structure elements


emp1.emp_id = 1;
strcpy(emp1.emp_name, "John Doe");
strcpy(emp1.designation, "Software Engineer");

// Pass the structure to the function


display_employee(&emp1);

return 0;
}

S13-1 Write C program to interchange values of two numbers using pointers.

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// calling the swap function


swap(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}
S13-3 Write C program to create a student structure having fields roll_no, stud_name and address. Accept the
details of ‘n’ students into the structure, rearrange the data in alphabetical order of stud_name and display
the data.

#include <stdio.h>
#include <string.h>

struct student
{
int roll_no;
char stud_name[50];
char address[100];
};

void main()
{
int n, i, j;
struct student s[10];

printf("Enter the number of students: ");


scanf("%d", &n);

for (i = 0; i < n; i++)


{
printf("Enter the details of student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &s[i].roll_no);
printf("Student name: ");
scanf("%s", s[i].stud_name);
printf("Address: ");
scanf("%s", s[i].address);
}
// Sort the students in alphabetical order of stud_name
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(s[i].stud_name, s[j].stud_name) > 0)
{
struct student temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}

// Display the sorted data


printf("\nThe sorted data is:\n");
for (i = 0; i < n; i++)
{
printf("Roll number: %d\n", s[i].roll_no);
printf("Student name: %s\n", s[i].stud_name);
printf("Address: %s\n", s[i].address);
printf("\n");
}
}

S14-1 Write C program to accept multiple strings in command line arguments and display length of each string
using predefined function.

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
// Iterate over the command line arguments, starting from the second argument (the first argument is the
program name).
for (int i = 1; i < argc; i++) {
// Get the length of the current argument string.
int length = strlen(argv[i]);

// Print the length of the string.


printf("The length of the string \"%s\" is %d\n", argv[i], length);
}

return 0;
}

To compile and run this program, you can use the following commands:
gcc -o string_length string_length.c
./string_length "Hello, world!" "This is a test. "

Output:
The length of the string "Hello, world!" is 12
The length of the string "This is a test is 13

S14-2 Write a program to store and access “id, name and percentage” for 3 students.(array
of structures)

#include <stdio.h>

struct student {
int id;
char name[20];
float percentage;
};

int main() {
struct student students[3];

// Get the details of the 3 students.


for (int i = 0; i < 3; i++) {
printf("Enter the details of student %d:\n", i + 1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Percentage: ");
scanf("%f", &students[i].percentage);
}

// Display the details of the 3 students.


printf("\nThe details of the 3 students are:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Percentage: %.2f\n", students[i].percentage);
}

return 0;
}

S15-1 Write a program to store and access “name, subject and percentage” for one student
. (Using union)

#include <stdio.h>
union student {
char name[20];
char subject[10];
int percentage;
};

int main() {
union student s;

// Store the student's name


strcpy(s.name, "John Doe");

// Store the student's subject


strcpy(s.subject, "Mathematics");

// Store the student's percentage


s.percentage = 85;

// Access the student's name


printf("Student's name: %s\n", s.name);

// Access the student's subject


printf("Student's subject: %s\n", s.subject);

// Access the student's percentage


printf("Student's percentage: %d\n", s.percentage);

return 0;
}
S15-3 Write C program to accept n student details like roll_no, stud_name, mark1, mark2,
mark3. Calculate the total and average of marks using structure.

#include <stdio.h>

// Define a structure for student details


struct Student {
int roll_no;
char stud_name[50];
float mark1, mark2, mark3;
float total, average;
};

int main() {
int n, i;

printf("Enter the number of students: ");


scanf("%d", &n);

// Declare an array of structures to store details of 'n' students


struct Student students[n];

// Input details for each student


for (i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].roll_no);
printf("Student name: ");
scanf("%s", students[i].stud_name);
printf("Marks in three subjects (mark1, mark2, mark3): ");
scanf("%f %f %f", &students[i].mark1, &students[i].mark2, &students[i].mark3);
// Calculate total and average marks
students[i].total = students[i].mark1 + students[i].mark2 + students[i].mark3;
students[i].average = students[i].total / 3;
}

// Displaying details for each student along with total and average marks
printf("\nStudent details:\n");
printf("Roll No\tName\tMark1\tMark2\tMark3\tTotal\tAverage\n");
for (i = 0; i < n; i++) {
printf("%d\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].roll_no, students[i].stud_name,
students[i].mark1, students[i].mark2, students[i].mark3,
students[i].total, students[i].average);
}

return 0;
}

S16-1 Write C program to find area of circle using macros.

#include <stdio.h>

#define PI 3.14159

int main() {
float radius, area;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;

printf("Area of circle : %f\n", area);


return 0;
}

S16-2 Write a program to find the number of vowels ,consonants, digits and white space in a string.

#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0, spaces = 0;

printf("Enter a string: ");


fgets(str, sizeof(str), stdin);

for (int i = 0; str[i] != '\0'; i++) {


char ch = toupper(str[i]);
if (isalpha(ch)) {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
vowels++;
else
consonants++;
} else if (isdigit(ch))
digits++;
else if (isspace(ch))
spaces++;
}

printf("Vowels: %d\nConsonants: %d\nDigits: %d\nSpaces: %d\n", vowels, consonants, digits, spaces);

return 0;
}

S16-3 Write a menu driven program in ‘C’ that shows the working of a library.
Book Information should be included in structure (Book no,book name,author).functions
should be Done. [20 Marks]
1.Add book information.
2.Display book information.

#include <stdio.h>
#include <stdlib.h>

struct book {
int bookno;
char bookname[20];
char author[20];
};

void addbook(struct book *b) {


printf("Enter book number: ");
scanf("%d", &b->bookno);
printf("Enter book name: ");
scanf("%s", b->bookname);
printf("Enter author name: ");
scanf("%s", b->author);
}

void displaybook(struct book *b) {


printf("Book number: %d\n", b->bookno);
printf("Book name: %s\n", b->bookname);
printf("Author name: %s\n", b->author);
}
int main() {
struct book b;
int choice;

while (1) {
printf("1. Add book information\n");
printf("2. Display book information\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addbook(&b);
break;
case 2:
displaybook(&b);
break;
case 3:
exit(0);
break;
default:
printf("Invalid choice\n");
}
}

return 0;
}
S18-3 Write a program to find sum and average of n elements entered by user. To perform this,
allocate memory dynamically using calloc() function.

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int *)calloc(n, sizeof(int)); // allocates memory for n integers


if (ptr == NULL) {
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");


for (i = 0; i < n; ++i) {
scanf("%d", ptr + i);
}

for (i = 0; i < n; ++i) {


sum += *(ptr + i);
}

printf("Sum = %d\n", sum);


printf("Average = %f\n", (float)sum / n);

free(ptr); // deallocates the memory

return 0;
}

S19-1 Write C program to read a string and find its length without using any library function

#include <stdio.h>

int main() {
char str[100];
int length = 0;

printf("Enter a string: ");


scanf("%s", str);

// Calculate length
while (str[length] != '\0') {
length++;
}

printf("Length of the string: %d\n", length);

return 0;
}

S20-1 Write a macro that will generate the code to make the sum of given 3 integers.

#include <stdio.h>

#define SUM_OF_THREE_INTS(a, b, c) ((a) + (b) + (c))

int main() {
int num1 = 5, num2 = 10, num3 = 15;
int sum = SUM_OF_THREE_INTS(num1, num2, num3);
printf("Sum of %d, %d, and %d is %d\n", num1, num2, num3, sum);
return 0;
}

S20-2 Write C Program that will find first occurrence of character using user defined function and
pointers

#include <stdio.h>

char *find_first_occurrence(char *str, char c) {


while (*str != '\0') {
if (*str == c) {
return str;
}
str++;
}
return NULL;
}

int main() {
char str[100];
char c;

printf("Enter a string: ");


scanf("%s", str);

printf("Enter a character to find: ");


scanf(" %c", &c);
char *ptr = find_first_occurrence(str, c);
if (ptr == NULL) {
printf("Character not found.\n");
} else {
printf("The first occurrence of '%c' is at index %d.\n", c, ptr - str);
}

return 0;
}

S20-3 Write a ‘C’ program to accept the book details such as BookID, Title, Author, Price. Read
the details of n number of books. Display the details of those books which are priced
below Rs.100.

#include <stdio.h>

struct Book
{
int BookID;
char Title[100];
char Author[100];
float Price;
};

int main()
{
int n, i;
struct Book book[100];

printf("Enter the number of books: ");


scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter details for book %d:\n", i + 1);
printf("Enter BookID: ");
scanf("%d", &book[i].BookID);
printf("Enter Title: ");
scanf("%s", book[i].Title);
printf("Enter Author: ");
scanf("%s", book[i].Author);
printf("Enter Price: ");
scanf("%f", &book[i].Price);
}

printf("\n\nDetails of books priced below Rs.100:\n");


for (i = 0; i < n; i++)
{
if (book[i].Price < 100)
{
printf("\nBookID: %d", book[i].BookID);
printf("\nTitle: %s", book[i].Title);
printf("\nAuthor: %s", book[i].Author);
printf("\nPrice: %.2f", book[i].Price);
}
}

return 0;
}

You might also like