C PROGRAM – USE OF IF, LOOPS, ARRAY, FUNCTION, POINTER, STRING
AIM:
To write a C program that reads a student’s name and marks in multiple subjects,
stores them in an array, uses a function with pointers to calculate the average,
and classifies the result using if–else statements and loops.
ALGORITHM:
1. Start the program.
2. Declare a string variable to store the student name.
3. Declare an integer array to store marks and variables for loop counter and number of subjects.
4. Declare a pointer to int and a float variable to store the average.
5. Read the student name using scanf.
6. Read the number of subjects (maximum 5) from the user.
7. If the number of subjects is less than or equal to 0 or greater than 5:
a. Print “Invalid number of subjects”.
b. Stop the program.
8. Make the pointer point to the marks array.
9. Using a for loop, read marks of all subjects from the user and store them in the array using the p
10. Call the function calc_average(), passing the marks array and number of subjects.
11. Inside calc_average():
a. Initialize sum = 0.
b. Use a for loop to add all marks.
c. Compute average = (float)sum / n.
d. Return the average to main().
12. In main(), print the student name and the average marks.
13. Use if–else if–else:
a. If average >= 75 → print “Distinction”.
b. Else if average >= 60 → print “First Class”.
c. Else if average >= 40 → print “Pass”.
d. Else → print “Fail”.
14. Use a while loop and pointer/array notation to print all marks again.
15. Stop the program.
PROGRAM:
/* Program to demonstrate use of if, loops, array, function, pointer and string */
#include <stdio.h>
#include <string.h>
/* Function declaration (uses pointer and array) */
float calc_average(int *marks, int n);
int main() {
char name[50]; /* string (character array) */
int marks[5]; /* 1D array */
int n, i;
float avg;
int *p; /* pointer to int */
/* Take name as input (string) */
printf("Enter your name: ");
scanf("%49s", name); /* reads a word as string */
/* Take number of subjects */
printf("How many subjects (max 5)? ");
scanf("%d", &n);
/* if statement to validate input */
if (n <= 0 || n > 5) {
printf("Invalid number of subjects.
");
return 0; /* stop program */
}
/* Use pointer with array */
p = marks; /* p points to first element of marks array */
printf("Enter %d marks (0–100):
", n);
for (i = 0; i < n; i++) { /* for loop */
printf("Mark %d: ", i + 1);
scanf("%d", &p[i]); /* same as scanf("%d", &marks[i]); */
}
/* Call function to calculate average (array passed as pointer) */
avg = calc_average(marks, n);
/* Use string and average */
printf("\nHello %s!\n", name);
printf("Your average mark = %.2f\n", avg);
/* if–else if–else to classify result */
if (avg >= 75) {
printf("Result: Distinction\n");
} else if (avg >= 60) {
printf("Result: First Class\n");
} else if (avg >= 40) {
printf("Result: Pass\n");
} else {
printf("Result: Fail\n");
}
/* Use while loop + pointer arithmetic to display all marks again */
printf("\nMarks you entered:\n");
i = 0;
while (i < n) {
printf("Subject %d: %d\n", i + 1, *(marks + i)); /* *(p + i) is also ok */
i++;
}
return 0;
}
/* Function definition */
/* marks is a pointer to int (array passed as pointer) */
float calc_average(int *marks, int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum += marks[i]; /* array notation */
/* sum += *(marks + i); // pointer notation (also correct) */
}
return (float)sum / n;
}
SAMPLE OUTPUT (Example):
Enter your name: Arjun
How many subjects (max 5)? 3
Enter 3 marks (0–100):
Mark 1: 80
Mark 2: 70
Mark 3: 75
Hello Arjun!
Your average mark = 75.00
Result: Distinction
Marks you entered:
Subject 1: 80
Subject 2: 70
Subject 3: 75