1.
Write a program to find the average expenditure of a company for each month of each
year, each year and average over the range of years specified. Use arrays to construct a
table, display the table of expenditure and find the sum and average.
#include <stdio.h>
int main() {
int years, startYear;
float expenditure[10][12]; // 10 years max, 12 months per year
float yearlyTotal, overallTotal = 0.0;
printf("Enter number of years (max 10): ");
scanf("%d", &years);
printf("Enter starting year: ");
scanf("%d", &startYear);
for (int i = 0; i < years; i++) {
printf("\nEnter expenditures for year %d:\n", startYear + i);
for (int j = 0; j < 12; j++) {
printf("Month %d: ", j + 1);
scanf("%f", &expenditure[i][j]);
printf("\nYear\tTotal\t\tAverage\n");
for (int i = 0; i < years; i++) {
yearlyTotal = 0.0;
for (int j = 0; j < 12; j++) {
yearlyTotal += expenditure[i][j];
}
float yearlyAverage = yearlyTotal / 12.0;
overallTotal += yearlyTotal;
printf("%d\t%.2f\t%.2f\n", startYear + i, yearlyTotal, yearlyAverage);
float overallAverage = overallTotal / (years * 12);
printf("\nOverall Total: %.2f\n", overallTotal);
printf("Overall Monthly Average: %.2f\n", overallAverage);
return 0;
2. Write a program to find the position of the character 'C' in the sentence "idea without
execution is worthless" using pointer and string.
#include <stdio.h>
int main() {
char str[] = "idea without execution is worthless";
char *ptr = str;
char target = 'C';
int position = 0;
int found = 0;
while (*ptr != '\0') {
if (*ptr == target) {
found = 1;
break;
}
ptr++;
position++;
}
if (found) {
printf("Character '%c' found at position %d.\n", target, position);
} else {
printf("Character '%c' not found in the sentence.\n", target);
}
return 0;
}
3. Store and retrieve the name of the students and obtained marks in c programming in
1st semester using structure.
#include <stdio.h>
struct Student {
char name[50];
float marks;
};
int main() {
int n;
struct Student students[100];
printf("Enter number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("\nEnter name of student %d: ", i + 1);
scanf(" %[^\n]", students[i].name); // read string with spaces
printf("Enter marks of %s: ", students[i].name);
scanf("%f", &students[i].marks);
printf("\n--- Student Records for 1st Semester ---\n");
printf("Name\t\tMarks\n");
for (int i = 0; i < n; i++) {
printf("%s\t\t%.2f\n", students[i].name, students[i].marks);
return 0;
4. Write a program to read name, rollno, address, and phone number of each student in
your class using structure. Store the information in file so that you can recover the
information later. While recovering the information from the file sort the information
alphabetically according to the name.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
char name[50];
int rollno;
char address[100];
char phone[10];
};
void sortByName(struct Student s[], int n) {
struct Student temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(s[i].name, s[j].name) > 0) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
int main() {
int n;
struct Student students[100];
FILE *fp;
printf("Enter number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("\nEnter details of student %d\n", i + 1);
printf("Name: ");
scanf(" %[^\n]", students[i].name);
printf("Roll No: ");
scanf("%d", &students[i].rollno);
printf("Address: ");
scanf(" %[^\n]", students[i].address);
printf("Phone Number: ");
scanf(" %[^\n]", students[i].phone);
fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
for (int i = 0; i < n; i++) {
fprintf(fp, "%s\n%d\n%s\n%s\n",
students[i].name,
students[i].rollno,
students[i].address,
students[i].phone);
fclose(fp);
fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("Error reading file!\n");
return 1;
}
struct Student loaded[100];
int count = 0;
while (fgets(loaded[count].name, sizeof(loaded[count].name), fp)) {
loaded[count].name[strcspn(loaded[count].name, "\n")] = '\0';
fscanf(fp, "%d\n", &loaded[count].rollno);
fgets(loaded[count].address, sizeof(loaded[count].address), fp);
loaded[count].address[strcspn(loaded[count].address, "\n")] = '\0';
fgets(loaded[count].phone, sizeof(loaded[count].phone), fp);
loaded[count].phone[strcspn(loaded[count].phone, "\n")] = '\0';
count++;
fclose(fp);
sortByName(loaded, count);
printf("\n--- Sorted Student Records ---\n");
for (int i = 0; i < count; i++) {
printf("\nStudent %d\n", i + 1);
printf("Name: %s\n", loaded[i].name);
printf("Roll No: %d\n", loaded[i].rollno);
printf("Address: %s\n", loaded[i].address);
printf("Phone: %s\n", loaded[i].phone);
}
return 0;