Advance c Practical Important Questions
Advance c 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 :-
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];
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!";
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>
#include <stdio.h>
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;
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;
}
#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("\n\nBook details:\n");
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;
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;
length = strlen(str);
reverse_string(str);
return 0;
}
begin = str;
end = str + strlen(str) - 1;
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("\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;
// Accept name
printf("Name: ");
scanf("%s", s.name);
// Accept marks
printf("Marks: ");
scanf("%f", &s.marks);
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;
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 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;
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>
int main() {
char cities[MAX_CITIES][MAX_CITY_LENGTH];
int n;
printf("Enter the number of cities: ");
scanf("%d", &n);
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++;
}
}
return 0;
}
S8-1 Write C Program to check whether a number is positive, negative or zero using macros.
#include <stdio.h>
int main() {
int 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];
S9-1 Write C Program to find minimum of three numbers using nested macros.
#include <stdio.h>
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;
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;
return 0;
}
#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;
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;
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;
return 0;
}
S11-1 Write C program to multiply two numbers using function pointer.
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
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("\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;
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;
}
// 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;
return 0;
}
./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];
};
int main() {
struct employee emp1;
return 0;
}
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before 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];
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]);
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];
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;
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>
int main() {
int n, i;
// 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;
}
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
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;
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];
};
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);
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;
// Calculate length
while (str[length] != '\0') {
length++;
}
return 0;
}
S20-1 Write a macro that will generate the code to make the sum of given 3 integers.
#include <stdio.h>
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>
int main() {
char str[100];
char c;
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];
return 0;
}